Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Country.php
Go to the documentation of this file.
1 <?php
8 
20 {
24  public static $_format = [];
25 
29  protected $_localeLists;
30 
34  protected $_formatFactory;
35 
40 
51  public function __construct(
52  \Magento\Framework\Model\Context $context,
53  \Magento\Framework\Registry $registry,
54  \Magento\Framework\Locale\ListsInterface $localeLists,
55  \Magento\Directory\Model\Country\FormatFactory $formatFactory,
56  \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory,
57  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
58  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
59  array $data = []
60  ) {
61  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
62  $this->_localeLists = $localeLists;
63  $this->_formatFactory = $formatFactory;
64  $this->_regionCollectionFactory = $regionCollectionFactory;
65  }
66 
70  protected function _construct()
71  {
72  $this->_init(\Magento\Directory\Model\ResourceModel\Country::class);
73  }
74 
81  public function loadByCode($code)
82  {
83  $this->_getResource()->loadByCode($this, $code);
84  return $this;
85  }
86 
92  public function getRegions()
93  {
94  return $this->getLoadedRegionCollection();
95  }
96 
100  public function getLoadedRegionCollection()
101  {
102  $collection = $this->getRegionCollection();
103  $collection->load();
104  return $collection;
105  }
106 
110  public function getRegionCollection()
111  {
112  $collection = $this->_regionCollectionFactory->create();
113  $collection->addCountryFilter($this->getId());
114  return $collection;
115  }
116 
122  public function formatAddress(\Magento\Framework\DataObject $address, $html = false)
123  {
124  //TODO: is it still used?
125  $address->getRegion();
126  $address->getCountry();
127 
128  $template = $this->getData('address_template_' . ($html ? 'html' : 'plain'));
129  if (empty($template)) {
130  if (!$this->getId()) {
131  $template = '{{firstname}} {{lastname}}';
132  } elseif (!$html) {
133  $template = "{{firstname}} {{lastname}}
134 {{company}}
135 {{street1}}
136 {{street2}}
137 {{city}}, {{region}} {{postcode}}";
138  } else {
139  $template = "{{firstname}} {{lastname}}<br/>
140 {{street}}<br/>
141 {{city}}, {{region}} {{postcode}}<br/>
142 T: {{telephone}}";
143  }
144  }
145 
146  $filter = new \Magento\Framework\Filter\Template\Simple();
147  $addressText = $filter->setData($address->getData())->filter($template);
148 
149  if ($html) {
150  $addressText = preg_replace('#(<br\s*/?>\s*){2,}#im', '<br/>', $addressText);
151  } else {
152  $addressText = preg_replace('#(\n\s*){2,}#m', "\n", $addressText);
153  }
154 
155  return $addressText;
156  }
157 
163  public function getFormats()
164  {
165  if (!isset(self::$_format[$this->getId()]) && $this->getId()) {
166  self::$_format[$this->getId()] = $this->_formatFactory->create()->getCollection()->setCountryFilter(
167  $this
168  )->load();
169  }
170 
171  if (isset(self::$_format[$this->getId()])) {
172  return self::$_format[$this->getId()];
173  }
174 
175  return null;
176  }
177 
184  public function getFormat($type)
185  {
186  if ($this->getFormats()) {
187  foreach ($this->getFormats() as $format) {
188  if ($format->getType() == $type) {
189  return $format;
190  }
191  }
192  }
193  return null;
194  }
195 
201  public function getName($locale = null)
202  {
203  if ($locale == null) {
204  $cache_key = 'name_default';
205  } else {
206  $cache_key = 'name_' . $locale;
207  }
208 
209  if (!$this->getData($cache_key)) {
210  $this->setData($cache_key, $this->_localeLists->getCountryTranslation($this->getId(), $locale));
211  }
212  return $this->getData($cache_key);
213  }
214 }
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
formatAddress(\Magento\Framework\DataObject $address, $html=false)
Definition: Country.php:122
$resource
Definition: bulk.php:12
$address
Definition: customer.php:38
$type
Definition: item.phtml:13
$format
Definition: list.phtml:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Locale\ListsInterface $localeLists, \Magento\Directory\Model\Country\FormatFactory $formatFactory, \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Country.php:51
$template
Definition: export.php:12
$code
Definition: info.phtml:12