1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26
27namespace PrestaShop\PrestaShop\Adapter\Country;
28
29use Configuration;
30use Country;
31use Db;
32use DbQuery;
33
34/**
35 * This class will provide data from DB / ORM about Country
36 */
37class CountryDataProvider
38{
39    /**
40     * Return available countries.
41     *
42     * @param int $id_lang Language ID
43     * @param bool $active return only active coutries
44     * @param bool $contain_states return only country with states
45     * @param bool $list_states Include the states list with the returned list
46     *
47     * @return array Countries and corresponding zones
48     */
49    public function getCountries($id_lang, $active = false, $contain_states = false, $list_states = true)
50    {
51        return Country::getCountries($id_lang, $active, $contain_states, $list_states);
52    }
53
54    /**
55     * Returns list of countries IDs which need DNI
56     *
57     * @return array
58     */
59    public function getCountriesIdWhichNeedDni()
60    {
61        $query = new DbQuery();
62        $query
63            ->select('c.`id_country`')
64            ->from('country', 'c')
65            ->where('c.`need_identification_number` = 1')
66        ;
67        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
68
69        return array_map(function ($country) { return $country['id_country']; }, $result);
70    }
71
72    /**
73     * Returns list of countries IDs which need Postcode
74     *
75     * @return array
76     *
77     * @throws \PrestaShopDatabaseException
78     */
79    public function getCountriesIdWhichNeedPostcode()
80    {
81        $query = new DbQuery();
82        $query
83            ->select('c.`id_country`')
84            ->from('country', 'c')
85            ->where('c.`need_zip_code` = 1')
86        ;
87        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
88
89        return array_map(function ($country) { return $country['id_country']; }, $result);
90    }
91
92    /**
93     * Returns list of countries IDS which need a state
94     *
95     * @return array
96     *
97     * @throws \PrestaShopDatabaseException
98     */
99    public function getCountriesIdWhichNeedState()
100    {
101        $query = new DbQuery();
102        $query
103            ->select('c.`id_country`')
104            ->from('country', 'c')
105            ->where('c.`contains_states` = 1')
106        ;
107        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
108
109        return array_map(function ($country) { return $country['id_country']; }, $result);
110    }
111
112    /**
113     * Get Country IsoCode by Id.
114     *
115     * @param int $id Country Id
116     *
117     * @return string the related iso code
118     */
119    public function getIsoCodebyId($id = null)
120    {
121        $countryId = (null === $id) ? Configuration::get('PS_COUNTRY_DEFAULT') : $id;
122
123        return Country::getIsoById($countryId);
124    }
125
126    /**
127     * Get country Id by ISO code.
128     *
129     * @param string $isoCode Country ISO code
130     *
131     * @return int
132     */
133    public function getIdByIsoCode($isoCode)
134    {
135        return Country::getByIso($isoCode);
136    }
137}
138