1
|
|
|
<?php namespace C4tech\RayEmitter\Domain; |
2
|
|
|
|
3
|
|
|
use C4tech\RayEmitter\Contracts\Domain\Entity as EntityInterface; |
4
|
|
|
use C4tech\RayEmitter\Contracts\Domain\ValueObject as ValueObjectInterface; |
5
|
|
|
use C4tech\RayEmitter\Exceptions\UnknownProperty; |
6
|
|
|
|
7
|
|
|
abstract class Entity implements EntityInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The Entity's identity. |
11
|
|
|
* @var ValueObjectInterface |
12
|
|
|
*/ |
13
|
|
|
protected $identity; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @inheritDoc |
17
|
|
|
*/ |
18
|
13 |
|
public function __construct(ValueObjectInterface $identifier) |
19
|
|
|
{ |
20
|
13 |
|
if (method_exists($this, 'setId')) { |
21
|
5 |
|
$this->setId($identifier); |
|
|
|
|
22
|
5 |
|
} else { |
23
|
9 |
|
$this->identity = $identifier; |
24
|
|
|
} |
25
|
13 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
*/ |
30
|
2 |
|
public function getId() |
31
|
|
|
{ |
32
|
2 |
|
return $this->identity; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set |
37
|
|
|
* |
38
|
|
|
* Protected method to set property value. |
39
|
|
|
* @param string $property Requested "property" |
40
|
|
|
* @param mixed $value Requested value |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
4 |
|
protected function set($property, $value) |
44
|
|
|
{ |
45
|
4 |
|
$method = 'set' . studly_case($property); |
46
|
|
|
|
47
|
4 |
|
if ($property !== 'id' && method_exists($this, $method)) { |
48
|
2 |
|
$this->$method($value); |
49
|
4 |
|
} elseif ($property !== 'identity' && property_exists($this, $property)) { |
50
|
1 |
|
$this->$property = $value; |
51
|
1 |
|
} else { |
52
|
1 |
|
throw new UnknownProperty('There is no way to set ' . $property, 504); |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Magic Getter |
59
|
|
|
* |
60
|
|
|
* Expose getter methods as properties. |
61
|
|
|
* @param string $property Requested "property" |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
4 |
|
public function __get($property) |
65
|
|
|
{ |
66
|
4 |
|
$method = 'get' . studly_case($property); |
67
|
|
|
|
68
|
4 |
|
if (method_exists($this, $method)) { |
69
|
1 |
|
return $this->$method(); |
70
|
3 |
|
} elseif (isset($this->$property)) { |
71
|
2 |
|
return $this->$property; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
throw new UnknownProperty('Cannot find the property ' . $property . ' on ' . get_class($this), 504); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.