PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-cms-sample-data/Model/Block/Converter.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 323 lines | 193 code | 25 blank | 105 comment | 18 complexity | 6e7c8b1ca336aae16c24db84ee8fd658 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CmsSampleData\Model\Block;
  7. /**
  8. * Class Converter
  9. */
  10. class Converter
  11. {
  12. /**
  13. * @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
  14. */
  15. protected $categoryFactory;
  16. /**
  17. * @var \Magento\CatalogSampleData\Model\Product\Converter
  18. */
  19. protected $productConverter;
  20. /**
  21. * @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory
  22. */
  23. protected $attributeCollectionFactory;
  24. /**
  25. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory
  26. */
  27. protected $attrOptionCollectionFactory;
  28. /**
  29. * @var array
  30. */
  31. protected $attributeCodeOptionsPair;
  32. /**
  33. * @var array
  34. */
  35. protected $attributeCodeOptionValueIdsPair;
  36. /**
  37. * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
  38. */
  39. protected $productCollectionFactory;
  40. /**
  41. * @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryFactory
  42. * @param \Magento\Eav\Model\Config $eavConfig
  43. * @param \Magento\CatalogSampleData\Model\Product\Converter $productConverter
  44. * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory
  45. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
  46. * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
  47. */
  48. public function __construct(
  49. \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryFactory,
  50. \Magento\Eav\Model\Config $eavConfig,
  51. \Magento\CatalogSampleData\Model\Product\Converter $productConverter,
  52. \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeCollectionFactory,
  53. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
  54. \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
  55. ) {
  56. $this->categoryFactory = $categoryFactory;
  57. $this->eavConfig = $eavConfig;
  58. $this->productConverter = $productConverter;
  59. $this->attributeCollectionFactory = $attributeCollectionFactory;
  60. $this->attrOptionCollectionFactory = $attrOptionCollectionFactory;
  61. $this->productCollectionFactory = $productCollectionFactory;
  62. }
  63. /**
  64. * Convert CSV format row to array
  65. *
  66. * @param array $row
  67. * @return array
  68. */
  69. public function convertRow($row)
  70. {
  71. $data = [];
  72. foreach ($row as $field => $value) {
  73. if ('content' == $field) {
  74. $data['block'][$field] = $this->replaceMatches($value);
  75. continue;
  76. }
  77. $data['block'][$field] = $value;
  78. }
  79. return $data;
  80. }
  81. /**
  82. * @param string $urlKey
  83. * @return \Magento\Framework\Object
  84. */
  85. protected function getCategoryByUrlKey($urlKey)
  86. {
  87. $category = $this->categoryFactory->create()
  88. ->addAttributeToFilter('url_key', $urlKey)
  89. ->addUrlRewriteToResult()
  90. ->getFirstItem();
  91. return $category;
  92. }
  93. /**
  94. * Get formatted array value
  95. *
  96. * @param mixed $value
  97. * @param string $separator
  98. * @return array
  99. */
  100. protected function getArrayValue($value, $separator = "/")
  101. {
  102. if (is_array($value)) {
  103. return $value;
  104. }
  105. if (false !== strpos($value, $separator)) {
  106. $value = array_filter(explode($separator, $value));
  107. }
  108. return !is_array($value) ? [$value] : $value;
  109. }
  110. /**
  111. * @param string $content
  112. * @return mixed
  113. */
  114. protected function replaceMatches($content)
  115. {
  116. $matches = $this->getMatches($content);
  117. if (!empty($matches['value'])) {
  118. $replaces = $this->getReplaces($matches);
  119. $content = preg_replace($replaces['regexp'], $replaces['value'], $content);
  120. }
  121. return $content;
  122. }
  123. /**
  124. * @param string $content
  125. * @return array
  126. */
  127. protected function getMatches($content)
  128. {
  129. $regexp = '/{{(category[^ ]*) key="([^"]+)"}}/';
  130. preg_match_all($regexp, $content, $matchesCategory);
  131. $regexp = '/{{(product[^ ]*) sku="([^"]+)"}}/';
  132. preg_match_all($regexp, $content, $matchesProduct);
  133. $regexp = '/{{(attribute) key="([^"]*)"}}/';
  134. preg_match_all($regexp, $content, $matchesAttribute);
  135. return [
  136. 'type' => $matchesCategory[1] + $matchesAttribute[1] + $matchesProduct[1],
  137. 'value' => $matchesCategory[2] + $matchesAttribute[2] + $matchesProduct[2]
  138. ];
  139. }
  140. /**
  141. * @param array $matches
  142. * @return array
  143. */
  144. protected function getReplaces($matches)
  145. {
  146. $replaceData = [];
  147. foreach ($matches['value'] as $matchKey => $matchValue) {
  148. $callback = "matcher" . ucfirst(trim($matches['type'][$matchKey]));
  149. $matchResult = call_user_func_array([$this, $callback], [$matchValue]);
  150. if (!empty($matchResult)) {
  151. $replaceData = array_merge_recursive($replaceData, $matchResult);
  152. }
  153. }
  154. return $replaceData;
  155. }
  156. /**
  157. * @param string $urlAttributes
  158. * @return string
  159. */
  160. protected function getUrlFilter($urlAttributes)
  161. {
  162. $separatedAttributes = $this->getArrayValue($urlAttributes, ';');
  163. $urlFilter = null;
  164. foreach ($separatedAttributes as $attributeNumber => $attributeValue) {
  165. $attributeData = $this->getArrayValue($attributeValue, '=');
  166. $attributeOptions = $this->productConverter->getAttributeOptions($attributeData[0]);
  167. $attributeValue = $attributeOptions->getItemByColumnValue('value', $attributeData[1]);
  168. if ($attributeNumber == 0) {
  169. $urlFilter = $attributeData[0] . '=' . $attributeValue->getId();
  170. continue;
  171. }
  172. $urlFilter .= '&' . $attributeData[0] . '=' . $attributeValue->getId();
  173. }
  174. return $urlFilter;
  175. }
  176. /**
  177. * Get attribute options by attribute code
  178. *
  179. * @param string $attributeCode
  180. * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection|null
  181. */
  182. protected function getAttributeOptions($attributeCode)
  183. {
  184. if (!$this->attributeCodeOptionsPair || !isset($this->attributeCodeOptionsPair[$attributeCode])) {
  185. $this->loadAttributeOptions($attributeCode);
  186. }
  187. return isset($this->attributeCodeOptionsPair[$attributeCode])
  188. ? $this->attributeCodeOptionsPair[$attributeCode]
  189. : null;
  190. }
  191. /**
  192. * Loads all attributes with options for attribute
  193. *
  194. * @param string $attributeCode
  195. * @return $this
  196. */
  197. protected function loadAttributeOptions($attributeCode)
  198. {
  199. /** @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection $collection */
  200. $collection = $this->attributeCollectionFactory->create();
  201. $collection->addFieldToSelect(['attribute_code', 'attribute_id']);
  202. $collection->addFieldToFilter('attribute_code', $attributeCode);
  203. $collection->setFrontendInputTypeFilter(['in' => ['select', 'multiselect']]);
  204. foreach ($collection as $item) {
  205. $options = $this->attrOptionCollectionFactory->create()
  206. ->setAttributeFilter($item->getAttributeId())->setPositionOrder('asc', true)->load();
  207. $this->attributeCodeOptionsPair[$item->getAttributeCode()] = $options;
  208. }
  209. return $this;
  210. }
  211. /**
  212. * Find attribute option value pair
  213. *
  214. * @param string $attributeCode
  215. * @param string $value
  216. * @return mixed
  217. */
  218. protected function getAttributeOptionValueId($attributeCode, $value)
  219. {
  220. if (!empty($this->attributeCodeOptionValueIdsPair[$attributeCode][$value])) {
  221. return $this->attributeCodeOptionValueIdsPair[$attributeCode][$value];
  222. }
  223. $options = $this->getAttributeOptions($attributeCode);
  224. $opt = [];
  225. if ($options) {
  226. foreach ($options as $option) {
  227. $opt[$option->getValue()] = $option->getId();
  228. }
  229. }
  230. $this->attributeCodeOptionValueIdsPair[$attributeCode] = $opt;
  231. return $this->attributeCodeOptionValueIdsPair[$attributeCode][$value];
  232. }
  233. /**
  234. * @param string $matchValue
  235. * @return array
  236. */
  237. protected function matcherCategory($matchValue)
  238. {
  239. $replaceData = [];
  240. $category = $this->getCategoryByUrlKey($matchValue);
  241. if (!empty($category)) {
  242. $categoryUrl = $category->getRequestPath();
  243. $replaceData['regexp'][] = '/{{category key="' . $matchValue . '"}}/';
  244. $replaceData['value'][] = '{{store url=""}}' . $categoryUrl;
  245. }
  246. return $replaceData;
  247. }
  248. /**
  249. * @param string $matchValue
  250. * @return array
  251. */
  252. protected function matcherCategoryId($matchValue)
  253. {
  254. $replaceData = [];
  255. $category = $this->getCategoryByUrlKey($matchValue);
  256. if (!empty($category)) {
  257. $replaceData['regexp'][] = '/{{categoryId key="' . $matchValue . '"}}/';
  258. $replaceData['value'][] = sprintf('%03d', $category->getId());
  259. }
  260. return $replaceData;
  261. }
  262. /**
  263. * @param string $matchValue
  264. * @return array
  265. */
  266. protected function matcherProduct($matchValue)
  267. {
  268. $replaceData = [];
  269. $productCollection = $this->productCollectionFactory->create();
  270. $productItem = $productCollection->addAttributeToFilter('sku', $matchValue)
  271. ->addUrlRewrite()
  272. ->getFirstItem();
  273. $productUrl = null;
  274. if ($productItem) {
  275. $productUrl = '{{store url=""}}' . $productItem->getRequestPath();
  276. }
  277. $replaceData['regexp'][] = '/{{product sku="' . $matchValue . '"}}/';
  278. $replaceData['value'][] = $productUrl;
  279. return $replaceData;
  280. }
  281. /**
  282. * @param string $matchValue
  283. * @return array
  284. */
  285. protected function matcherAttribute($matchValue)
  286. {
  287. $replaceData = [];
  288. if (strpos($matchValue, ':') === false) {
  289. return $replaceData;
  290. }
  291. list($code, $value) = explode(':', $matchValue);
  292. if (!empty($code) && !empty($value)) {
  293. $replaceData['regexp'][] = '/{{attribute key="' . $matchValue . '"}}/';
  294. $replaceData['value'][] = sprintf('%03d', $this->getAttributeOptionValueId($code, $value));
  295. }
  296. return $replaceData;
  297. }
  298. }