1
|
|
|
<?php namespace JobApis\Jobs\Client; |
2
|
|
|
|
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) |
49
|
|
|
{ |
50
|
12 |
|
return lcfirst(preg_replace('/[s|g]et|add/', '', $method)); |
51
|
|
|
} |
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) |
61
|
|
|
{ |
62
|
12 |
|
$attribute = $this->getAttributeFromGetSetMethod($method); |
63
|
12 |
|
return $this->{$attribute}; |
64
|
|
|
} |
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) |
74
|
|
|
{ |
75
|
14 |
|
return substr($method, 0, 3) === "get"; |
76
|
|
|
} |
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) |
86
|
|
|
{ |
87
|
14 |
|
return substr($method, 0, 3) === "set"; |
88
|
|
|
} |
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) |
99
|
|
|
{ |
100
|
10 |
|
$attribute = $this->getAttributeFromGetSetMethod($method); |
101
|
10 |
|
$value = count($parameters) ? $parameters[0] : null; |
102
|
10 |
|
$this->{$attribute} = $value; |
103
|
10 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|