PageRenderTime 33ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-bundle/Model/Option.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 297 lines | 137 code | 25 blank | 135 comment | 11 complexity | 78722305cc1f256cb7dae341cd17e419 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Model;
  7. /**
  8. * Bundle Option Model
  9. *
  10. * @method int getParentId()
  11. * @method null|\Magento\Catalog\Model\Product[] getSelections()
  12. * @method Option setParentId(int $value)
  13. */
  14. class Option extends \Magento\Framework\Model\AbstractExtensibleModel implements
  15. \Magento\Bundle\Api\Data\OptionInterface
  16. {
  17. /**#@+
  18. * Constants
  19. */
  20. const KEY_OPTION_ID = 'option_id';
  21. const KEY_TITLE = 'title';
  22. const KEY_REQUIRED = 'required';
  23. const KEY_TYPE = 'type';
  24. const KEY_POSITION = 'position';
  25. const KEY_SKU = 'sku';
  26. const KEY_PRODUCT_LINKS = 'product_links';
  27. /**#@-*/
  28. /**
  29. * Default selection object
  30. *
  31. * @var \Magento\Catalog\Model\Product|null
  32. */
  33. protected $defaultSelection = null;
  34. /**
  35. * Initialize resource model
  36. *
  37. * @return void
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('Magento\Bundle\Model\ResourceModel\Option');
  42. parent::_construct();
  43. }
  44. /**
  45. * Add selection to option
  46. *
  47. * @param \Magento\Catalog\Model\Product $selection
  48. * @return void
  49. */
  50. public function addSelection(\Magento\Catalog\Model\Product $selection)
  51. {
  52. if (!$this->hasData('selections')) {
  53. $this->setData('selections', []);
  54. }
  55. $selections = $this->getData('selections');
  56. $selections[] = $selection;
  57. $this->setSelections($selections);
  58. }
  59. /**
  60. * Check Is Saleable Option
  61. *
  62. * @return bool
  63. */
  64. public function isSaleable()
  65. {
  66. $saleable = false;
  67. $selections = $this->getSelections();
  68. if ($selections) {
  69. foreach ($selections as $selection) {
  70. if ($selection->isSaleable()) {
  71. $saleable = true;
  72. break;
  73. }
  74. }
  75. }
  76. return $saleable;
  77. }
  78. /**
  79. * Retrieve default Selection object
  80. *
  81. * @return \Magento\Catalog\Model\Product|null
  82. */
  83. public function getDefaultSelection()
  84. {
  85. if (!$this->defaultSelection && $this->getSelections()) {
  86. foreach ($this->getSelections() as $selection) {
  87. if ($selection->getIsDefault()) {
  88. $this->defaultSelection = $selection;
  89. break;
  90. }
  91. }
  92. }
  93. return $this->defaultSelection;
  94. }
  95. /**
  96. * Check is multi Option selection
  97. *
  98. * @return bool
  99. */
  100. public function isMultiSelection()
  101. {
  102. return $this->getType() == 'checkbox' || $this->getType() == 'multi';
  103. }
  104. /**
  105. * Retrieve options searchable data
  106. *
  107. * @param int $productId
  108. * @param int $storeId
  109. * @return array
  110. */
  111. public function getSearchableData($productId, $storeId)
  112. {
  113. return $this->_getResource()->getSearchableData($productId, $storeId);
  114. }
  115. /**
  116. * Return selection by it's id
  117. *
  118. * @param int $selectionId
  119. * @return \Magento\Catalog\Model\Product|null
  120. */
  121. public function getSelectionById($selectionId)
  122. {
  123. $foundSelection = null;
  124. foreach ($this->getSelections() as $selection) {
  125. if ($selection->getSelectionId() == $selectionId) {
  126. $foundSelection = $selection;
  127. break;
  128. }
  129. }
  130. return $foundSelection;
  131. }
  132. //@codeCoverageIgnoreStart
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function getOptionId()
  137. {
  138. return $this->getData(self::KEY_OPTION_ID);
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function getTitle()
  144. {
  145. return $this->getData(self::KEY_TITLE);
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function getRequired()
  151. {
  152. return $this->getData(self::KEY_REQUIRED);
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function getType()
  158. {
  159. return $this->getData(self::KEY_TYPE);
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function getPosition()
  165. {
  166. return $this->getData(self::KEY_POSITION);
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function getSku()
  172. {
  173. return $this->getData(self::KEY_SKU);
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function getProductLinks()
  179. {
  180. return $this->getData(self::KEY_PRODUCT_LINKS);
  181. }
  182. /**
  183. * Set option id
  184. *
  185. * @param int $optionId
  186. * @return $this
  187. */
  188. public function setOptionId($optionId)
  189. {
  190. return $this->setData(self::KEY_OPTION_ID, $optionId);
  191. }
  192. /**
  193. * Set option title
  194. *
  195. * @param string $title
  196. * @return $this
  197. */
  198. public function setTitle($title)
  199. {
  200. return $this->setData(self::KEY_TITLE, $title);
  201. }
  202. /**
  203. * Set whether option is required
  204. *
  205. * @param bool $required
  206. * @return $this
  207. */
  208. public function setRequired($required)
  209. {
  210. return $this->setData(self::KEY_REQUIRED, $required);
  211. }
  212. /**
  213. * Set input type
  214. *
  215. * @param string $type
  216. * @return $this
  217. */
  218. public function setType($type)
  219. {
  220. return $this->setData(self::KEY_TYPE, $type);
  221. }
  222. /**
  223. * Set option position
  224. *
  225. * @param int $position
  226. * @return $this
  227. */
  228. public function setPosition($position)
  229. {
  230. return $this->setData(self::KEY_POSITION, $position);
  231. }
  232. /**
  233. * Set product sku
  234. *
  235. * @param string $sku
  236. * @return $this
  237. */
  238. public function setSku($sku)
  239. {
  240. return $this->setData(self::KEY_SKU, $sku);
  241. }
  242. /**
  243. * Set product links
  244. *
  245. * @param \Magento\Bundle\Api\Data\LinkInterface[] $productLinks
  246. * @return $this
  247. */
  248. public function setProductLinks(array $productLinks = null)
  249. {
  250. return $this->setData(self::KEY_PRODUCT_LINKS, $productLinks);
  251. }
  252. /**
  253. * {@inheritdoc}
  254. *
  255. * @return \Magento\Bundle\Api\Data\OptionExtensionInterface|null
  256. */
  257. public function getExtensionAttributes()
  258. {
  259. return $this->_getExtensionAttributes();
  260. }
  261. /**
  262. * {@inheritdoc}
  263. *
  264. * @param \Magento\Bundle\Api\Data\OptionExtensionInterface $extensionAttributes
  265. * @return $this
  266. */
  267. public function setExtensionAttributes(\Magento\Bundle\Api\Data\OptionExtensionInterface $extensionAttributes)
  268. {
  269. return $this->_setExtensionAttributes($extensionAttributes);
  270. }
  271. //@codeCoverageIgnoreEnd
  272. }