1 | <?php namespace JobApis\Jobs\Client; |
||
3 | trait AttributeTrait |
||
4 | { |
||
5 | /** |
||
6 | * Magic method to check if property is set |
||
7 | * |
||
8 | * @param string $name |
||
9 | * |
||
10 | * @return boolean |
||
11 | */ |
||
12 | 2 | public function __isset($name) |
|
13 | { |
||
14 | 2 | return (property_exists($this, $name)); |
|
15 | } |
||
16 | |||
17 | /** |
||
18 | * Magic method to handle get and set methods for properties |
||
19 | * |
||
20 | * @param string $method |
||
21 | * @param array $parameters |
||
22 | * |
||
23 | * @return mixed |
||
24 | * @throws \BadMethodCallException |
||
25 | */ |
||
26 | 14 | public function __call($method, $parameters) |
|
27 | { |
||
28 | 14 | if ($this->isSetterMethod($method)) { |
|
29 | 10 | return $this->setAttributeValueFromMethod($method, $parameters); |
|
30 | 14 | } elseif ($this->isGetterMethod($method)) { |
|
31 | 12 | return $this->getAttributeValueFromMethod($method); |
|
32 | } |
||
33 | |||
34 | 2 | throw new \BadMethodCallException(sprintf( |
|
35 | 2 | '%s does not contain a method by the name of "%s"', |
|
36 | 2 | __CLASS__, |
|
37 | $method |
||
38 | 2 | )); |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * Get property name from get and set method names |
||
43 | * |
||
44 | * @param string $method |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | 12 | private function getAttributeFromGetSetMethod($method) |
|
52 | |||
53 | /** |
||
54 | * Gets an attribute based on arbitrary method name |
||
55 | * |
||
56 | * @param string $method |
||
57 | * |
||
58 | * @return mixed |
||
59 | */ |
||
60 | 12 | private function getAttributeValueFromMethod($method) |
|
65 | |||
66 | /** |
||
67 | * Checks if given method name is a get method |
||
68 | * |
||
69 | * @param string $method |
||
70 | * |
||
71 | * @return boolean |
||
72 | */ |
||
73 | 14 | private function isGetterMethod($method) |
|
77 | |||
78 | /** |
||
79 | * Checks if given method name is a set method |
||
80 | * |
||
81 | * @param string $method |
||
82 | * |
||
83 | * @return boolean |
||
84 | */ |
||
85 | 14 | private function isSetterMethod($method) |
|
89 | |||
90 | /** |
||
91 | * Sets an attribute based on arbitrary method name and parameters |
||
92 | * |
||
93 | * @param string $method |
||
94 | * @param array $parameters |
||
95 | * |
||
96 | * @return object |
||
97 | */ |
||
98 | 10 | private function setAttributeValueFromMethod($method, $parameters) |
|
105 | } |
||
106 |