1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Part of the Fusion.Collection package. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Fusion\Collection; |
12
|
|
|
|
13
|
|
|
use Fusion\Collection\Contracts\AbstractCollection; |
14
|
|
|
use Fusion\Collection\Contracts\CollectionValidationInterface; |
15
|
|
|
use Fusion\Collection\Exceptions\CollectionException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Library-provided implementation of the `CollectionValidationInterface`. |
19
|
|
|
* |
20
|
|
|
* @since 2.0.0 |
21
|
|
|
*/ |
22
|
|
|
class CollectionValidator implements CollectionValidationInterface |
23
|
|
|
{ |
24
|
|
|
/** {@inheritdoc} */ |
25
|
68 |
|
public function validateNonNullValue($value): void |
26
|
|
|
{ |
27
|
68 |
|
if ($value === null) { |
28
|
7 |
|
throw new CollectionException('Collection operations will not accept null values.'); |
29
|
|
|
} |
30
|
66 |
|
} |
31
|
|
|
|
32
|
|
|
/** {@inheritdoc} */ |
33
|
26 |
|
public function validateOffsetExists($offset, AbstractCollection $collection): void |
34
|
|
|
{ |
35
|
26 |
|
if ($collection->offsetExists($offset) === false) { |
36
|
10 |
|
throw new CollectionException("The key or index '$offset' does not exist in the collection."); |
37
|
|
|
} |
38
|
16 |
|
} |
39
|
|
|
|
40
|
|
|
/** {@inheritdoc} */ |
41
|
18 |
|
public function validateIntValue($value): void |
42
|
|
|
{ |
43
|
18 |
|
if (is_int($value) === false) { |
44
|
3 |
|
$message = sprintf( |
45
|
3 |
|
'Collection offset type must be an integer. %s given.', |
46
|
3 |
|
gettype($value) |
47
|
|
|
); |
48
|
|
|
|
49
|
3 |
|
throw new CollectionException($message); |
50
|
|
|
} |
51
|
15 |
|
} |
52
|
|
|
|
53
|
|
|
/** {@inheritdoc} */ |
54
|
25 |
|
public function validateStringValue($value): void |
55
|
|
|
{ |
56
|
25 |
|
if (is_string($value) === false) { |
57
|
4 |
|
throw new CollectionException('Offset to access must be a string.'); |
58
|
|
|
} |
59
|
23 |
|
} |
60
|
|
|
|
61
|
|
|
/** {@inheritdoc} */ |
62
|
26 |
|
public function validateNonEmptyAcceptedType(string $acceptedType): void |
63
|
|
|
{ |
64
|
26 |
|
if ($acceptedType == '') { |
65
|
3 |
|
throw new CollectionException('Accepted type string cannot be empty.'); |
66
|
|
|
} |
67
|
23 |
|
} |
68
|
|
|
|
69
|
|
|
/** {@inheritdoc} */ |
70
|
22 |
|
public function validateValueIsAcceptedType($value, string $acceptedType): void |
71
|
|
|
{ |
72
|
22 |
|
if ($this->notAcceptedType($value, $acceptedType)) { |
73
|
12 |
|
$message = sprintf( |
74
|
12 |
|
'Unable to modify collection. Only instances of type "%s" are allowed. Type "%s" given.', |
75
|
|
|
$acceptedType, |
76
|
12 |
|
is_object($value) ? get_class($value) : gettype($value) |
77
|
|
|
); |
78
|
|
|
|
79
|
12 |
|
throw new CollectionException($message); |
80
|
|
|
} |
81
|
16 |
|
} |
82
|
|
|
|
83
|
22 |
|
private function notAcceptedType($value, string $acceptedType): bool |
84
|
|
|
{ |
85
|
22 |
|
return ($value instanceof $acceptedType) === false; |
86
|
|
|
} |
87
|
|
|
} |