1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Pfilsx\FormLayer\Layer; |
5
|
|
|
|
6
|
|
|
use Pfilsx\FormLayer\Exception\InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
abstract class EntityFormLayer implements FormLayerInterface |
9
|
|
|
{ |
10
|
|
|
protected $entity; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Associated entity FQN |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
|
|
abstract public static function getEntityClass(): string; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Returns entity identifier |
20
|
|
|
* @return int|null |
21
|
|
|
*/ |
22
|
|
|
public function getId(): ?int |
23
|
|
|
{ |
24
|
|
|
if ($this->entity !== null) { |
25
|
|
|
if (method_exists($this->entity, 'getId')) { |
26
|
|
|
return $this->entity->getId(); |
27
|
|
|
} |
28
|
|
|
if (property_exists($this->entity, 'id')) { |
29
|
|
|
return $this->entity->id; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
return null; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritDoc |
37
|
|
|
*/ |
38
|
|
|
public function load($entity) |
39
|
|
|
{ |
40
|
|
|
if (!is_a($entity, static::getEntityClass())) { |
41
|
|
|
throw new InvalidArgumentException('Expected instance of ' . static::getEntityClass() . ', got ' . get_class($entity)); |
42
|
|
|
} |
43
|
|
|
$this->entity = $entity; |
44
|
|
|
$this->loadLayerFields(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
|
|
public function create(bool $force = false) |
51
|
|
|
{ |
52
|
|
|
if ($force || $this->entity === null) { |
53
|
|
|
$className = static::getEntityClass(); |
54
|
|
|
$this->entity = new $className(); |
55
|
|
|
} |
56
|
|
|
$this->update(); |
57
|
|
|
return $this->entity; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
|
|
public function update() |
64
|
|
|
{ |
65
|
|
|
if ($this->entity !== null) { |
66
|
|
|
$this->loadEntityFields(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Loads data from associated entity |
72
|
|
|
*/ |
73
|
|
|
protected function loadLayerFields() |
74
|
|
|
{ |
75
|
|
|
foreach (get_object_vars($this) as $prop => $val) { |
76
|
|
|
$getter = 'get' . $prop; |
77
|
|
|
$value = $val; |
78
|
|
|
if (method_exists($this->entity, $getter)) { |
79
|
|
|
$value = $this->entity->$getter(); |
80
|
|
|
} elseif (property_exists($this->entity, $prop)) { |
81
|
|
|
$value = $this->entity->$prop; |
82
|
|
|
} |
83
|
|
|
$loadMethod = 'load' . $prop; |
84
|
|
|
if (method_exists($this, $loadMethod)) { |
85
|
|
|
$this->$loadMethod($value); |
86
|
|
|
} else { |
87
|
|
|
$this->$prop = $value; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Saves data into associated entity |
94
|
|
|
*/ |
95
|
|
|
protected function loadEntityFields() |
96
|
|
|
{ |
97
|
|
|
foreach (get_object_vars($this) as $prop => $value) { |
98
|
|
|
$saveMethod = 'save' . $prop; |
99
|
|
|
$value = method_exists($this, $saveMethod) ? $this->$saveMethod() : $this->$prop; |
100
|
|
|
$setter = 'set' . $prop; |
101
|
|
|
if (method_exists($this->entity, $setter)) { |
102
|
|
|
$this->entity->$setter($value); |
103
|
|
|
} elseif (property_exists($this->entity, $prop)) { |
104
|
|
|
$this->entity->$prop = $value; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|