1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Models\SmartProperties\RecordsTraits\HasSmartProperties; |
4
|
|
|
|
5
|
|
|
use ByTIC\Models\SmartProperties\Properties\AbstractProperty\Generic as PropertyValue; |
6
|
|
|
use ByTIC\Models\SmartProperties\Properties\Definitions\Definition; |
7
|
|
|
use ByTIC\Models\SmartProperties\Properties\PropertiesRegistry; |
8
|
|
|
use Nip\Records\RecordManager; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class RecordTrait |
12
|
|
|
* @package ByTIC\Models\SmartProperties\RecordsTraits\HasStatus |
13
|
|
|
* |
14
|
|
|
* @property string $status |
15
|
|
|
* @method RecordManager|RecordsTrait getManager() |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
trait RecordTrait |
19
|
|
|
{ |
20
|
|
|
use \ByTIC\Models\SmartProperties\RecordsTraits\AbstractTrait\RecordTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param $name |
24
|
|
|
* @return PropertyValue |
25
|
|
|
*/ |
26
|
|
|
public function getSmartProperty($name) |
27
|
|
|
{ |
28
|
|
|
if ($this->getManager()->hasSmartPropertyDefinition($name) === false) { |
29
|
|
|
return null; |
30
|
9 |
|
} |
31
|
|
|
$definition = $this->getManager()->getSmartPropertyDefinition($name); |
32
|
9 |
|
return PropertiesRegistry::getWithInit($this, $definition, function () use ($definition) { |
33
|
|
|
return $this->getNewSmartPropertyFromDefinition($definition); |
34
|
9 |
|
}); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $name |
39
|
|
|
* @param $value |
40
|
9 |
|
* @return bool |
41
|
|
|
* @throws \Exception |
42
|
9 |
|
*/ |
43
|
8 |
|
public function updateSmartProperty($name, $value) |
44
|
|
|
{ |
45
|
9 |
|
if (empty($value)) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$newStatus = $this->getNewSmartPropertyFromValue($name, $value); |
50
|
8 |
|
$return = $newStatus->update(); |
51
|
|
|
$this->setSmartProperty($name, $newStatus); |
52
|
8 |
|
return $return; |
53
|
8 |
|
} |
54
|
8 |
|
|
55
|
8 |
|
/** |
56
|
|
|
* @param Definition $definition |
57
|
8 |
|
* @return PropertyValue |
58
|
|
|
* @internal Do not use |
59
|
|
|
*/ |
60
|
|
|
public function getNewSmartPropertyFromDefinition($definition) |
61
|
|
|
{ |
62
|
|
|
$name = $definition->getName(); |
63
|
8 |
|
$value = $this->getSmartPropertyValueFromDefinition($definition); |
64
|
|
|
|
65
|
8 |
|
return $this->getNewSmartPropertyFromValue($name, $value); |
66
|
8 |
|
} |
67
|
|
|
|
68
|
8 |
|
/** |
69
|
|
|
* @param Definition $definition |
70
|
|
|
* @return mixed |
71
|
|
|
* @internal Do not use |
72
|
|
|
*/ |
73
|
|
|
public function getSmartPropertyValueFromDefinition($definition) |
74
|
|
|
{ |
75
|
8 |
|
$field = $definition->getField(); |
76
|
|
|
$value = $this->getAttributeFromArray($field); |
|
|
|
|
77
|
8 |
|
if (empty($value)) { |
78
|
8 |
|
$value = $definition->getDefaultValue(); |
79
|
8 |
|
$this->setAttribute($field, $value); |
|
|
|
|
80
|
6 |
|
} |
81
|
|
|
|
82
|
|
|
return $value; |
83
|
8 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $name |
87
|
|
|
* @param $value |
88
|
|
|
* @return PropertyValue |
89
|
|
|
* @throws \Exception |
90
|
|
|
* @internal Do not use |
91
|
|
|
*/ |
92
|
8 |
|
public function getNewSmartPropertyFromValue($name, $value) |
93
|
|
|
{ |
94
|
8 |
|
$object = clone $this->getManager()->getSmartPropertyItem($name, $value); |
95
|
8 |
|
$object->setItem($this); |
|
|
|
|
96
|
|
|
|
97
|
8 |
|
return $object; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $name |
102
|
|
|
* @param PropertyValue|string $value |
103
|
|
|
* @throws \Exception |
104
|
|
|
*/ |
105
|
9 |
|
protected function setSmartProperty($name, $value) |
106
|
|
|
{ |
107
|
9 |
|
if (empty($value)) { |
108
|
9 |
|
return; |
109
|
9 |
|
} |
110
|
9 |
|
$definition = $this->getManager()->getSmartPropertyDefinition($name); |
111
|
9 |
|
$field = $definition->getField(); |
|
|
|
|
112
|
9 |
|
|
113
|
|
|
if (is_string($value)) { |
114
|
4 |
|
$value = $this->getNewSmartPropertyFromValue($name, $value); |
115
|
4 |
|
} |
116
|
4 |
|
|
117
|
1 |
|
$this->setPropertyValue($field, $value->getName()); |
|
|
|
|
118
|
|
|
|
119
|
4 |
|
$currentProperty = $this->getSmartProperty($name); |
120
|
4 |
|
if ($currentProperty->getName() === $value->getName()) { |
121
|
|
|
return; |
122
|
4 |
|
} |
123
|
|
|
PropertiesRegistry::set($this, $definition, $value); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|