PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/Magento/Directory/Model/ResourceModel/Country/Collection.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 292 lines | 157 code | 22 blank | 113 comment | 22 complexity | 23ad03df8b79a73f9db409ba0b956955 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. /**
  8. * Directory Country Resource Collection
  9. */
  10. namespace Magento\Directory\Model\ResourceModel\Country;
  11. /**
  12. * Class Collection
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  16. {
  17. /**
  18. * Locale model
  19. *
  20. * @var \Magento\Framework\Locale\ListsInterface
  21. */
  22. protected $_localeLists;
  23. /**
  24. * Core store config
  25. *
  26. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  27. */
  28. protected $_scopeConfig;
  29. /**
  30. * @var \Magento\Directory\Model\ResourceModel\CountryFactory
  31. */
  32. protected $_countryFactory;
  33. /**
  34. * Array utils object
  35. *
  36. * @var \Magento\Framework\Stdlib\ArrayUtils
  37. */
  38. protected $_arrayUtils;
  39. /**
  40. * @var \Magento\Framework\Locale\ResolverInterface
  41. */
  42. protected $_localeResolver;
  43. /**
  44. * @var \Magento\Directory\Helper\Data
  45. */
  46. protected $helperData;
  47. /**
  48. * @var string[]
  49. */
  50. protected $countriesWithNotRequiredStates;
  51. /**
  52. * Initialize dependencies.
  53. *
  54. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  55. * @param \Psr\Log\LoggerInterface $logger
  56. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  57. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  58. * @param \Magento\Framework\Locale\ListsInterface $localeLists
  59. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  60. * @param \Magento\Directory\Model\ResourceModel\CountryFactory $countryFactory
  61. * @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
  62. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  63. * @param \Magento\Framework\App\Helper\AbstractHelper $helperData
  64. * @param array $countriesWithNotRequiredStates
  65. * @param mixed $connection
  66. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  67. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  68. */
  69. public function __construct(
  70. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  71. \Psr\Log\LoggerInterface $logger,
  72. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  73. \Magento\Framework\Event\ManagerInterface $eventManager,
  74. \Magento\Framework\Locale\ListsInterface $localeLists,
  75. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  76. \Magento\Directory\Model\ResourceModel\CountryFactory $countryFactory,
  77. \Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
  78. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  79. \Magento\Framework\App\Helper\AbstractHelper $helperData,
  80. array $countriesWithNotRequiredStates = [],
  81. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  82. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  83. ) {
  84. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  85. $this->_scopeConfig = $scopeConfig;
  86. $this->_localeLists = $localeLists;
  87. $this->_localeResolver = $localeResolver;
  88. $this->_countryFactory = $countryFactory;
  89. $this->_arrayUtils = $arrayUtils;
  90. $this->helperData = $helperData;
  91. $this->countriesWithNotRequiredStates = $countriesWithNotRequiredStates;
  92. }
  93. /**
  94. * Foreground countries
  95. *
  96. * @var array
  97. */
  98. protected $_foregroundCountries = [];
  99. /**
  100. * Define main table
  101. *
  102. * @return void
  103. */
  104. protected function _construct()
  105. {
  106. $this->_init(\Magento\Directory\Model\Country::class, \Magento\Directory\Model\ResourceModel\Country::class);
  107. }
  108. /**
  109. * Load allowed countries for current store
  110. *
  111. * @param null|int|string|\Magento\Store\Model\Store $store
  112. * @return \Magento\Directory\Model\ResourceModel\Country\Collection
  113. */
  114. public function loadByStore($store = null)
  115. {
  116. $allowCountries = explode(',',
  117. (string)$this->_scopeConfig->getValue(
  118. 'general/country/allow',
  119. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  120. $store
  121. )
  122. );
  123. if (!empty($allowCountries)) {
  124. $this->addFieldToFilter("country_id", ['in' => $allowCountries]);
  125. }
  126. return $this;
  127. }
  128. /**
  129. * Loads Item By Id
  130. *
  131. * @param string $countryId
  132. * @return \Magento\Directory\Model\ResourceModel\Country|null
  133. */
  134. public function getItemById($countryId)
  135. {
  136. foreach ($this->_items as $country) {
  137. if ($country->getCountryId() == $countryId) {
  138. return $country;
  139. }
  140. }
  141. return null;
  142. }
  143. /**
  144. * Add filter by country code to collection.
  145. * $countryCode can be either array of country codes or string representing one country code.
  146. * $iso can be either array containing 'iso2', 'iso3' values or string with containing one of that values directly.
  147. * The collection will contain countries where at least one of contry $iso fields matches $countryCode.
  148. *
  149. * @param string|string[] $countryCode
  150. * @param string|string[] $iso
  151. * @return $this
  152. */
  153. public function addCountryCodeFilter($countryCode, $iso = ['iso3', 'iso2'])
  154. {
  155. if (!empty($countryCode)) {
  156. if (is_array($countryCode)) {
  157. if (is_array($iso)) {
  158. $whereOr = [];
  159. foreach ($iso as $iso_curr) {
  160. $whereOr[] .= $this->_getConditionSql("{$iso_curr}_code", ['in' => $countryCode]);
  161. }
  162. $this->_select->where('(' . implode(') OR (', $whereOr) . ')');
  163. } else {
  164. $this->addFieldToFilter("{$iso}_code", ['in' => $countryCode]);
  165. }
  166. } else {
  167. if (is_array($iso)) {
  168. $whereOr = [];
  169. foreach ($iso as $iso_curr) {
  170. $whereOr[] .= $this->_getConditionSql("{$iso_curr}_code", $countryCode);
  171. }
  172. $this->_select->where('(' . implode(') OR (', $whereOr) . ')');
  173. } else {
  174. $this->addFieldToFilter("{$iso}_code", $countryCode);
  175. }
  176. }
  177. }
  178. return $this;
  179. }
  180. /**
  181. * Add filter by country code(s) to collection
  182. *
  183. * @param string|string[] $countryId
  184. * @return $this
  185. */
  186. public function addCountryIdFilter($countryId)
  187. {
  188. if (!empty($countryId)) {
  189. if (is_array($countryId)) {
  190. $this->addFieldToFilter("country_id", ['in' => $countryId]);
  191. } else {
  192. $this->addFieldToFilter("country_id", $countryId);
  193. }
  194. }
  195. return $this;
  196. }
  197. /**
  198. * Convert collection items to select options array
  199. *
  200. * @param string|boolean $emptyLabel
  201. * @return array
  202. */
  203. public function toOptionArray($emptyLabel = ' ')
  204. {
  205. $options = $this->_toOptionArray('country_id', 'name', ['title' => 'iso2_code']);
  206. $sort = [];
  207. foreach ($options as $data) {
  208. $name = (string)$this->_localeLists->getCountryTranslation($data['value']);
  209. if (!empty($name)) {
  210. $sort[$name] = $data['value'];
  211. }
  212. }
  213. $this->_arrayUtils->ksortMultibyte($sort, $this->_localeResolver->getLocale());
  214. foreach (array_reverse($this->_foregroundCountries) as $foregroundCountry) {
  215. $name = array_search($foregroundCountry, $sort);
  216. unset($sort[$name]);
  217. $sort = [$name => $foregroundCountry] + $sort;
  218. }
  219. $isRegionVisible = (bool)$this->helperData->isShowNonRequiredState();
  220. $options = [];
  221. foreach ($sort as $label => $value) {
  222. $option = ['value' => $value, 'label' => $label];
  223. if ($this->helperData->isRegionRequired($value)) {
  224. $option['is_region_required'] = true;
  225. } else {
  226. $option['is_region_visible'] = $isRegionVisible;
  227. }
  228. if ($this->helperData->isZipCodeOptional($value)) {
  229. $option['is_zipcode_optional'] = true;
  230. }
  231. $options[] = $option;
  232. }
  233. if (count($options) > 0 && $emptyLabel !== false) {
  234. array_unshift($options, ['value' => '', 'label' => $emptyLabel]);
  235. }
  236. return $options;
  237. }
  238. /**
  239. * Set foreground countries array
  240. *
  241. * @param string|array $foregroundCountries
  242. * @return $this
  243. */
  244. public function setForegroundCountries($foregroundCountries)
  245. {
  246. if (empty($foregroundCountries)) {
  247. return $this;
  248. }
  249. $this->_foregroundCountries = (array)$foregroundCountries;
  250. return $this;
  251. }
  252. /**
  253. * Get list of countries with required states
  254. *
  255. * @return \Magento\Directory\Model\Country[]
  256. */
  257. public function getCountriesWithRequiredStates()
  258. {
  259. $countries = [];
  260. foreach ($this->getItems() as $country) {
  261. /** @var \Magento\Directory\Model\Country $country */
  262. if ($country->getRegionCollection()->getSize() > 0
  263. && !in_array($country->getId(), $this->countriesWithNotRequiredStates)
  264. ) {
  265. $countries[$country->getId()] = $country;
  266. }
  267. }
  268. return $countries;
  269. }
  270. }