1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Marek\Toggable\API\Toggl\Values; |
4
|
|
|
|
5
|
|
|
use Marek\Toggable\API\Exception\PropertyNotFoundException; |
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ValueObject |
10
|
|
|
* @package Marek\Toggable\API\Toggl\Values |
11
|
|
|
*/ |
12
|
|
|
abstract class ValueObject |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Construct object optionally with a set of properties. |
16
|
|
|
* |
17
|
|
|
* Readonly properties values must be set using $properties as they are not writable anymore |
18
|
|
|
* after object has been created. |
19
|
|
|
* |
20
|
|
|
* @param array $properties |
21
|
|
|
*/ |
22
|
301 |
|
public function __construct(array $properties = array()) |
23
|
|
|
{ |
24
|
301 |
|
foreach ($properties as $property => $value) { |
25
|
264 |
|
$this->$property = $value; |
26
|
301 |
|
} |
27
|
301 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $property |
31
|
|
|
* @param $value |
32
|
|
|
* |
33
|
|
|
* @throws \Marek\Toggable\API\Exception\PropertyNotFoundException |
34
|
|
|
*/ |
35
|
1 |
View Code Duplication |
public function __set($property, $value) |
|
|
|
|
36
|
|
|
{ |
37
|
1 |
|
if (property_exists($this, $property)) { |
38
|
|
|
$this->$property = $value; |
39
|
|
|
} else { |
40
|
1 |
|
throw new PropertyNotFoundException($property, get_class($this)); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $property |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
* |
49
|
|
|
* @throws \Marek\Toggable\API\Exception\PropertyNotFoundException |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function __get($property) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
if (property_exists($this, $property)) { |
54
|
|
|
return $this->$property; |
55
|
|
|
} |
56
|
|
|
throw new PropertyNotFoundException($property, get_class($this)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Magic isset function handling isset() to non public properties. |
61
|
|
|
* |
62
|
|
|
* Returns true for all (public/)protected/private properties. |
63
|
|
|
* |
64
|
|
|
* @ignore This method is for internal use |
65
|
|
|
* |
66
|
|
|
* @param string $property Name of the property |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function __isset($property) |
71
|
|
|
{ |
72
|
|
|
return property_exists($this, $property); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $property |
77
|
|
|
* |
78
|
|
|
* @throws \Marek\Toggable\API\Exception\PropertyNotFoundException |
79
|
|
|
*/ |
80
|
|
|
public function __unset($property) |
81
|
|
|
{ |
82
|
|
|
$this->__set($property, NULL); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.