Skip to content

Commit cfffad2

Browse files
committed
Updated the client regarding the mosparo v1.1 changes and updated the README
1 parent 4be2213 commit cfffad2

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,27 @@ To verify the form data, call `verifySubmission` with the form data in an array
8383
* radio and so on) have to be removed from this array
8484
* @param string $mosparoSubmitToken Submit token which mosparo returned on the form initialization
8585
* @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
86+
* @return \Mosparo\ApiClient\VerificationResult Returns a VerificationResult object with the response from mosparo
8787
*
8888
* @throws \Mosparo\ApiClient\Exception Submit or validation token not available.
8989
* @throws \Mosparo\ApiClient\Exception An error occurred while sending the request to mosparo.
9090
*/
9191
$result = $client->verifySubmission($formData, $mosparoSubmitToken, $mosparoValidationToken);
9292
```
9393

94+
#### Request the statistical data
95+
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.
96+
```php
97+
/**
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
101+
*
102+
* @throws \Mosparo\ApiClient\Exception An error occurred while sending the request to mosparo.
103+
*/
104+
$result = $client->getStatisticByDate($range, $startDate);
105+
```
106+
94107
### VerificationResult
95108

96109
#### Constants
@@ -116,4 +129,15 @@ Returns the verification status of one field.
116129
Returns true, if there were verification issues.
117130

118131
#### getIssues(): array
119-
Returns an array with all verification issues.
132+
Returns an array with all verification issues.
133+
134+
### StatisticResult
135+
136+
#### getNumberOfValidSubmissions(): int
137+
Returns the total number of valid submissions in the requested date range.
138+
139+
#### getNumberOfSpamSubmissions(): int
140+
Returns the total number of spam submissions in the requested date range.
141+
142+
#### getNumbersByDate(): array
143+
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.

src/Client.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,14 @@ public function validateSubmission(array $formData, string $submitToken = null,
155155
* Retrieves the statistics data from mosparo for the given time range,
156156
* counted by date.
157157
*
158-
* @param int $range Time range in seconds
158+
* @param int $range Time range in seconds (will be rounded up to a full day since mosparo v1.1)
159+
* @param \DateTime $startDate The start date from which the statistics are to be returned (requires mosparo v1.1)
159160
* @return \Mosparo\ApiClient\StatisticResult
160161
*
161-
* @throws \Mosparo\ApiClient\Exception Submit or validation token not available.
162162
* @throws \Mosparo\ApiClient\Exception An error occurred while sending the request to mosparo.
163+
* @throws \Mosparo\ApiClient\Exception Response from API invalid.
163164
*/
164-
public function getStatisticByDate(int $range = 0): StatisticResult
165+
public function getStatisticByDate(int $range = 0, \DateTime $startDate = null): StatisticResult
165166
{
166167
$requestHelper = new RequestHelper($this->publicKey, $this->privateKey);
167168

@@ -172,6 +173,10 @@ public function getStatisticByDate(int $range = 0): StatisticResult
172173
$queryData['range'] = $range;
173174
}
174175

176+
if ($startDate !== null) {
177+
$queryData['startDate'] = $startDate->format('Y-m-d');
178+
}
179+
175180
$requestSignature = $requestHelper->createHmacHash($apiEndpoint . $requestHelper->toJson($queryData));
176181

177182
$data = [

src/RequestHelper.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ public function prepareFormData(array $formData): array
6868
} else {
6969
if (is_numeric($value)) {
7070
$value = strval($value);
71-
} else if ($value === null) {
72-
$value = '';
7371
}
7472

7573
$data[$key] = hash('sha256', $value);

tests/ClientTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,42 @@ public function testGetStatisticByDateWithRange()
236236
$this->assertEquals($numbersByDate, $result->getNumbersByDate());
237237
}
238238

239+
public function testGetStatisticByDateWithStartDate()
240+
{
241+
$publicKey = 'testPublicKey';
242+
$privateKey = 'testPrivateKey';
243+
$numbersByDate = [
244+
'2021-04-29' => [
245+
'numberOfValidSubmissions' => 2,
246+
'numberOfSpamSubmissions' => 5
247+
]
248+
];
249+
250+
// Set the response
251+
$this->handler->append(new Response(200, ['Content-Type' => 'application/json'], json_encode([
252+
'result' => true,
253+
'data' => [
254+
'numberOfValidSubmissions' => 2,
255+
'numberOfSpamSubmissions' => 5,
256+
'numbersByDate' => $numbersByDate
257+
]
258+
])));
259+
260+
// Start the test
261+
$apiClient = new Client('http://test.local', $publicKey, $privateKey, ['handler' => $this->handlerStack]);
262+
263+
$result = $apiClient->getStatisticByDate(0, new \DateTime('2024-01-01'));
264+
265+
// Check the result
266+
$this->assertInstanceOf(StatisticResult::class, $result);
267+
$this->assertEquals(count($this->history), 1);
268+
$this->assertEquals('startDate=2024-01-01', $this->history[0]['request']->getUri()->getQuery());
269+
270+
$this->assertEquals(2, $result->getNumberOfValidSubmissions());
271+
$this->assertEquals(5, $result->getNumberOfSpamSubmissions());
272+
$this->assertEquals($numbersByDate, $result->getNumbersByDate());
273+
}
274+
239275
public function testGetStatisticByDateReturnsError()
240276
{
241277
$this->expectException(Exception::class);
@@ -255,4 +291,13 @@ public function testGetStatisticByDateReturnsError()
255291

256292
$result = $apiClient->getStatisticByDate();
257293
}
294+
295+
public function testDeprecatedValidateSubmissionMethod()
296+
{
297+
$this->expectException(Exception::class);
298+
$this->expectExceptionMessage('Submit or validation token not available.');
299+
300+
$apiClient = new Client('http://test.local', 'testPublicKey', 'testPrivateKey', ['handler' => $this->handlerStack]);
301+
$result = $apiClient->validateSubmission(['name' => 'John Example']);
302+
}
258303
}

0 commit comments

Comments
 (0)