Skip to content

Commit d00f77a

Browse files
feat(client): use with for constructors
1 parent f566b27 commit d00f77a

29 files changed

+531
-347
lines changed

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ $client = new Client(
5050
environment: "environment_1",
5151
);
5252

53-
$params = SmartscraperCreateParams::from(
53+
$params = SmartscraperCreateParams::with(
5454
userPrompt: "Extract the product name, price, and description"
5555
);
5656
$completedSmartscraper = $client->smartscraper->create($params);
@@ -68,11 +68,11 @@ When the library is unable to connect to the API, or if the API returns a non-su
6868
use Scrapegraphai\Errors\APIConnectionError;
6969
use Scrapegraphai\Smartscraper\SmartscraperCreateParams;
7070

71+
$params = SmartscraperCreateParams::with(
72+
userPrompt: "Extract the product name, price, and description"
73+
);
7174
try {
72-
$params = SmartscraperCreateParams::from(
73-
userPrompt: "Extract the product name, price, and description"
74-
);
75-
$Smartscraper = $client->smartscraper->create($params);
75+
$Smartscraper = $client->smartscraper->create($params);
7676
} catch (APIConnectionError $e) {
7777
echo "The server could not be reached", PHP_EOL;
7878
var_dump($e->getPrevious());
@@ -117,12 +117,11 @@ use Scrapegraphai\Smartscraper\SmartscraperCreateParams;
117117

118118
// Configure the default for all requests:
119119
$client = new Client(maxRetries: 0);
120-
$params = SmartscraperCreateParams::from(
120+
$params = SmartscraperCreateParams::with(
121121
userPrompt: "Extract the product name, price, and description"
122122
);
123123

124-
// Or, configure per-request:
125-
$result = $client
124+
// Or, configure per-request:$result = $client
126125
->smartscraper
127126
->create($params, new RequestOptions(maxRetries: 5));
128127
```
@@ -143,7 +142,7 @@ Note: the `extra_` parameters of the same name overrides the documented paramete
143142
use Scrapegraphai\RequestOptions;
144143
use Scrapegraphai\Smartscraper\SmartscraperCreateParams;
145144

146-
$params = SmartscraperCreateParams::from(
145+
$params = SmartscraperCreateParams::with(
147146
userPrompt: "Extract the product name, price, and description"
148147
);
149148
$completedSmartscraper = $client

src/Crawl/CrawlStartParams.php

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ final class CrawlStartParams implements BaseModel
8383
#[Api(optional: true)]
8484
public ?bool $sitemap;
8585

86+
/**
87+
* `new CrawlStartParams()` is missing required properties by the API.
88+
*
89+
* To enforce required parameters use
90+
* ```
91+
* CrawlStartParams::with(url: ...)
92+
* ```
93+
*
94+
* Otherwise ensure the following setters are called
95+
*
96+
* ```
97+
* (new CrawlStartParams)->withURL(...)
98+
* ```
99+
*/
86100
public function __construct()
87101
{
88102
self::introspect();
@@ -94,7 +108,7 @@ public function __construct()
94108
*
95109
* You must use named parameters to construct any parameters with a default value.
96110
*/
97-
public static function from(
111+
public static function with(
98112
string $url,
99113
?int $depth = null,
100114
?bool $extractionMode = null,
@@ -124,87 +138,96 @@ public static function from(
124138
/**
125139
* Starting URL for crawling.
126140
*/
127-
public function setURL(string $url): self
141+
public function withURL(string $url): self
128142
{
129-
$this->url = $url;
143+
$obj = clone $this;
144+
$obj->url = $url;
130145

131-
return $this;
146+
return $obj;
132147
}
133148

134149
/**
135150
* Maximum crawl depth from starting URL.
136151
*/
137-
public function setDepth(int $depth): self
152+
public function withDepth(int $depth): self
138153
{
139-
$this->depth = $depth;
154+
$obj = clone $this;
155+
$obj->depth = $depth;
140156

141-
return $this;
157+
return $obj;
142158
}
143159

144160
/**
145161
* Use AI extraction (true) or markdown conversion (false).
146162
*/
147-
public function setExtractionMode(bool $extractionMode): self
163+
public function withExtractionMode(bool $extractionMode): self
148164
{
149-
$this->extractionMode = $extractionMode;
165+
$obj = clone $this;
166+
$obj->extractionMode = $extractionMode;
150167

151-
return $this;
168+
return $obj;
152169
}
153170

154171
/**
155172
* Maximum number of pages to crawl.
156173
*/
157-
public function setMaxPages(int $maxPages): self
174+
public function withMaxPages(int $maxPages): self
158175
{
159-
$this->maxPages = $maxPages;
176+
$obj = clone $this;
177+
$obj->maxPages = $maxPages;
160178

161-
return $this;
179+
return $obj;
162180
}
163181

164182
/**
165183
* Extraction prompt (required if extraction_mode is true).
166184
*/
167-
public function setPrompt(?string $prompt): self
185+
public function withPrompt(?string $prompt): self
168186
{
169-
$this->prompt = $prompt;
187+
$obj = clone $this;
188+
$obj->prompt = $prompt;
170189

171-
return $this;
190+
return $obj;
172191
}
173192

174193
/**
175194
* Enable heavy JavaScript rendering.
176195
*/
177-
public function setRenderHeavyJs(bool $renderHeavyJs): self
196+
public function withRenderHeavyJs(bool $renderHeavyJs): self
178197
{
179-
$this->renderHeavyJs = $renderHeavyJs;
198+
$obj = clone $this;
199+
$obj->renderHeavyJs = $renderHeavyJs;
180200

181-
return $this;
201+
return $obj;
182202
}
183203

184-
public function setRules(Rules $rules): self
204+
public function withRules(Rules $rules): self
185205
{
186-
$this->rules = $rules;
206+
$obj = clone $this;
207+
$obj->rules = $rules;
187208

188-
return $this;
209+
return $obj;
189210
}
190211

191212
/**
192213
* Output schema for extraction.
193214
*/
194-
public function setSchema(mixed $schema): self
215+
public function withSchema(mixed $schema): self
195216
{
196-
$this->schema = $schema;
217+
$obj = clone $this;
218+
$obj->schema = $schema;
197219

198-
return $this;
220+
return $obj;
199221
}
200222

201223
/**
202224
* Use sitemap for crawling.
203225
*/
204-
public function setSitemap(bool $sitemap): self
226+
public function withSitemap(bool $sitemap): self
205227
{
206-
$this->sitemap = $sitemap;
228+
$obj = clone $this;
229+
$obj->sitemap = $sitemap;
207230

208-
return $this;
231+
return $obj;
209232
}
210233
}

src/Crawl/CrawlStartParams/Rules.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct()
4343
*
4444
* @param null|list<string> $exclude
4545
*/
46-
public static function from(
46+
public static function with(
4747
?array $exclude = null,
4848
?bool $sameDomain = null
4949
): self {
@@ -60,20 +60,22 @@ public static function from(
6060
*
6161
* @param list<string> $exclude
6262
*/
63-
public function setExclude(array $exclude): self
63+
public function withExclude(array $exclude): self
6464
{
65-
$this->exclude = $exclude;
65+
$obj = clone $this;
66+
$obj->exclude = $exclude;
6667

67-
return $this;
68+
return $obj;
6869
}
6970

7071
/**
7172
* Restrict crawling to same domain.
7273
*/
73-
public function setSameDomain(bool $sameDomain): self
74+
public function withSameDomain(bool $sameDomain): self
7475
{
75-
$this->sameDomain = $sameDomain;
76+
$obj = clone $this;
77+
$obj->sameDomain = $sameDomain;
7678

77-
return $this;
79+
return $obj;
7880
}
7981
}

src/Feedback/FeedbackSubmitParams.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ final class FeedbackSubmitParams implements BaseModel
3939
#[Api('feedback_text', optional: true)]
4040
public ?string $feedbackText;
4141

42+
/**
43+
* `new FeedbackSubmitParams()` is missing required properties by the API.
44+
*
45+
* To enforce required parameters use
46+
* ```
47+
* FeedbackSubmitParams::with(rating: ..., requestID: ...)
48+
* ```
49+
*
50+
* Otherwise ensure the following setters are called
51+
*
52+
* ```
53+
* (new FeedbackSubmitParams)->withRating(...)->withRequestID(...)
54+
* ```
55+
*/
4256
public function __construct()
4357
{
4458
self::introspect();
@@ -50,7 +64,7 @@ public function __construct()
5064
*
5165
* You must use named parameters to construct any parameters with a default value.
5266
*/
53-
public static function from(
67+
public static function with(
5468
int $rating,
5569
string $requestID,
5670
?string $feedbackText = null
@@ -68,30 +82,33 @@ public static function from(
6882
/**
6983
* Rating score.
7084
*/
71-
public function setRating(int $rating): self
85+
public function withRating(int $rating): self
7286
{
73-
$this->rating = $rating;
87+
$obj = clone $this;
88+
$obj->rating = $rating;
7489

75-
return $this;
90+
return $obj;
7691
}
7792

7893
/**
7994
* Request to provide feedback for.
8095
*/
81-
public function setRequestID(string $requestID): self
96+
public function withRequestID(string $requestID): self
8297
{
83-
$this->requestID = $requestID;
98+
$obj = clone $this;
99+
$obj->requestID = $requestID;
84100

85-
return $this;
101+
return $obj;
86102
}
87103

88104
/**
89105
* Optional feedback comments.
90106
*/
91-
public function setFeedbackText(?string $feedbackText): self
107+
public function withFeedbackText(?string $feedbackText): self
92108
{
93-
$this->feedbackText = $feedbackText;
109+
$obj = clone $this;
110+
$obj->feedbackText = $feedbackText;
94111

95-
return $this;
112+
return $obj;
96113
}
97114
}

src/GenerateSchema/GenerateSchemaCreateParams.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ final class GenerateSchemaCreateParams implements BaseModel
3232
#[Api('existing_schema', optional: true)]
3333
public mixed $existingSchema;
3434

35+
/**
36+
* `new GenerateSchemaCreateParams()` is missing required properties by the API.
37+
*
38+
* To enforce required parameters use
39+
* ```
40+
* GenerateSchemaCreateParams::with(userPrompt: ...)
41+
* ```
42+
*
43+
* Otherwise ensure the following setters are called
44+
*
45+
* ```
46+
* (new GenerateSchemaCreateParams)->withUserPrompt(...)
47+
* ```
48+
*/
3549
public function __construct()
3650
{
3751
self::introspect();
@@ -43,7 +57,7 @@ public function __construct()
4357
*
4458
* You must use named parameters to construct any parameters with a default value.
4559
*/
46-
public static function from(
60+
public static function with(
4761
string $userPrompt,
4862
mixed $existingSchema = null
4963
): self {
@@ -59,20 +73,22 @@ public static function from(
5973
/**
6074
* Natural language description of desired schema.
6175
*/
62-
public function setUserPrompt(string $userPrompt): self
76+
public function withUserPrompt(string $userPrompt): self
6377
{
64-
$this->userPrompt = $userPrompt;
78+
$obj = clone $this;
79+
$obj->userPrompt = $userPrompt;
6580

66-
return $this;
81+
return $obj;
6782
}
6883

6984
/**
7085
* Existing schema to modify or extend.
7186
*/
72-
public function setExistingSchema(mixed $existingSchema): self
87+
public function withExistingSchema(mixed $existingSchema): self
7388
{
74-
$this->existingSchema = $existingSchema;
89+
$obj = clone $this;
90+
$obj->existingSchema = $existingSchema;
7591

76-
return $this;
92+
return $obj;
7793
}
7894
}

0 commit comments

Comments
 (0)