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\Core\Form\ChoiceProvider;
28
29use PrestaShop\PrestaShop\Adapter\Country\CountryDataProvider;
30use PrestaShop\PrestaShop\Core\Form\FormChoiceAttributeProviderInterface;
31use PrestaShop\PrestaShop\Core\Form\FormChoiceProviderInterface;
32
33/**
34 * Class CountryByIdChoiceProvider provides country choices with ID values.
35 */
36final class CountryByIdChoiceProvider implements FormChoiceProviderInterface, FormChoiceAttributeProviderInterface
37{
38    /**
39     * @var CountryDataProvider
40     */
41    private $countryDataProvider;
42
43    /**
44     * @var int
45     */
46    private $langId;
47
48    /**
49     * @var array
50     */
51    private $countries;
52
53    /**
54     * @var int[]
55     */
56    private $dniCountriesId;
57
58    /**
59     * @var int[]
60     */
61    private $postcodeCountriesId;
62
63    /**
64     * @param int $langId
65     * @param CountryDataProvider $countryDataProvider
66     */
67    public function __construct(
68        $langId,
69        CountryDataProvider $countryDataProvider
70    ) {
71        $this->langId = $langId;
72        $this->countryDataProvider = $countryDataProvider;
73    }
74
75    /**
76     * Get currency choices.
77     *
78     * @return array
79     */
80    public function getChoices()
81    {
82        $countries = $this->getCountries();
83        $choices = [];
84
85        foreach ($countries as $country) {
86            $choices[$country['name']] = $country['id_country'];
87        }
88
89        return $choices;
90    }
91
92    /**
93     * @return array
94     */
95    public function getChoicesAttributes()
96    {
97        $countries = $this->getCountries();
98        $dniCountriesId = $this->getDniCountriesId();
99        $postcodeCountriesId = $this->getPostcodeCountriesId();
100        $choicesAttributes = [];
101
102        foreach ($countries as $country) {
103            if (in_array($country['id_country'], $dniCountriesId)) {
104                $choicesAttributes[$country['name']]['need_dni'] = 1;
105            }
106            if (in_array($country['id_country'], $postcodeCountriesId)) {
107                $choicesAttributes[$country['name']]['need_postcode'] = 1;
108            }
109        }
110
111        return $choicesAttributes;
112    }
113
114    /**
115     * @return array
116     */
117    private function getCountries()
118    {
119        if (null === $this->countries) {
120            $this->countries = $this->countryDataProvider->getCountries($this->langId);
121        }
122
123        return $this->countries;
124    }
125
126    /**
127     * @return int[]
128     */
129    private function getDniCountriesId()
130    {
131        if (null === $this->dniCountriesId) {
132            $this->dniCountriesId = $this->countryDataProvider->getCountriesIdWhichNeedDni();
133        }
134
135        return $this->dniCountriesId;
136    }
137
138    private function getPostcodeCountriesId()
139    {
140        if (null === $this->postcodeCountriesId) {
141            $this->postcodeCountriesId = $this->countryDataProvider->getCountriesIdWhichNeedPostcode();
142        }
143
144        return $this->postcodeCountriesId;
145    }
146}
147