classes/XLite/Model/Country.php line 15

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XLite\Model;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * Country
  10.  *
  11.  * @ORM\Entity
  12.  * @ORM\Table  (name="countries",
  13.  *      indexes={
  14.  *          @ORM\Index (name="enabled", columns={"enabled"})
  15.  *      }
  16.  * )
  17.  */
  18. class Country extends \XLite\Model\Base\I18n
  19. {
  20.     /**
  21.      * Country code (ISO 3166-1 alpha-2)
  22.      *
  23.      * @var string
  24.      *
  25.      * @ORM\Id
  26.      * @ORM\Column (type="string", options={ "fixed": true }, length=2, unique=true)
  27.      */
  28.     protected $code;
  29.     /**
  30.      * Id
  31.      *
  32.      * @var integer
  33.      *
  34.      * @ORM\Column (type="integer")
  35.      */
  36.     protected $id;
  37.     /**
  38.      * Country code (ISO 3166-1 alpha-3)
  39.      *
  40.      * @var string
  41.      *
  42.      * @ORM\Column (type="string", options={ "fixed": true }, length=3)
  43.      */
  44.     protected $code3 '';
  45.     /**
  46.      * Enabled falg
  47.      *
  48.      * @var boolean
  49.      *
  50.      * @ORM\Column (type="boolean")
  51.      */
  52.     protected $enabled true;
  53.     /**
  54.      * States (relation)
  55.      *
  56.      * @var \Doctrine\Common\Collections\ArrayCollection
  57.      *
  58.      * @ORM\OneToMany (targetEntity="XLite\Model\State", mappedBy="country", cascade={"all"})
  59.      * @ORM\OrderBy   ({"state" = "ASC"})
  60.      */
  61.     protected $states;
  62.     /**
  63.      * Regions (relation)
  64.      *
  65.      * @var \Doctrine\Common\Collections\ArrayCollection
  66.      *
  67.      * @ORM\OneToMany (targetEntity="XLite\Model\Region", mappedBy="country", cascade={"all"})
  68.      * @ORM\OrderBy   ({"weight" = "ASC"})
  69.      */
  70.     protected $regions;
  71.     /**
  72.      * Currency
  73.      *
  74.      * @var \XLite\Model\Currency
  75.      *
  76.      * @ORM\ManyToOne (targetEntity="XLite\Model\Currency", inversedBy="countries")
  77.      * @ORM\JoinColumn (name="currency_id", referencedColumnName="currency_id", onDelete="CASCADE")
  78.      */
  79.     protected $currency;
  80.     /**
  81.      * Language
  82.      *
  83.      * @var \XLite\Model\Language
  84.      *
  85.      * @ORM\ManyToOne (targetEntity="XLite\Model\Language", inversedBy="countries")
  86.      * @ORM\JoinColumn (name="lng_id", referencedColumnName="lng_id", onDelete="CASCADE")
  87.      */
  88.     protected $language;
  89.     /**
  90.      * @var \Doctrine\Common\Collections\Collection
  91.      *
  92.      * @ORM\OneToMany (targetEntity="XLite\Model\CountryTranslation", mappedBy="owner", cascade={"all"})
  93.      */
  94.     protected $translations;
  95.     /**
  96.      * Constructor
  97.      *
  98.      * @param array $data Entity properties OPTIONAL
  99.      *
  100.      * @return void
  101.      */
  102.     public function __construct(array $data = [])
  103.     {
  104.         $this->states = new \Doctrine\Common\Collections\ArrayCollection();
  105.         $this->regions = new \Doctrine\Common\Collections\ArrayCollection();
  106.         parent::__construct($data);
  107.     }
  108.     /**
  109.      * Get count of states
  110.      *
  111.      * @return integer
  112.      */
  113.     public function getStatesCount()
  114.     {
  115.         return count($this->states);
  116.     }
  117.     /**
  118.      * Check if country has states
  119.      *
  120.      * @return boolean
  121.      */
  122.     public function hasStates()
  123.     {
  124.         return !$this->isForcedCustomState() && $this->getStatesCount();
  125.     }
  126.     /**
  127.      * @return bool
  128.      */
  129.     public function isForcedCustomState()
  130.     {
  131.         $countries \Includes\Utils\ConfigParser::getOptions([
  132.             'storefront_options',
  133.             'autocomplete_states_for_countries'
  134.         ]);
  135.         return in_array($this->getCode(), $countries) || in_array('All'$countries);
  136.     }
  137.     /**
  138.      * Check if country has regions
  139.      *
  140.      * @return boolean
  141.      */
  142.     public function hasRegions()
  143.     {
  144.         return count($this->regions);
  145.     }
  146.     /**
  147.      * Set code
  148.      *
  149.      * @param string $code
  150.      * @return Country
  151.      */
  152.     public function setCode($code)
  153.     {
  154.         $this->code $code;
  155.         return $this;
  156.     }
  157.     /**
  158.      * Get code
  159.      *
  160.      * @return string
  161.      */
  162.     public function getCode()
  163.     {
  164.         return $this->code;
  165.     }
  166.     /**
  167.      * Set id
  168.      *
  169.      * @param integer $id
  170.      * @return Country
  171.      */
  172.     public function setId($id)
  173.     {
  174.         $this->id $id;
  175.         return $this;
  176.     }
  177.     /**
  178.      * Get id
  179.      *
  180.      * @return integer
  181.      */
  182.     public function getId()
  183.     {
  184.         return $this->id;
  185.     }
  186.     /**
  187.      * Set code3
  188.      *
  189.      * @param string $code3
  190.      * @return Country
  191.      */
  192.     public function setCode3($code3)
  193.     {
  194.         $this->code3 $code3;
  195.         return $this;
  196.     }
  197.     /**
  198.      * Get code3
  199.      *
  200.      * @return string
  201.      */
  202.     public function getCode3()
  203.     {
  204.         return $this->code3;
  205.     }
  206.     /**
  207.      * Set enabled
  208.      *
  209.      * @param boolean $enabled
  210.      * @return Country
  211.      */
  212.     public function setEnabled($enabled)
  213.     {
  214.         $this->enabled = (bool)$enabled;
  215.         return $this;
  216.     }
  217.     /**
  218.      * Get enabled
  219.      *
  220.      * @return boolean
  221.      */
  222.     public function getEnabled()
  223.     {
  224.         return $this->enabled;
  225.     }
  226.     /**
  227.      * Add states
  228.      *
  229.      * @param \XLite\Model\State $states
  230.      * @return Country
  231.      */
  232.     public function addStates(\XLite\Model\State $states)
  233.     {
  234.         $this->states[] = $states;
  235.         return $this;
  236.     }
  237.     /**
  238.      * Get states
  239.      *
  240.      * @return \Doctrine\Common\Collections\Collection
  241.      */
  242.     public function getStates()
  243.     {
  244.         return $this->states;
  245.     }
  246.     /**
  247.      * Add regions
  248.      *
  249.      * @param \XLite\Model\Region $regions
  250.      * @return Country
  251.      */
  252.     public function addRegions(\XLite\Model\Region $regions)
  253.     {
  254.         $this->regions[] = $regions;
  255.         return $this;
  256.     }
  257.     /**
  258.      * Get regions
  259.      *
  260.      * @return \Doctrine\Common\Collections\Collection
  261.      */
  262.     public function getRegions()
  263.     {
  264.         return $this->regions;
  265.     }
  266.     /**
  267.      * Set currency
  268.      *
  269.      * @param \XLite\Model\Currency $currency
  270.      * @return Country
  271.      */
  272.     public function setCurrency(\XLite\Model\Currency $currency null)
  273.     {
  274.         $this->currency $currency;
  275.         return $this;
  276.     }
  277.     /**
  278.      * Get currency
  279.      *
  280.      * @return \XLite\Model\Currency
  281.      */
  282.     public function getCurrency()
  283.     {
  284.         return $this->currency;
  285.     }
  286.     /**
  287.      * Set language
  288.      *
  289.      * @param \XLite\Model\Language $lng
  290.      * @return Country
  291.      */
  292.     public function setLanguage(\XLite\Model\Language $lng)
  293.     {
  294.         $this->language $lng;
  295.         return $this;
  296.     }
  297.     /**
  298.      * Get language
  299.      *
  300.      * @return \XLite\Model\Language $lng
  301.      */
  302.     public function getLanguage()
  303.     {
  304.         return $this->language;
  305.     }
  306.     // {{{ Translation Getters / setters
  307.     /**
  308.      * @return string
  309.      */
  310.     public function getCountry()
  311.     {
  312.         return $this->getTranslationField(__FUNCTION__);
  313.     }
  314.     /**
  315.      * @param string $country
  316.      *
  317.      * @return \XLite\Model\Base\Translation
  318.      */
  319.     public function setCountry($country)
  320.     {
  321.         return $this->setTranslationField(__FUNCTION__$country);
  322.     }
  323.     // }}}
  324. }