1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Entity; |
4
|
|
|
|
5
|
|
|
use Common\ContainerAccessors; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Entity\EntityContainer |
9
|
|
|
* |
10
|
|
|
* @property array $row - Entity row read from DB |
11
|
|
|
*/ |
12
|
|
|
class EntityContainer extends ContainerAccessors { |
13
|
|
|
/** |
14
|
|
|
* @var EntityModel $model |
15
|
|
|
*/ |
16
|
|
|
protected $model; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \Common\Accessors $accessors |
20
|
|
|
*/ |
21
|
|
|
protected $accessors; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array $original |
25
|
|
|
*/ |
26
|
|
|
protected $original = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array $delta |
30
|
|
|
*/ |
31
|
|
|
protected $delta = array(); |
32
|
|
|
|
33
|
|
|
/** @noinspection PhpMissingParentConstructorInspection */ |
34
|
|
|
/** |
35
|
|
|
* Entity\EntityContainer constructor. |
36
|
|
|
* |
37
|
|
|
* @param EntityModel $model |
38
|
|
|
*/ |
39
|
1 |
|
public function __construct($model) { |
40
|
1 |
|
$this->setModel($model); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param EntityModel $model |
45
|
|
|
*/ |
46
|
1 |
|
public function setModel(EntityModel $model) { |
47
|
1 |
|
$this->model = $model; |
48
|
1 |
|
$this->accessors = $model->getAccessors(); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return EntityModel |
53
|
|
|
*/ |
54
|
1 |
|
public function getModel() { |
55
|
1 |
|
return $this->model; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function __set($name, $value) { |
59
|
1 |
|
$properties = $this->model->getProperties(); |
60
|
1 |
|
if (isset($properties[$name][P_DB_FIELD_TYPE])) { |
61
|
1 |
|
$value = \classSupernova::$gc->types->castAs($properties[$name][P_DB_FIELD_TYPE], $value); |
|
|
|
|
62
|
1 |
|
} |
63
|
|
|
|
64
|
1 |
|
parent::__set($name, $value); |
65
|
|
|
// If it is first assign - saving this value as original |
66
|
1 |
|
if (!array_key_exists($name, $this->original)) { |
67
|
1 |
|
$this->original[$name] = $value; |
68
|
1 |
|
} // If it is not first assign |
69
|
1 |
|
elseif ($value != $this->original[$name]) { |
70
|
1 |
|
$this->delta[$name] = $value; |
71
|
|
|
// New value not equal original value. We should update delta |
72
|
1 |
|
if((is_int($value) || is_float($value))) { |
73
|
1 |
|
$this->delta[$name] -= $this->original[$name]; |
74
|
1 |
|
} |
75
|
1 |
|
} |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
public function clear() { |
79
|
1 |
|
parent::clear(); |
80
|
1 |
|
$this->original = array(); |
81
|
1 |
|
$this->delta = array(); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
1 |
|
public function isChanged() { |
86
|
1 |
|
return !empty($this->delta); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function getDeltas() { |
90
|
1 |
|
return $this->delta; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: