Country.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Model\ResourceModel;
  7. /**
  8. * Country Resource Model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Country extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * Resource initialization
  17. *
  18. * @return void
  19. */
  20. protected function _construct()
  21. {
  22. $this->_init('directory_country', 'country_id');
  23. }
  24. /**
  25. * Load country by ISO code
  26. *
  27. * @param \Magento\Directory\Model\Country $country
  28. * @param string $code
  29. * @return \Magento\Directory\Model\ResourceModel\Country
  30. * @throws \Magento\Framework\Exception\LocalizedException
  31. */
  32. public function loadByCode(\Magento\Directory\Model\Country $country, $code)
  33. {
  34. switch (strlen($code)) {
  35. case 2:
  36. $field = 'iso2_code';
  37. break;
  38. case 3:
  39. $field = 'iso3_code';
  40. break;
  41. default:
  42. throw new \Magento\Framework\Exception\LocalizedException(
  43. __('Please correct the country code: %1.', htmlspecialchars($code))
  44. );
  45. }
  46. return $this->load($country, $code, $field);
  47. }
  48. }