Skip to content

Commit 595fd71

Browse files
committed
Upgraded the minimum PHP version to 8.1, added the new methods for mosparo 1.5, optimized and enhanced the tests, and updated the README.
1 parent db0357e commit 595fd71

23 files changed

Lines changed: 1880 additions & 82 deletions

README.md

Lines changed: 250 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@
1313
-----
1414

1515
## Description
16+
1617
With this PHP library you can connect to a mosparo installation and verify the submitted data.
1718

19+
The API client version 2.0+ is fully compatible with mosparo 1.5+. The verification method is also compatible with older mosparo versions.
20+
1821
## Installation
22+
1923
Install this library by using composer:
2024

2125
```text
2226
composer require mosparo/php-api-client
2327
```
2428

2529
## Usage
30+
2631
1. Create a project in your mosparo installation
2732
2. Include the mosparo script in your form
2833
```html
@@ -64,80 +69,312 @@ if ($result->isSubmittable()) {
6469
### Client
6570

6671
#### Client initialization
72+
6773
Create a new client object to use the API client.
74+
6875
```php
6976
/**
70-
* @param string $url URL of the mosparo installation
71-
* @param string $publicKey Public key of the mosparo project
72-
* @param string $privateKey Private key of the mosparo project
77+
* @param string $url URL of the mosparo installation.
78+
* @param string $publicKey Public key of the mosparo project.
79+
* @param string $privateKey Private key of the mosparo project.
7380
* @param array $args Arguments for the Guzzle client, see https://docs.guzzlephp.org/en/stable/request-options.html
7481
*/
75-
$client = new Mosparo\ApiClient\Client($url, $publicKey, $privateKey, $args);
82+
$client = new Mosparo\ApiClient\Client(string $url, string $publicKey, string $privateKey, array $args);
7683
```
7784

7885
#### Verify form data
86+
7987
To verify the form data, call `verifySubmission` with the form data in an array and the submit and validation tokens, which mosparo generated on the form initialization and the form data validation. The method will return a `VerificationResult` object.
88+
8089
```php
8190
/**
8291
* @param array $formData Array with the form values. All not-processed fields by mosparo (hidden, checkbox,
83-
* radio and so on) have to be removed from this array
84-
* @param string $mosparoSubmitToken Submit token which mosparo returned on the form initialization
85-
* @param string $mosparoValidationToken Validation token which mosparo returned after the form was validated
86-
* @return \Mosparo\ApiClient\VerificationResult Returns a VerificationResult object with the response from mosparo
92+
* radio and so on) have to be removed from this array.
93+
* @param string $mosparoSubmitToken Submit token that mosparo returned on the form initialization.
94+
* @param string $mosparoValidationToken Validation token that mosparo returned after the form was validated.
95+
* @return \Mosparo\ApiClient\VerificationResult Returns a VerificationResult object with the response from mosparo.
8796
*
8897
* @throws \Mosparo\ApiClient\Exception Submit or validation token not available.
8998
* @throws \Mosparo\ApiClient\Exception An error occurred while sending the request to mosparo.
9099
*/
91-
$result = $client->verifySubmission($formData, $mosparoSubmitToken, $mosparoValidationToken);
100+
$result = $client->verifySubmission(array $formData, string $mosparoSubmitToken, string $mosparoValidationToken);
101+
```
102+
103+
#### Store metadata
104+
105+
To store metadata with a submission, call `storeMetadata` with the metadata as an array and the submit and validation tokens, which mosparo generated during form initialization and form data validation. The method returns `true` when the metadata is saved correctly.
106+
107+
```php
108+
/**
109+
* @param string $mosparoSubmitToken Submit token that mosparo returned on the form initialization.
110+
* @param string $mosparoValidationToken Validation token that mosparo returned after the form was validated.
111+
* @param array $metadata An associative array with the metadata that should be added to the submission.
112+
* @return bool Returns true if the metadata is saved correctly.
113+
*
114+
* @throws \Mosparo\ApiClient\Exception Invalid response from the mosparo API.
115+
*/
116+
$result = $client->storeMetadata(string $mosparoSubmitToken, string $mosparoValidationToken, array $metadata);
92117
```
93118

94119
#### Request the statistical data
120+
95121
mosparo also has an API method to get the statistical data for a project. You can use the method `getStatisticByDate` to get the statistical data. You can specify the range in seconds or a start date from which mosparo should return the statistical data. This method will return a `StatisticResult` object.
122+
96123
```php
97124
/**
98-
* @param int $range = 0 The range in seconds for which mosparo should return the statistical data (will be rounded up to a full day since mosparo v1.1)
99-
* @param \DateTime $startDate = null The Start date from which on mosparo should return the statistical data (requires mosparo v1.1)
100-
* @return \Mosparo\ApiClient\StatisticResult Returns a StatisticResult object with the response from mosparo
125+
* @param int $range = 0 The range in seconds for which mosparo should return the statistical data (will be rounded up to a full day since mosparo v1.1).
126+
* @param \DateTime|null $startDate = null The Start date from which on mosparo should return the statistical data (requires mosparo v1.1).
127+
* @return \Mosparo\ApiClient\StatisticResult Returns a StatisticResult object with the response from mosparo.
101128
*
102129
* @throws \Mosparo\ApiClient\Exception An error occurred while sending the request to mosparo.
103130
*/
104-
$result = $client->getStatisticByDate($range, $startDate);
131+
$result = $client->getStatisticByDate(int $range = 0, ?DateTime $startDate = null);
132+
```
133+
134+
#### Stream the rule package hash index
135+
136+
To efficiently update a rule package using the batch API endpoint, first retrieve the full hash index to determine which rule items need updating. For this, mosparo offers an API that lets you download all the hashes so you can later decide based on this index.
137+
138+
```php
139+
/**
140+
* @param int $rulePackageId The ID of the rule package for which the hash index should be requested.
141+
* @param \SplFileObject $target The target to which the client should stream the result. This has to be a SplFileObject object.
142+
* @param callable $progressCallback The callback will be called after every processed megabyte, so that a progressbar or similar can be updated.
143+
* @return bool Returns true if everything worked as expected.
144+
*
145+
* @throws \Mosparo\ApiClient\Exception An error occurred while sending the request to mosparo.
146+
*/
147+
$result = $client->streamRulePackageHashIndex(int $rulePackageId, \SplFileObject $target, callable $progressCallback);
148+
```
149+
150+
#### Get the rule package rules
151+
152+
To get the content of a rule package, you can ask for the rules of a rule package. Use `getRulePackageRules` to get the rules of a rule package. This API endpoint uses pagination to limit the number of rules, so you can additionally specify which page and how many items per page you want to receive.
153+
154+
```php
155+
/**
156+
* @param int $rulePackageId The ID of the rule package.
157+
* @param int $page The page you want to receive.
158+
* @param int $perPage = 1000 The number of rules per page. Default: 1000
159+
* @return \Mosparo\ApiClient\RulePackageRulesResult The result containing the rules and other information.
160+
*
161+
* @throws \Mosparo\ApiClient\Exception An error occurred while sending the request to mosparo.
162+
*/
163+
$result = $client->getRulePackageRules(int $rulePackageId, int $page, int $perPage = 1000);
164+
```
165+
166+
#### Get the rule items of a rule package rule
167+
168+
To get the content of a rule inside a rule package, you can ask for the rule items. Use `getRulePackageRulesRuleItems` to get the rule items of a rule package rule. This API endpoint uses pagination to limit the number of rule items, so you can additionally specify which page and how many items per page you want to receive.
169+
170+
```php
171+
/**
172+
* @param int $rulePackageId The ID of the rule package.
173+
* @param int $rulePackageRuleId The ID of the rule inside the rule package.
174+
* @param int $page The page you want to receive.
175+
* @param int $perPage = 1000 The number of rule items per page. Default: 1000
176+
* @return \Mosparo\ApiClient\RulePackageRulesRuleItemsResult The result containing the rule items and other information.
177+
*
178+
* @throws \Mosparo\ApiClient\Exception An error occurred while sending the request to mosparo.
179+
*/
180+
$result = $client->getRulePackageRulesRuleItems(int $rulePackageId, int $rulePackageRuleId, int $page, int $perPage = 1000);
181+
```
182+
183+
#### Store a rule package
184+
185+
If you want to store a complete rule package in mosparo, use the `storeRulePackage` method. Specify the rule package ID, the content of the rule package, as well as the hash of the content.
186+
187+
```php
188+
/**
189+
* @param int $rulePackageId The ID of the rule package.
190+
* @param string $rulePackageContent The content of the rule package.
191+
* @param string $rulePackageHash The SHA256 hash of the content.
192+
* @return \Mosparo\ApiClient\RulePackageImportResult The result containing the information about the import.
193+
*
194+
* @throws \Mosparo\ApiClient\Exception An error occurred while sending the request to mosparo.
195+
*/
196+
$result = $client->storeRulePackage(int $rulePackageId, string $rulePackageContent, string $rulePackageHash);
197+
```
198+
199+
#### Batch update a rule package
200+
201+
To change the content of a rule package, you can use the batch functionality, which allows you to update only parts of a rule package rather than importing the whole package again. To use this method, it is recommended to get the hash index first. Specify the tasks in the correct format as described in the [API documentation](https://documentation.mosparo.io/docs/api/rule_package#properties-of-tasks).
202+
203+
```php
204+
/**
205+
* @param int $rulePackageId The ID of the rule package.
206+
* @param array $tasks The tasks that should be executed.
207+
* @return \Mosparo\ApiClient\RulePackageBatchResult The result containing the information about the executed tasks.
208+
*
209+
* @throws \Mosparo\ApiClient\Exception An error occurred while sending the request to mosparo.
210+
*/
211+
$result = $client->batchUpdateRulePackage(int $rulePackageId, array $tasks);
105212
```
106213

107214
### VerificationResult
108215

109216
#### Constants
217+
110218
- FIELD_NOT_VERIFIED: 'not-verified'
111219
- FIELD_VALID: 'valid'
112220
- FIELD_INVALID: 'invalid'
113221

114222
#### isSubmittable(): boolean
223+
115224
Returns true, if the form is submittable. This means that the verification was successful and the
116225
form data are valid.
117226

118227
#### isValid(): boolean
228+
119229
Returns true, if mosparo determined the form as valid. The difference to `isSubmittable()` is, that this
120230
is the original result from mosparo while `isSubmittable()` also checks if the verification was done correctly.
121231

122232
#### getVerifiedFields(): array (see Constants)
233+
123234
Returns an array with all verified field keys.
124235

125236
#### getVerifiedField($key): string (see Constants)
237+
126238
Returns the verification status of one field.
127239

128240
#### hasIssues(): boolean
241+
129242
Returns true, if there were verification issues.
130243

131244
#### getIssues(): array
245+
132246
Returns an array with all verification issues.
133247

134248
### StatisticResult
135249

136250
#### getNumberOfValidSubmissions(): int
251+
137252
Returns the total number of valid submissions in the requested date range.
138253

139254
#### getNumberOfSpamSubmissions(): int
255+
140256
Returns the total number of spam submissions in the requested date range.
141257

258+
#### getLastSubmissionAt(): ?\DateTime
259+
260+
Returns the timestamp of the last processed submission, or null if no submission was processed in the requested date range.
261+
142262
#### getNumbersByDate(): array
263+
143264
Returns an array with all statistical data for the requested time range. The date is the key in the array, while an array is set as a value. The array contains a key `numberOfValidSubmissions` with the number of valid submissions and a key `numberOfSpamSubmissions` with the number of spam submissions.
265+
266+
### RulePackageBatchResult
267+
268+
#### isSuccessful(): bool
269+
270+
Returns true if the process was successful.
271+
272+
#### getErrors(): array
273+
274+
Returns an array with the occurred errors. The tasks not mentioned in the errors were executed successfully.
275+
276+
### RulePackageImportResult
277+
278+
#### isSuccessful(): bool
279+
280+
Returns true if the import was successful.
281+
282+
#### isHashValidated(): bool
283+
284+
Returns true if the hash was validated by mosparo.
285+
286+
### RulePackageRulesResult
287+
288+
#### isSuccessful(): bool
289+
290+
Returns true if the request was successful.
291+
292+
#### getRules(): array
293+
294+
An array with RulePackageRule objects, containing the rules of the requested rule package.
295+
296+
#### getPage(): int
297+
298+
The page that was requested.
299+
300+
#### getTotalPages(): int
301+
302+
The total number of available pages for this rule package.
303+
304+
### RulePackageRulesRuleItemsResult
305+
306+
#### isSuccessful(): bool
307+
308+
Returns true if the request was successful.
309+
310+
#### getRuleItems(): array
311+
312+
An array with RulePackageRuleItem objects, containing the rule items of the requested rule package rule.
313+
314+
#### getPage(): int
315+
316+
The page that was requested.
317+
318+
#### getTotalPages(): int
319+
320+
The total number of available pages for this rule package rule.
321+
322+
### RulePackageRule
323+
324+
#### getId(): int
325+
326+
Returns the ID of the rule.
327+
328+
#### getUuid(): string
329+
330+
Returns the UUID of the rule.
331+
332+
#### getType(): string
333+
334+
Returns the type of the rule.
335+
336+
#### getName(): string
337+
338+
Returns the name of the rule.
339+
340+
#### getDescription(): string
341+
342+
Returns the description of the rule.
343+
344+
#### getNumberOfItems(): int
345+
346+
Returns the number of items in this rule.
347+
348+
#### getSpamRatingFactor(): float
349+
350+
Returns the spam rating factor.
351+
352+
#### getUpdatedAt(): ?\DateTime
353+
354+
Returns the timestamp of the last update or null, if the rule was never updated.
355+
356+
#### getListRoute(): string
357+
358+
Returns the route to get the rule's items.
359+
360+
### RulePackageRuleItem
361+
362+
#### getId(): int
363+
364+
Returns the ID of the rule item.
365+
366+
#### getUuid(): string
367+
368+
Returns the UUID of the rule item.
369+
370+
#### getType(): string
371+
372+
Returns the type of the rule item.
373+
374+
#### getValue(): string
375+
376+
Returns the value of the rule item.
377+
378+
#### getRating(): float
379+
380+
Returns the rating.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">=7.4",
14+
"php": ">=8.1",
1515
"guzzlehttp/guzzle": "^7.0",
16-
"ext-json": "*"
16+
"ext-json": "*"
1717
},
1818
"require-dev": {
1919
"phpunit/phpunit": "^9.5"

0 commit comments

Comments
 (0)