1 | <?php |
||
20 | trait Setters |
||
21 | { |
||
22 | /** |
||
23 | * @param string $name |
||
24 | * @param mixed $value |
||
25 | * @return mixed|void |
||
26 | * @throws \InvalidArgumentException |
||
27 | * @throws \LogicException |
||
28 | */ |
||
29 | public function __set($name, $value) |
||
30 | { |
||
31 | // Try to call `setProperty` method |
||
32 | $setter = Str::getSetter($name); |
||
33 | if (method_exists($this, $setter)) { |
||
34 | return $this->{$setter}($value); |
||
35 | } |
||
36 | |||
37 | $parser = Registry::get($this); |
||
38 | if ($parser->has($name)) { |
||
39 | if (!$parser->isWritable($name)) throw new \LogicException( |
||
40 | sprintf('Can not set value to read only property %s::$%s', get_class($this), $name)); |
||
41 | if (!$parser->typesAreEqual($name, $value)) throw new \LogicException( |
||
42 | sprintf('Can not set value: types mismatch %s::$%s', get_class($this), $name)); |
||
43 | |||
44 | $this->setPropertyValue($name, $value); |
||
45 | } |
||
46 | |||
47 | $this->$name = $value; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param $name |
||
52 | * @throws \InvalidArgumentException |
||
53 | * @return bool |
||
54 | */ |
||
55 | public function __unset($name) |
||
75 | |||
76 | /** |
||
77 | * @param string $name |
||
78 | * @param $value |
||
79 | * @return void |
||
80 | */ |
||
81 | private function setPropertyValue($name, $value) |
||
87 | } |
||
88 |