AbstractBackend.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Eav\Model\Entity\Attribute\Backend;
  8. use Magento\Framework\Exception\LocalizedException;
  9. /**
  10. * Entity/Attribute/Model - attribute backend abstract
  11. *
  12. * @api
  13. * @SuppressWarnings(PHPMD.NumberOfChildren)
  14. * @since 100.0.2
  15. */
  16. abstract class AbstractBackend implements \Magento\Eav\Model\Entity\Attribute\Backend\BackendInterface
  17. {
  18. /**
  19. * Reference to the attribute instance
  20. *
  21. * @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute
  22. */
  23. protected $_attribute;
  24. /**
  25. * PK value_id for loaded entity (for faster updates)
  26. *
  27. * @var integer
  28. */
  29. protected $_valueId;
  30. /**
  31. * PK value_ids for each loaded entity
  32. *
  33. * @var array
  34. */
  35. protected $_valueIds = [];
  36. /**
  37. * Table name for this attribute
  38. *
  39. * @var string
  40. */
  41. protected $_table;
  42. /**
  43. * Name of the entity_id field for the value table of this attribute
  44. *
  45. * @var string
  46. */
  47. protected $_entityIdField;
  48. /**
  49. * Default value for the attribute
  50. *
  51. * @var mixed
  52. */
  53. protected $_defaultValue = null;
  54. /**
  55. * Set attribute instance
  56. *
  57. * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
  58. * @return $this
  59. * @codeCoverageIgnore
  60. */
  61. public function setAttribute($attribute)
  62. {
  63. $this->_attribute = $attribute;
  64. return $this;
  65. }
  66. /**
  67. * Get attribute instance
  68. *
  69. * @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute
  70. * @codeCoverageIgnore
  71. */
  72. public function getAttribute()
  73. {
  74. return $this->_attribute;
  75. }
  76. /**
  77. * Get backend type of the attribute
  78. *
  79. * @return string
  80. * @codeCoverageIgnore
  81. */
  82. public function getType()
  83. {
  84. return $this->getAttribute()->getBackendType();
  85. }
  86. /**
  87. * Check whether the attribute is a real field in the entity table
  88. *
  89. * @return bool
  90. * @codeCoverageIgnore
  91. */
  92. public function isStatic()
  93. {
  94. return $this->getAttribute()->isStatic();
  95. }
  96. /**
  97. * Get table name for the values of the attribute
  98. *
  99. * @return string
  100. */
  101. public function getTable()
  102. {
  103. if (empty($this->_table)) {
  104. if ($this->isStatic()) {
  105. $this->_table = $this->getAttribute()->getEntityType()->getValueTablePrefix();
  106. } elseif ($this->getAttribute()->getBackendTable()) {
  107. $this->_table = $this->getAttribute()->getBackendTable();
  108. } else {
  109. $entity = $this->getAttribute()->getEntity();
  110. $tableName = sprintf('%s_%s', $entity->getValueTablePrefix(), $this->getType());
  111. $this->_table = $tableName;
  112. }
  113. }
  114. return $this->_table;
  115. }
  116. /**
  117. * Get entity_id field in the attribute values tables
  118. *
  119. * @return string
  120. */
  121. public function getEntityIdField()
  122. {
  123. if (empty($this->_entityIdField)) {
  124. if ($this->getAttribute()->getEntityIdField()) {
  125. $this->_entityIdField = $this->getAttribute()->getEntityIdField();
  126. } else {
  127. $this->_entityIdField = $this->getAttribute()->getEntityType()->getValueEntityIdField();
  128. }
  129. }
  130. return $this->_entityIdField;
  131. }
  132. /**
  133. * Set value id
  134. *
  135. * @param int $valueId
  136. * @return $this
  137. * @codeCoverageIgnore
  138. */
  139. public function setValueId($valueId)
  140. {
  141. $this->_valueId = $valueId;
  142. return $this;
  143. }
  144. /**
  145. * Set entity value id
  146. *
  147. * @param \Magento\Framework\DataObject $entity
  148. * @param int $valueId
  149. * @return $this
  150. */
  151. public function setEntityValueId($entity, $valueId)
  152. {
  153. if (!$entity || !$entity->getId()) {
  154. return $this->setValueId($valueId);
  155. }
  156. $this->_valueIds[$entity->getId()] = $valueId;
  157. return $this;
  158. }
  159. /**
  160. * Retrieve value id
  161. *
  162. * @return int
  163. * @codeCoverageIgnore
  164. */
  165. public function getValueId()
  166. {
  167. return $this->_valueId;
  168. }
  169. /**
  170. * Get entity value id
  171. *
  172. * @param \Magento\Framework\DataObject $entity
  173. * @return int
  174. */
  175. public function getEntityValueId($entity)
  176. {
  177. if (!$entity || !$entity->getId() || !array_key_exists($entity->getId(), $this->_valueIds)) {
  178. return $this->getValueId();
  179. }
  180. return $this->_valueIds[$entity->getId()];
  181. }
  182. /**
  183. * Retrieve default value
  184. *
  185. * @return string
  186. */
  187. public function getDefaultValue()
  188. {
  189. if ($this->_defaultValue === null) {
  190. if ($this->getAttribute()->getDefaultValue() !== null) {
  191. $this->_defaultValue = $this->getAttribute()->getDefaultValue();
  192. } else {
  193. $this->_defaultValue = "";
  194. }
  195. }
  196. return $this->_defaultValue;
  197. }
  198. /**
  199. * Validate object
  200. *
  201. * @param \Magento\Framework\DataObject $object
  202. * @return bool
  203. * @throws LocalizedException
  204. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  205. */
  206. public function validate($object)
  207. {
  208. $attribute = $this->getAttribute();
  209. $attrCode = $attribute->getAttributeCode();
  210. $value = $object->getData($attrCode);
  211. if ($attribute->getIsVisible()
  212. && $attribute->getIsRequired()
  213. && $attribute->isValueEmpty($value)
  214. && $attribute->isValueEmpty($attribute->getDefaultValue())
  215. ) {
  216. $label = $attribute->getFrontend()->getLabel();
  217. throw new LocalizedException(
  218. __('The "%1" attribute value is empty. Set the attribute and try again.', $label)
  219. );
  220. }
  221. if ($attribute->getIsUnique()
  222. && !$attribute->getIsRequired()
  223. && ($value == '' || $attribute->isValueEmpty($value))
  224. ) {
  225. return true;
  226. }
  227. if ($attribute->getIsUnique()) {
  228. if (!$attribute->getEntity()->checkAttributeUniqueValue($attribute, $object)) {
  229. $label = $attribute->getFrontend()->getLabel();
  230. throw new LocalizedException(
  231. __('The value of the "%1" attribute isn\'t unique. Set a unique value and try again.', $label)
  232. );
  233. }
  234. }
  235. return true;
  236. }
  237. /**
  238. * After load method
  239. *
  240. * @param \Magento\Framework\DataObject $object
  241. * @return $this
  242. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  243. * @codeCoverageIgnore
  244. */
  245. public function afterLoad($object)
  246. {
  247. return $this;
  248. }
  249. /**
  250. * Before save method
  251. *
  252. * @param \Magento\Framework\DataObject $object
  253. * @return $this
  254. */
  255. public function beforeSave($object)
  256. {
  257. $attrCode = $this->getAttribute()->getAttributeCode();
  258. if (!$object->hasData($attrCode) && $this->getDefaultValue() !== '') {
  259. $object->setData($attrCode, $this->getDefaultValue());
  260. }
  261. return $this;
  262. }
  263. /**
  264. * After save method
  265. *
  266. * @param \Magento\Framework\DataObject $object
  267. * @return $this
  268. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  269. * @codeCoverageIgnore
  270. */
  271. public function afterSave($object)
  272. {
  273. return $this;
  274. }
  275. /**
  276. * Before delete method
  277. *
  278. * @param \Magento\Framework\DataObject $object
  279. * @return $this
  280. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  281. * @codeCoverageIgnore
  282. */
  283. public function beforeDelete($object)
  284. {
  285. return $this;
  286. }
  287. /**
  288. * After delete method
  289. *
  290. * @param \Magento\Framework\DataObject $object
  291. * @return $this
  292. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  293. * @codeCoverageIgnore
  294. */
  295. public function afterDelete($object)
  296. {
  297. return $this;
  298. }
  299. /**
  300. * Retrieve data for update attribute
  301. *
  302. * @param \Magento\Framework\DataObject $object
  303. * @return array
  304. */
  305. public function getAffectedFields($object)
  306. {
  307. $data = [];
  308. $data[$this->getTable()][] = [
  309. 'attribute_id' => $this->getAttribute()->getAttributeId(),
  310. 'value_id' => $this->getEntityValueId($object),
  311. ];
  312. return $data;
  313. }
  314. /**
  315. * By default attribute value is considered scalar that can be stored in a generic way {@inheritdoc}
  316. *
  317. * @codeCoverageIgnore
  318. */
  319. public function isScalar()
  320. {
  321. return true;
  322. }
  323. }