1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\System\EAV; |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | class Attribute |
19: | { |
20: | |
21: | |
22: | |
23: | public $Id; |
24: | |
25: | |
26: | |
27: | |
28: | public $EntityId; |
29: | |
30: | |
31: | |
32: | |
33: | public $Name; |
34: | |
35: | |
36: | |
37: | |
38: | public $Value; |
39: | |
40: | |
41: | |
42: | |
43: | public $Type; |
44: | |
45: | |
46: | |
47: | |
48: | public $IsEncrypt; |
49: | |
50: | |
51: | |
52: | |
53: | public $Encrypted; |
54: | |
55: | |
56: | |
57: | |
58: | public $ReadOnly; |
59: | |
60: | |
61: | |
62: | |
63: | |
64: | public $Override; |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | public $CanInherit; |
71: | |
72: | |
73: | |
74: | |
75: | |
76: | public $Inherited; |
77: | |
78: | |
79: | |
80: | |
81: | |
82: | public $IsDefault; |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | public $bExtended; |
89: | |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | public function __construct($sName, $mValue = null, $sType = 'string', $bIsEncrypt = false, $iEntityId = 0, $bReadOnly = false, $bExtended = false) |
99: | { |
100: | $this->Id = 0; |
101: | $this->EntityId = $iEntityId; |
102: | $this->Name = $sName; |
103: | $this->Value = $mValue; |
104: | $this->IsEncrypt = $bIsEncrypt; |
105: | $this->Encrypted = false; |
106: | $this->ReadOnly = $bReadOnly; |
107: | $this->Override = false; |
108: | $this->Inherited = false; |
109: | $this->CanInherit = false; |
110: | $this->IsDefault = false; |
111: | $this->bExtended = $bExtended; |
112: | |
113: | $this->setType($sType); |
114: | } |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | |
126: | public static function createInstance($sName, $sValue = null, $sType = null, $bEncrypt = false, $iEntityId = 0, $bReadOnly = false, $bExtended = false) |
127: | { |
128: | return new self($sName, $sValue, $sType, $bEncrypt, $iEntityId, $bReadOnly, $bExtended); |
129: | } |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | public function validate() |
137: | { |
138: | return true; |
139: | } |
140: | |
141: | |
142: | |
143: | |
144: | public function needToEscape() |
145: | { |
146: | $bResult = false; |
147: | if (!is_null($this->Value)) { |
148: | switch ($this->Type) { |
149: | case "mediumblob": |
150: | case "string": |
151: | $bResult = true; |
152: | break; |
153: | case "text": |
154: | $bResult = true; |
155: | break; |
156: | case "datetime": |
157: | $bResult = !empty($this->Value); |
158: | break; |
159: | } |
160: | } |
161: | |
162: | return $bResult; |
163: | } |
164: | |
165: | |
166: | |
167: | |
168: | public function setType($sType) |
169: | { |
170: | if ($sType === null) { |
171: | $sType = gettype($this->Value); |
172: | } |
173: | $this->Type = $sType; |
174: | |
175: | $sType = strtolower($sType); |
176: | if (in_array($sType, ['string', 'int', 'array', 'double', 'bool'])) { |
177: | settype($this->Value, $sType); |
178: | } elseif ($sType === 'bigint') { |
179: | settype($this->Value, 'int'); |
180: | } elseif (in_array($sType, ['encoded', 'datetime', 'mediumblob']) && !is_null($this->Value)) { |
181: | settype($this->Value, 'string'); |
182: | } elseif (0 === strpos($sType, 'string(')) { |
183: | settype($this->Value, 'string'); |
184: | if (0 < strlen($this->Value)) { |
185: | $iSize = substr($sType, 7, -1); |
186: | if (is_numeric($iSize) && (int) $iSize < strlen($this->Value)) { |
187: | $this->Value = \Aurora\System\Utils::Utf8Truncate($this->Value, (int) $iSize); |
188: | } |
189: | } |
190: | } |
191: | } |
192: | |
193: | |
194: | |
195: | |
196: | public function getValueFormat() |
197: | { |
198: | $sResult = '%s'; |
199: | switch ($this->Type) { |
200: | case "int": |
201: | $sResult = '%d'; |
202: | break; |
203: | case "bool": |
204: | $sResult = '%d'; |
205: | break; |
206: | } |
207: | |
208: | return $sResult; |
209: | } |
210: | |
211: | public function Encrypt() |
212: | { |
213: | if (!empty($this->Value) && !$this->Encrypted) { |
214: | $this->Value = \Aurora\System\Utils::EncryptValue($this->Value); |
215: | $this->Encrypted = true; |
216: | } |
217: | } |
218: | |
219: | public function Decrypt() |
220: | { |
221: | if ($this->Encrypted) { |
222: | $this->Value = \Aurora\System\Utils::DecryptValue($this->Value); |
223: | $this->Encrypted = false; |
224: | } |
225: | } |
226: | |
227: | public function save($oEntity) |
228: | { |
229: | return \Aurora\System\Managers\Eav::getInstance()->setAttribute($oEntity, $this); |
230: | } |
231: | } |
232: | |