/app/code/Magento/Directory/Model/ResourceModel/Country/Collection.php
PHP | 292 lines | 157 code | 22 blank | 113 comment | 22 complexity | 23ad03df8b79a73f9db409ba0b956955 MD5 | raw file
- <?php
- /**
- * Copyright © 2016 Magento. All rights reserved.
- * See COPYING.txt for license details.
- */
- // @codingStandardsIgnoreFile
- /**
- * Directory Country Resource Collection
- */
- namespace Magento\Directory\Model\ResourceModel\Country;
- /**
- * Class Collection
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
- {
- /**
- * Locale model
- *
- * @var \Magento\Framework\Locale\ListsInterface
- */
- protected $_localeLists;
- /**
- * Core store config
- *
- * @var \Magento\Framework\App\Config\ScopeConfigInterface
- */
- protected $_scopeConfig;
- /**
- * @var \Magento\Directory\Model\ResourceModel\CountryFactory
- */
- protected $_countryFactory;
- /**
- * Array utils object
- *
- * @var \Magento\Framework\Stdlib\ArrayUtils
- */
- protected $_arrayUtils;
- /**
- * @var \Magento\Framework\Locale\ResolverInterface
- */
- protected $_localeResolver;
- /**
- * @var \Magento\Directory\Helper\Data
- */
- protected $helperData;
- /**
- * @var string[]
- */
- protected $countriesWithNotRequiredStates;
- /**
- * Initialize dependencies.
- *
- * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
- * @param \Psr\Log\LoggerInterface $logger
- * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
- * @param \Magento\Framework\Event\ManagerInterface $eventManager
- * @param \Magento\Framework\Locale\ListsInterface $localeLists
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
- * @param \Magento\Directory\Model\ResourceModel\CountryFactory $countryFactory
- * @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
- * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
- * @param \Magento\Framework\App\Helper\AbstractHelper $helperData
- * @param array $countriesWithNotRequiredStates
- * @param mixed $connection
- * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
- \Psr\Log\LoggerInterface $logger,
- \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
- \Magento\Framework\Event\ManagerInterface $eventManager,
- \Magento\Framework\Locale\ListsInterface $localeLists,
- \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
- \Magento\Directory\Model\ResourceModel\CountryFactory $countryFactory,
- \Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
- \Magento\Framework\Locale\ResolverInterface $localeResolver,
- \Magento\Framework\App\Helper\AbstractHelper $helperData,
- array $countriesWithNotRequiredStates = [],
- \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
- \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
- ) {
- parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
- $this->_scopeConfig = $scopeConfig;
- $this->_localeLists = $localeLists;
- $this->_localeResolver = $localeResolver;
- $this->_countryFactory = $countryFactory;
- $this->_arrayUtils = $arrayUtils;
- $this->helperData = $helperData;
- $this->countriesWithNotRequiredStates = $countriesWithNotRequiredStates;
- }
- /**
- * Foreground countries
- *
- * @var array
- */
- protected $_foregroundCountries = [];
- /**
- * Define main table
- *
- * @return void
- */
- protected function _construct()
- {
- $this->_init(\Magento\Directory\Model\Country::class, \Magento\Directory\Model\ResourceModel\Country::class);
- }
- /**
- * Load allowed countries for current store
- *
- * @param null|int|string|\Magento\Store\Model\Store $store
- * @return \Magento\Directory\Model\ResourceModel\Country\Collection
- */
- public function loadByStore($store = null)
- {
- $allowCountries = explode(',',
- (string)$this->_scopeConfig->getValue(
- 'general/country/allow',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
- $store
- )
- );
- if (!empty($allowCountries)) {
- $this->addFieldToFilter("country_id", ['in' => $allowCountries]);
- }
- return $this;
- }
- /**
- * Loads Item By Id
- *
- * @param string $countryId
- * @return \Magento\Directory\Model\ResourceModel\Country|null
- */
- public function getItemById($countryId)
- {
- foreach ($this->_items as $country) {
- if ($country->getCountryId() == $countryId) {
- return $country;
- }
- }
- return null;
- }
- /**
- * Add filter by country code to collection.
- * $countryCode can be either array of country codes or string representing one country code.
- * $iso can be either array containing 'iso2', 'iso3' values or string with containing one of that values directly.
- * The collection will contain countries where at least one of contry $iso fields matches $countryCode.
- *
- * @param string|string[] $countryCode
- * @param string|string[] $iso
- * @return $this
- */
- public function addCountryCodeFilter($countryCode, $iso = ['iso3', 'iso2'])
- {
- if (!empty($countryCode)) {
- if (is_array($countryCode)) {
- if (is_array($iso)) {
- $whereOr = [];
- foreach ($iso as $iso_curr) {
- $whereOr[] .= $this->_getConditionSql("{$iso_curr}_code", ['in' => $countryCode]);
- }
- $this->_select->where('(' . implode(') OR (', $whereOr) . ')');
- } else {
- $this->addFieldToFilter("{$iso}_code", ['in' => $countryCode]);
- }
- } else {
- if (is_array($iso)) {
- $whereOr = [];
- foreach ($iso as $iso_curr) {
- $whereOr[] .= $this->_getConditionSql("{$iso_curr}_code", $countryCode);
- }
- $this->_select->where('(' . implode(') OR (', $whereOr) . ')');
- } else {
- $this->addFieldToFilter("{$iso}_code", $countryCode);
- }
- }
- }
- return $this;
- }
- /**
- * Add filter by country code(s) to collection
- *
- * @param string|string[] $countryId
- * @return $this
- */
- public function addCountryIdFilter($countryId)
- {
- if (!empty($countryId)) {
- if (is_array($countryId)) {
- $this->addFieldToFilter("country_id", ['in' => $countryId]);
- } else {
- $this->addFieldToFilter("country_id", $countryId);
- }
- }
- return $this;
- }
- /**
- * Convert collection items to select options array
- *
- * @param string|boolean $emptyLabel
- * @return array
- */
- public function toOptionArray($emptyLabel = ' ')
- {
- $options = $this->_toOptionArray('country_id', 'name', ['title' => 'iso2_code']);
- $sort = [];
- foreach ($options as $data) {
- $name = (string)$this->_localeLists->getCountryTranslation($data['value']);
- if (!empty($name)) {
- $sort[$name] = $data['value'];
- }
- }
- $this->_arrayUtils->ksortMultibyte($sort, $this->_localeResolver->getLocale());
- foreach (array_reverse($this->_foregroundCountries) as $foregroundCountry) {
- $name = array_search($foregroundCountry, $sort);
- unset($sort[$name]);
- $sort = [$name => $foregroundCountry] + $sort;
- }
- $isRegionVisible = (bool)$this->helperData->isShowNonRequiredState();
- $options = [];
- foreach ($sort as $label => $value) {
- $option = ['value' => $value, 'label' => $label];
- if ($this->helperData->isRegionRequired($value)) {
- $option['is_region_required'] = true;
- } else {
- $option['is_region_visible'] = $isRegionVisible;
- }
- if ($this->helperData->isZipCodeOptional($value)) {
- $option['is_zipcode_optional'] = true;
- }
- $options[] = $option;
- }
- if (count($options) > 0 && $emptyLabel !== false) {
- array_unshift($options, ['value' => '', 'label' => $emptyLabel]);
- }
- return $options;
- }
- /**
- * Set foreground countries array
- *
- * @param string|array $foregroundCountries
- * @return $this
- */
- public function setForegroundCountries($foregroundCountries)
- {
- if (empty($foregroundCountries)) {
- return $this;
- }
- $this->_foregroundCountries = (array)$foregroundCountries;
- return $this;
- }
- /**
- * Get list of countries with required states
- *
- * @return \Magento\Directory\Model\Country[]
- */
- public function getCountriesWithRequiredStates()
- {
- $countries = [];
- foreach ($this->getItems() as $country) {
- /** @var \Magento\Directory\Model\Country $country */
- if ($country->getRegionCollection()->getSize() > 0
- && !in_array($country->getId(), $this->countriesWithNotRequiredStates)
- ) {
- $countries[$country->getId()] = $country;
- }
- }
- return $countries;
- }
- }