Country.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Model;
  7. /**
  8. * Country model
  9. *
  10. * @method string getCountryId()
  11. * @method \Magento\Directory\Model\Country setCountryId(string $value)
  12. *
  13. * @api
  14. * @since 100.0.2
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class Country extends \Magento\Framework\Model\AbstractModel
  18. {
  19. /**
  20. * @var array
  21. */
  22. public static $_format = [];
  23. /**
  24. * @var \Magento\Framework\Locale\ListsInterface
  25. */
  26. protected $_localeLists;
  27. /**
  28. * @var \Magento\Directory\Model\Country\FormatFactory
  29. */
  30. protected $_formatFactory;
  31. /**
  32. * @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
  33. */
  34. protected $_regionCollectionFactory;
  35. /**
  36. * @param \Magento\Framework\Model\Context $context
  37. * @param \Magento\Framework\Registry $registry
  38. * @param \Magento\Framework\Locale\ListsInterface $localeLists
  39. * @param Country\FormatFactory $formatFactory
  40. * @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
  41. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  42. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  43. * @param array $data
  44. */
  45. public function __construct(
  46. \Magento\Framework\Model\Context $context,
  47. \Magento\Framework\Registry $registry,
  48. \Magento\Framework\Locale\ListsInterface $localeLists,
  49. \Magento\Directory\Model\Country\FormatFactory $formatFactory,
  50. \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory,
  51. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  52. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  53. array $data = []
  54. ) {
  55. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  56. $this->_localeLists = $localeLists;
  57. $this->_formatFactory = $formatFactory;
  58. $this->_regionCollectionFactory = $regionCollectionFactory;
  59. }
  60. /**
  61. * @return void
  62. */
  63. protected function _construct()
  64. {
  65. $this->_init(\Magento\Directory\Model\ResourceModel\Country::class);
  66. }
  67. /**
  68. * Load country by code
  69. *
  70. * @param string $code
  71. * @return $this
  72. */
  73. public function loadByCode($code)
  74. {
  75. $this->_getResource()->loadByCode($this, $code);
  76. return $this;
  77. }
  78. /**
  79. * Get regions
  80. *
  81. * @return \Magento\Directory\Model\ResourceModel\Region\Collection
  82. */
  83. public function getRegions()
  84. {
  85. return $this->getLoadedRegionCollection();
  86. }
  87. /**
  88. * @return \Magento\Directory\Model\ResourceModel\Region\Collection
  89. */
  90. public function getLoadedRegionCollection()
  91. {
  92. $collection = $this->getRegionCollection();
  93. $collection->load();
  94. return $collection;
  95. }
  96. /**
  97. * @return \Magento\Directory\Model\ResourceModel\Region\Collection
  98. */
  99. public function getRegionCollection()
  100. {
  101. $collection = $this->_regionCollectionFactory->create();
  102. $collection->addCountryFilter($this->getId());
  103. return $collection;
  104. }
  105. /**
  106. * @param \Magento\Framework\DataObject $address
  107. * @param bool $html
  108. * @return string
  109. */
  110. public function formatAddress(\Magento\Framework\DataObject $address, $html = false)
  111. {
  112. //TODO: is it still used?
  113. $address->getRegion();
  114. $address->getCountry();
  115. $template = $this->getData('address_template_' . ($html ? 'html' : 'plain'));
  116. if (empty($template)) {
  117. if (!$this->getId()) {
  118. $template = '{{firstname}} {{lastname}}';
  119. } elseif (!$html) {
  120. $template = "{{firstname}} {{lastname}}
  121. {{company}}
  122. {{street1}}
  123. {{street2}}
  124. {{city}}, {{region}} {{postcode}}";
  125. } else {
  126. $template = "{{firstname}} {{lastname}}<br/>
  127. {{street}}<br/>
  128. {{city}}, {{region}} {{postcode}}<br/>
  129. T: {{telephone}}";
  130. }
  131. }
  132. $filter = new \Magento\Framework\Filter\Template\Simple();
  133. $addressText = $filter->setData($address->getData())->filter($template);
  134. if ($html) {
  135. $addressText = preg_replace('#(<br\s*/?>\s*){2,}#im', '<br/>', $addressText);
  136. } else {
  137. $addressText = preg_replace('#(\n\s*){2,}#m', "\n", $addressText);
  138. }
  139. return $addressText;
  140. }
  141. /**
  142. * Retrieve country formats
  143. *
  144. * @return \Magento\Directory\Model\ResourceModel\Country\Format\Collection
  145. */
  146. public function getFormats()
  147. {
  148. if (!isset(self::$_format[$this->getId()]) && $this->getId()) {
  149. self::$_format[$this->getId()] = $this->_formatFactory->create()->getCollection()->setCountryFilter(
  150. $this
  151. )->load();
  152. }
  153. if (isset(self::$_format[$this->getId()])) {
  154. return self::$_format[$this->getId()];
  155. }
  156. return null;
  157. }
  158. /**
  159. * Retrieve country format
  160. *
  161. * @param string $type
  162. * @return \Magento\Directory\Model\Country\Format|null
  163. */
  164. public function getFormat($type)
  165. {
  166. if ($this->getFormats()) {
  167. foreach ($this->getFormats() as $format) {
  168. if ($format->getType() == $type) {
  169. return $format;
  170. }
  171. }
  172. }
  173. return null;
  174. }
  175. /**
  176. * Get country name
  177. *
  178. * @return string
  179. */
  180. public function getName($locale = null)
  181. {
  182. if ($locale == null) {
  183. $cache_key = 'name_default';
  184. } else {
  185. $cache_key = 'name_' . $locale;
  186. }
  187. if (!$this->getData($cache_key)) {
  188. $this->setData($cache_key, $this->_localeLists->getCountryTranslation($this->getId(), $locale));
  189. }
  190. return $this->getData($cache_key);
  191. }
  192. }