Skip to content

Commit 8a15782

Browse files
committed
major updated
1 parent fff45e6 commit 8a15782

11 files changed

+617
-53
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
VERSION 2.0.0
2+
-------------
3+
Release date: 2020-02-24
4+
5+
- [BC] renamed method expectNElement() to expectNKeys() in ArrayValidation
6+
- added expectNKeys() and expectOnlyOneFromKeys() to StrictArrayValidator
7+
- added interfaces ArrayValidationInterface and ArrayValidatorInterface
8+
- [BC] renamed all classes and interfaces with better name
9+
- added ValidationDefinition and StrictValidationFromDefinition
10+
111
VERSION 1.1.0
212
-------------
313
Release date: 2020-02-20

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,34 @@
88

99
composer require peak/array-validation
1010

11-
## Usage
11+
## Usage
12+
13+
General validation (stateless)
14+
15+
```php
16+
$validator = new Validator();
17+
18+
if ($validator->expectExactlyKeys($array, ['key1', 'key2', 'key3']) === true) {
19+
// ...
20+
}
21+
```
22+
23+
Advanced validation with fluent interface.
24+
25+
```php
26+
27+
$validation = new StrictValidation($array);
28+
29+
// will throw an exception if any of tests below fail
30+
$validation
31+
->expectOnlyKeys(['id', 'title', 'description', 'isPrivate', 'tags'])
32+
->expectAtLeastKeys(['id', 'title', 'description'])
33+
->expectKeyToBeInteger('id')
34+
->expectKeysToBeString(['title', 'description'])
35+
->expectKeyToBeBoolean('isPrivate')
36+
->expectKeyToBeArray('tags');
37+
38+
// if we reach this point, it means the array is
39+
// valid according to the validation rules above.
40+
41+
```

src/StrictArrayValidator.php renamed to src/StrictValidation.php

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
use Peak\ArrayValidation\Exception\InvalidStructureException;
88
use Peak\ArrayValidation\Exception\InvalidTypeException;
99

