Completed
Push — master ( 3e146f...5e7d15 )
by Jason
02:20
created

CollectionValidator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 71
c 0
b 0
f 0
ccs 33
cts 33
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A notAcceptedType() 0 3 1
A validateValueIsAcceptedType() 0 11 3
A validateNonNullValue() 0 5 2
A validateIntValue() 0 10 2
A validateNonEmptyAcceptedType() 0 5 2
A validateStringValue() 0 5 2
A validateOffsetExists() 0 5 2
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 72
    public function validateNonNullValue($value): void
26
    {
27 72
        if ($value === null)
28
        {
29 7
            throw new CollectionException('Collection operations will not accept null values.');
30
        }
31 70
    }
32
33
    /** {@inheritdoc} */
34 29
    public function validateOffsetExists($offset, AbstractCollection $collection): void
35
    {
36 29
        if ($collection->offsetExists($offset) === false)
37
        {
38 10
            throw new CollectionException("The key or index '$offset' does not exist in the collection.");
39
        }
40 19
    }
41
42
    /** {@inheritdoc} */
43 20
    public function validateIntValue($value): void
44
    {
45 20
        if (is_int($value) === false)
46
        {
47 3
            $message = sprintf(
48 3
                'Collection offset type must be an integer. %s given.',
49 3
                gettype($value)
50
            );
51
52 3
            throw new CollectionException($message);
53
        }
54 17
    }
55
56
    /** {@inheritdoc} */
57 27
    public function validateStringValue($value): void
58
    {
59 27
        if (is_string($value) === false)
60
        {
61 4
            throw new CollectionException('Offset to access must be a string.');
62
        }
63 25
    }
64
65
    /** {@inheritdoc} */
66 27
    public function validateNonEmptyAcceptedType(string $acceptedType): void
67
    {
68 27
        if ($acceptedType == '')
69
        {
70 3
            throw new CollectionException('Accepted type string cannot be empty.');
71
        }
72 24
    }
73
74
    /** {@inheritdoc} */
75 23
    public function validateValueIsAcceptedType($value, string $acceptedType): void
76
    {
77 23
        if ($this->notAcceptedType($value, $acceptedType))
78
        {
79 12
            $message = sprintf(
80 12
                'Unable to modify collection. Only instances of type "%s" are allowed. Type "%s" given.',
81 12
                $acceptedType,
82 12
                is_object($value) ? get_class($value) : gettype($value)
83
            );
84
85 12
            throw new CollectionException($message);
86
        }
87 17
    }
88
89
    /** {@inheritdoc} */
90 23
    private function notAcceptedType($value, string $acceptedType)
91
    {
92 23
        return ($value instanceof $acceptedType) === false;
93
    }
94
}