1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\System\EAV;
9:
10: /**
11: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
12: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
13: * @copyright Copyright (c) 2019, Afterlogic Corp.
14: *
15: * @package EAV
16: * @subpackage Classes
17: */
18: class Attribute
19: {
20: /**
21: * @var int $Id
22: */
23: public $Id;
24:
25: /**
26: * @var int $EntityId
27: */
28: public $EntityId;
29:
30: /**
31: * @var string $Name
32: */
33: public $Name;
34:
35: /**
36: * @var mixed $Value
37: */
38: public $Value;
39:
40: /**
41: * @var string $Type
42: */
43: public $Type;
44:
45: /**
46: * @var bool $IsEncrypt
47: */
48: public $IsEncrypt;
49:
50: /**
51: * @var bool $Encrypted
52: */
53: public $Encrypted;
54:
55: /**
56: * @var bool $ReadOnly
57: */
58: public $ReadOnly;
59:
60: /**
61: *
62: * @var bool $Override
63: */
64: public $Override;
65:
66: /**
67: *
68: * @var bool $CanInherit
69: */
70: public $CanInherit;
71:
72: /**
73: *
74: * @var bool $Inherited
75: */
76: public $Inherited;
77:
78: /**
79: *
80: * @var bool $Inherited
81: */
82: public $IsDefault;
83:
84: /**
85: *
86: * @var bool $Inherited
87: */
88: public $bExtended;
89:
90: /**
91: * @param string $sName
92: * @param mixed $mValue
93: * @param string $sType
94: * @param bool $bIsEncrypt
95: * @param int $iEntityId
96: * @param bool $bReadOnly
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: * @param string $sName
118: * @param mixed $sValue
119: * @param string $sType
120: * @param bool $bEncrypt
121: * @param int $iEntityId
122: * @param bool $bReadOnly
123: *
124: * @return \Aurora\System\EAV\Attribute
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: * @throws \Aurora\System\Exceptions\ValidationException
133: *
134: * @return bool
135: */
136: public function validate()
137: {
138: return true;
139: }
140:
141: /**
142: * @return bool
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: * @param string $sType
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: * @return bool
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: