Completed
Push — master ( 41fc83...fb17b5 )
by Luka
02:57
created

src/Object/TypedProperty.php (1 issue)

private methods are used.

Unused Code Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Teto\Object;
3
use InvalidArgumentException;
4
use OutOfRangeException;
5
6
/**
7
 * Typed property function for class
8
 *
9
 * @package    Teto
10
 * @subpackage Object
11
 * @copyright  2014 USAMI Kenta
12
 * @license    http://www.apache.org/licenses/LICENSE-2.0
13
 * @author     USAMI Kenta <[email protected]>
14
 */
15
trait TypedProperty
16
{
17
    use TypeAssert;
18
19
    /** @var array */
20
    private $properties = [];
21
22
    /**
23
     * Set property (magic method)
24
     *
25
     * @param  string $name
26
     * @param  mixed  $value
27
     * @throws \OutOfRangeException      If you set to undefined property
28
     * @throws \InvalidArgumentException If differed from the defined type
29
     * @throws \RangeException           If differed from the defined length
30
     * @link   http://php.net/manual/language.oop5.magic.php
31
     */
32 25
    public function __set($name, $value)
33
    {
34 25
        if (!isset(self::$property_types[$name])) {
35 1
            throw new \OutOfRangeException("Unexpected key:'$name'");
36
        }
37
38 24
        $type = TypeDefinition::parse(self::$property_types[$name]);
39
40 24
        if ($type->is_array) {
41 13
            self::assertArrayOrObject($value);
42 12
            $values = $value;
43
        } else {
44 11
            $values = [$value];
45
        }
46
47 23
        foreach ($values as $v) {
48 20
            self::assertValue($type->expected, $name, $v, $type->is_nullable);
49
        }
50 17
        if ($type->len !== null && count($value) !== $type->len) {
51 2
            throw new \RangeException("Unexpected length:{$name} (expects {$type->len})");
52
        }
53
54 15
        $this->properties[$name] = $value;
55 15
    }
56
57
    /**
58
     * Get property (magic method)
59
     *
60
     * @param  string $name
61
     * @return mixed
62
     * @link   http://php.net/manual/language.oop5.magic.php
63
     */
64 15
    public function __get($name)
65
    {
66 15
        if (!isset(self::$property_types[$name])) {
67
            throw new \OutOfRangeException("Unexpected key:'$name'");
68
        }
69
70 15
        return isset($this->properties[$name]) ? $this->properties[$name] : null;
71
    }
72
73
    /**
74
     * @return boolean
75
     * @link   http://php.net/manual/language.oop5.magic.php
76
     */
77 15
    public function __isset($name)
78
    {
79 15
        return isset($this->properties[$name]);
80
    }
81
82
    /**
83
     * @link http://php.net/manual/language.oop5.magic.php
84
     */
85
    public function __unset($name)
86
    {
87
        if (isset(self::$property_types[$name])) {
88
            $this->properties[$name] = null;
89
        }
90
    }
91
92
    /**
93
     * @param array
94
     */
95
    private function setProperties(array $properties)
1 ignored issue
show
This method is not used, and could be removed.
Loading history...
96
    {
97
        foreach (self::$property_types as $name => $_) {
98
            if (isset($properties[$name])) {
99
                $this->$name = $properties[$name];
100
            }
101
        }
102
    }
103
}
104