10-
class StrictArrayValidator
10+
class StrictValidation implements ValidationInterface
1111
{
1212
/**
13-
* @var ArrayValidation
13+
* @var Validator
1414
*/
15-
private $arrayValidation;
15+
private $validator;
1616

1717
/**
1818
* @var array
@@ -28,40 +28,41 @@ class StrictArrayValidator
2828
* @var array
2929
*/
3030
private $messages = [
31+
'expectedN' => '{dataName}invalid data, expected {nExpected} element(s), received {nReceived} element(s)',
3132
'expected' => '{dataName}invalid data, expected {expectedType} [{keysExpected}], received [{keysReceived}]',
3233
'type' => '{dataName}invalid type for key [{key}], type {expectedType} is expected',
3334
];
3435

3536
/**
36-
* StrictArrayValidator constructor.
37+
* StrictValidation constructor.
3738
* @param array $data
3839
* @param string|null $dataName
39-
* @param ArrayValidation|null $arrayValidation
40+
* @param Validator|null $validator
4041
*/
41-
public function __construct(array $data, string $dataName = null, ArrayValidation $arrayValidation = null)
42+
public function __construct(array $data, string $dataName = null, Validator $validator = null)
4243
{
4344
$this->data = $data;
4445
$this->dataName = $dataName;
45-
if (!isset($arrayValidation)) {
46-
$arrayValidation = new ArrayValidation();
46+
if (!isset($validator)) {
47+
$validator = new Validator();
4748
}
48-
$this->arrayValidation = $arrayValidation;
49+
$this->validator = $validator;
4950
}
5051

5152
/**
52-
* @param array $keysName
53+
* @param array $keys
5354
* @return $this
5455
* @throws InvalidStructureException
5556
*/
56-
public function expectExactlyKeys(array $keysName)
57+
public function expectExactlyKeys(array $keys)
5758
{
58-
if ($this->arrayValidation->expectExactlyKeys($this->data, $keysName) === false) {
59+
if ($this->validator->expectExactlyKeys($this->data, $keys) === false) {
5960
$keysReceived = array_keys($this->data);
60-
natsort($keysName);
61+
natsort($keys);
6162
natsort($keysReceived);
6263
$message = $this->getErrorMessage('expected', [
6364
'expectedType' => 'exactly keys',
64-
'keysExpected' => implode(', ', $keysName),
65+
'keysExpected' => implode(', ', $keys),
6566
'keysReceived' => implode(', ', $keysReceived)
6667
]);
6768
throw new InvalidStructureException($message);
@@ -70,19 +71,19 @@ public function expectExactlyKeys(array $keysName)
7071
}
7172

7273
/**
73-
* @param array $keysName
74+
* @param array $keys
7475
* @return $this
7576
* @throws InvalidStructureException
7677
*/
77-
public function expectAtLeastKeys(array $keysName)
78+
public function expectAtLeastKeys(array $keys)
7879
{
79-
if ($this->arrayValidation->expectAtLeastKeys($this->data, $keysName) === false) {
80+
if ($this->validator->expectAtLeastKeys($this->data, $keys) === false) {
8081
$keysReceived = array_keys($this->data);
81-
natsort($keysName);
82+
natsort($keys);
8283
natsort($keysReceived);
8384
$message = $this->getErrorMessage('expected', [
8485
'expectedType' => 'at least keys',
85-
'keysExpected' => implode(', ', $keysName),
86+
'keysExpected' => implode(', ', $keys),
8687
'keysReceived' => implode(', ', $keysReceived)
8788
]);
8889
throw new InvalidStructureException($message);
@@ -91,26 +92,67 @@ public function expectAtLeastKeys(array $keysName)
9192
}
9293

9394
/**
94-
* @param array $keysName
95+
* @param array $keys
9596
* @return $this
9697
* @throws InvalidStructureException
9798
*/
98-
public function expectOnlyKeys(array $keysName)
99+
public function expectOnlyKeys(array $keys)
99100
{
100-
if ($this->arrayValidation->expectOnlyKeys($this->data, $keysName) === false) {
101+
if ($this->validator->expectOnlyKeys($this->data, $keys) === false) {
101102
$keysReceived = array_keys($this->data);
102-
natsort($keysName);
103+
natsort($keys);
103104
natsort($keysReceived);
104105
$message = $this->getErrorMessage('expected', [
105106
'expectedType' => 'only keys',
106-
'keysExpected' => implode(', ', $keysName),
107+
'keysExpected' => implode(', ', $keys),
108+
'keysReceived' => implode(', ', $keysReceived)
109+
]);
110+
throw new InvalidStructureException($message);
111+
}
112+
return $this;
113+
}
114+
115+
/**
116+
* @param array $keys
117+
* @return $this
118+
* @throws InvalidStructureException
119+
*/
120+
public function expectOnlyOneFromKeys(array $keys)
121+
{
122+
if ($this->validator->expectOnlyOneFromKeys($this->data, $keys) === false) {
123+
$keysReceived = array_keys($this->data);
124+
natsort($keys);
125+
natsort($keysReceived);
126+
$message = $this->getErrorMessage('expected', [
127+
'expectedType' => 'only one of keys',
128+
'keysExpected' => implode(', ', $keys),
107129
'keysReceived' => implode(', ', $keysReceived)
108130
]);
109131
throw new InvalidStructureException($message);
110132
}
111133
return $this;
112134
}
113135

136+
/**
137+
* @param int $n
138+
* @return $this
139+
* @throws InvalidStructureException
140+
*/
141+
public function expectNKeys(int $n)
142+
{
143+
if ($this->validator->expectNKeys($this->data, $n) === false) {
144+
$keysReceived = array_keys($this->data);
145+
natsort($keysReceived);
146+
$message = $this->getErrorMessage('expectedN', [
147+
'expectedType' => 'only N keys',
148+
'nExpected' => $n,
149+
'nReceived' => count($keysReceived)
150+
]);
151+
throw new InvalidStructureException($message);
152+
}
153+
return $this;
154+
}
155+
114156
/**
115157
* @param string $key
116158
* @param bool $acceptNull
@@ -119,7 +161,7 @@ public function expectOnlyKeys(array $keysName)
119161
*/
120162
public function expectKeyToBeArray(string $key, bool $acceptNull = false)
121163
{
122-
if (array_key_exists($key, $this->data) && $this->arrayValidation->expectKeyToBeArray($this->data, $key, $acceptNull) === false) {
164+
if (array_key_exists($key, $this->data) && $this->validator->expectKeyToBeArray($this->data, $key, $acceptNull) === false) {
123165
$message = $this->getErrorMessage('type', [
124166
'key' => $key,
125167
'expectedType' => 'array',
@@ -137,7 +179,7 @@ public function expectKeyToBeArray(string $key, bool $acceptNull = false)
137179
*/
138180
public function expectKeyToBeInteger(string $key, bool $acceptNull = false)
139181
{
140-
if (array_key_exists($key, $this->data) && $this->arrayValidation->expectKeyToBeInteger($this->data, $key, $acceptNull) === false) {
182+
if (array_key_exists($key, $this->data) && $this->validator->expectKeyToBeInteger($this->data, $key, $acceptNull) === false) {
141183
$message = $this->getErrorMessage('type', [
142184
'key' => $key,
143185
'expectedType' => 'integer',
@@ -155,7 +197,7 @@ public function expectKeyToBeInteger(string $key, bool $acceptNull = false)
155197
*/
156198
public function expectKeyToBeFloat(string $key, bool $acceptNull = false)
157199
{
158-
if (array_key_exists($key, $this->data) && $this->arrayValidation->expectKeyToBeFloat($this->data, $key, $acceptNull) === false) {
200+
if (array_key_exists($key, $this->data) && $this->validator->expectKeyToBeFloat($this->data, $key, $acceptNull) === false) {
159201
$message = $this->getErrorMessage('type', [
160202
'key' => $key,
161203
'expectedType' => 'float',
@@ -173,7 +215,7 @@ public function expectKeyToBeFloat(string $key, bool $acceptNull = false)
173215
*/
174216
public function expectKeyToBeString(string $key, bool $acceptNull = false)
175217
{
176-
if (array_key_exists($key, $this->data) && $this->arrayValidation->expectKeyToBeString($this->data, $key, $acceptNull) === false) {
218+
if (array_key_exists($key, $this->data) && $this->validator->expectKeyToBeString($this->data, $key, $acceptNull) === false) {
177219
$message = $this->getErrorMessage('type', [
178220
'key' => $key,
179221
'expectedType' => 'string',
@@ -191,7 +233,7 @@ public function expectKeyToBeString(string $key, bool $acceptNull = false)
191233
*/
192234
public function expectKeyToBeBoolean(string $key, bool $acceptNull = false)
193235
{
194-
if (array_key_exists($key, $this->data) && $this->arrayValidation->expectKeyToBeBoolean($this->data, $key, $acceptNull) === false) {
236+
if (array_key_exists($key, $this->data) && $this->validator->expectKeyToBeBoolean($this->data, $key, $acceptNull) === false) {
195237
$message = $this->getErrorMessage('type', [
196238
'key' => $key,
197239
'expectedType' => 'boolean',
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Peak\ArrayValidation;
6+
7+
class StrictValidationFromDefinition extends StrictValidation
8+
{
9+
/**
10+
* @var ValidationDefinition
11+
*/
12+
private $arrayValidationDefinition;
13+
14+
/**
15+
* StrictArrayValidatorFromDefinition constructor.
16+
* @param ValidationDefinition $arrayValidationDefinition
17+
* @param array $data
18+
* @param string|null $dataName
19+
* @param Validator|null $arrayValidation
20+
*/
21+
public function __construct(
22+
ValidationDefinition $arrayValidationDefinition,
23+
array $data,
24+
string $dataName = null,
25+
Validator $arrayValidation = null
26+
) {
27+
$this->arrayValidationDefinition = $arrayValidationDefinition;
28+
parent::__construct($data, $dataName, $arrayValidation);
29+
30+
$validations = $arrayValidationDefinition->getValidations();
31+
foreach ($validations as $name => $args) {
32+
call_user_func_array([$this, $name], $args);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)