1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Marek\Toggable\API\Toggl\Values; |
4
|
|
|
|
5
|
|
|
use Marek\Toggable\API\Exception\PropertyNotFoundException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ValueObject |
9
|
|
|
* @package Marek\Toggable\API\Toggl\Values |
10
|
|
|
*/ |
11
|
|
|
abstract class ValueObject |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Construct object optionally with a set of properties. |
15
|
|
|
* |
16
|
|
|
* Readonly properties values must be set using $properties as they are not writable anymore |
17
|
|
|
* after object has been created. |
18
|
|
|
* |
19
|
|
|
* @param array $properties |
20
|
|
|
* |
21
|
|
|
* @throws \Marek\Toggable\API\Exception\PropertyNotFoundException |
22
|
|
|
*/ |
23
|
387 |
|
public function __construct(array $properties = array()) |
24
|
|
|
{ |
25
|
387 |
|
foreach ($properties as $property => $value) { |
26
|
339 |
|
if (property_exists($this, $property)) { |
27
|
338 |
|
$this->$property = $value; |
28
|
338 |
|
} else { |
29
|
1 |
|
throw new PropertyNotFoundException($property, get_class($this)); |
30
|
|
|
} |
31
|
387 |
|
} |
32
|
387 |
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $property |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
* |
40
|
|
|
* @throws \Marek\Toggable\API\Exception\PropertyNotFoundException |
41
|
|
|
* @throws \Marek\Toggable\API\Exception\PropertyReadOnlyException |
42
|
|
|
*/ |
43
|
48 |
View Code Duplication |
public function __get($property) |
|
|
|
|
44
|
|
|
{ |
45
|
48 |
|
if (property_exists($this, $property)) { |
46
|
|
|
|
47
|
47 |
|
return $this->$property; |
48
|
|
|
|
49
|
|
|
} |
50
|
1 |
|
throw new PropertyNotFoundException($property, get_class($this)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $property |
55
|
|
|
* @param $value |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
* |
59
|
|
|
* @throws \Marek\Toggable\API\Exception\PropertyNotFoundException |
60
|
|
|
* @throws \Marek\Toggable\API\Exception\PropertyReadOnlyException |
61
|
|
|
*/ |
62
|
13 |
View Code Duplication |
public function __set($property, $value) |
|
|
|
|
63
|
|
|
{ |
64
|
13 |
|
if (property_exists($this, $property)) { |
65
|
|
|
|
66
|
13 |
|
$this->$property = $value; |
67
|
|
|
|
68
|
13 |
|
} else { |
69
|
|
|
|
70
|
|
|
throw new PropertyNotFoundException($property, get_class($this)); |
71
|
|
|
|
72
|
|
|
} |
73
|
13 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Magic isset function handling isset() to non public properties |
77
|
|
|
* |
78
|
|
|
* @param string $property |
79
|
|
|
* |
80
|
|
|
* @return boolean |
81
|
|
|
*/ |
82
|
14 |
|
public function __isset($property) |
83
|
|
|
{ |
84
|
14 |
|
return !empty($this->$property); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.