TypeProperty   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 106
ccs 23
cts 23
cp 1
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 19 6
A newConstructorException() 0 5 2
A check() 0 4 1
A getAllowedTypes() 0 4 1
A getType() 0 4 1
A __toString() 0 4 1
1
<?php
2
namespace GenericCollections\Utils;
3
4
use GenericCollections\Exceptions\TypePropertyException;
5
6
/**
7
 * TypeProperty is a ValueObject utility class,
8
 * the only logic in here is validate the type parameter.
9
 *
10
 * @package GenericCollections\Utils
11
 */
12
class TypeProperty
13
{
14
    /**
15
     * Store the value type
16
     *
17
     * @var string
18
     */
19
    private $type;
20
21
    /**
22
     * Type checker for this property
23
     *
24
     * @var TypeChecker
25
     */
26
    private $checker;
27
28
    /**
29
     * Set if the type check allows null
30
     *
31
     * @var bool
32
     */
33
    private $allowNull;
34
35
    /**
36
     * @param string $type
37
     * @param bool $allowNull
38
     */
39 156
    public function __construct($type, $allowNull = false)
40
    {
41
        // type checks
42 156
        if (empty($type)) {
43 1
            throw $this->newConstructorException('is empty');
44
        }
45 155
        if (! is_string($type)) {
46 1
            throw $this->newConstructorException('is not a string');
47
        }
48
        // allowed types
49 154
        $allowed = $this->getAllowedTypes();
50 154
        if (is_array($allowed) && count($allowed) && ! in_array($type, $allowed, true)) {
51 1
            throw $this->newConstructorException('is not allowed', $type);
52
        }
53
        // object initiation
54 153
        $this->checker = new TypeChecker();
55 153
        $this->allowNull = (bool) $allowNull;
56 153
        $this->type = $type;
57 153
    }
58
59
    /**
60
     * Helper method to throw an exception
61
     *
62
     * @param string $reason
63
     * @param string $type
64
     * @return TypePropertyException
65
     */
66 3
    private function newConstructorException($reason, $type = '')
67
    {
68 3
        $type = ('' !== $type) ? " '" . $type . "'" : '';
69 3
        return new TypePropertyException('The type ' . $type . ' for ' . get_class($this) . ' ' . $reason);
70
    }
71
72
    /**
73
     * Check if a value match with this type. It may allow nulls.
74
     *
75
     * @param mixed $value
76
     * @return bool
77
     */
78 123
    public function check($value)
79
    {
80 123
        return $this->checker->checkType($this->getType(), $value, $this->allowNull);
81
    }
82
83
    /**
84
     * Return the allowed types for this property.
85
     * Its used for the validations inside the constructor.
86
     *
87
     * To restrict to a certain types return the array with the list of types
88
     *
89
     * If a non array is returned then any type is allowed
90
     *
91
     * @return array|null
92
     */
93 153
    public function getAllowedTypes()
94
    {
95 153
        return null;
96
    }
97
98
    /**
99
     * Returns the type specification of the values
100
     *
101
     * @return string
102
     */
103 130
    public function getType()
104
    {
105 130
        return $this->type;
106
    }
107
108
    /**
109
     * When converting this object to string it return the type
110
     * @see TypeProperty::getType
111
     * @return string
112
     */
113 18
    public function __toString()
114
    {
115 18
        return $this->getType();
116
    }
117
}
118