1 <?php
2
3 /* --------------------------------------------------------------
4 AddressFormatProvider.inc.php 2016-01-04
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2016 Gambio GmbH
8 Released under the GNU General Public License (Version 2)
9 [http://www.gnu.org/licenses/gpl-2.0.html]
10 --------------------------------------------------------------
11 */
12
13 /**
14 * Class AbstractCollection
15 *
16 * @category System
17 * @package Shared
18 */
19 class AddressFormatProvider implements AddressFormatProviderInterface
20 {
21 /**
22 * Database connection.
23 * @var CI_DB_query_builder
24 */
25 protected $db;
26
27
28 /**
29 * AddressFormatProvider constructor.
30 *
31 * @param CI_DB_query_builder $db Database connection.
32 */
33 public function __construct(CI_DB_query_builder $db)
34 {
35 $this->db = $db;
36 }
37
38
39 /**
40 * Returns the address format IDs.
41 * @throws UnexpectedValueException If no ID has been found.
42 * @return IdCollection Collection of address format IDs.
43 */
44 public function getIds()
45 {
46 // Database query.
47 $query = $this->db->select('address_format_id')->from('address_format');
48
49 // Array in which the fetched address format IDs will be pushed as IdType to.
50 $fetchedIds = array();
51
52 // Iterate over each found row and push ID as IdType to array.
53 foreach($query->get()->result_array() as $row)
54 {
55 $id = (integer)$row['address_format_id'];
56 $fetchedIds[] = new IdType($id);
57 }
58
59 // Throw exception if not ID has been found.
60 if($fetchedIds === null)
61 {
62 throw new UnexpectedValueException('No address format IDs were found in the database');
63 }
64
65 // Create a new IdCollection class with the array of IdTypes.
66 $collection = MainFactory::create('IdCollection', $fetchedIds);
67
68 // Return created IdCollection
69 return $collection;
70 }
71
72
73 /**
74 * Returns address format ID based on the country ID provided.
75 *
76 * @param IdType $countryId Country ID.
77 *
78 * @throws UnexpectedValueException If no results have been found.
79 * @return int Address format ID.
80 */
81 public function getByCountryId(IdType $countryId)
82 {
83 // Database query.
84 $this->db->select('address_format.address_format_id')
85 ->from('address_format')
86 ->join('countries', 'address_format.address_format_id = countries.address_format_id')
87 ->where('countries.countries_id', $countryId->asInt());
88
89 $data = $this->db->get()->row_array();
90
91 if($data === null)
92 {
93 throw new UnexpectedValueException('No address format ID has been found');
94 }
95
96 $id = (integer)$data['address_format_id'];
97
98 return $id;
99 }
100
101
102 /**
103 * Returns the address format ID based on the ISO-2 country code provided.
104 *
105 * @param StringType $iso2 ISO-2 country code.
106 *
107 * @throws UnexpectedValueException If no results have been found.
108 * @throws InvalidArgumentException If code does not have two letters.
109 *
110 * @return int Address format ID.
111 */
112 public function getByIsoCode2(StringType $iso2)
113 {
114 if(strlen($iso2->asString()) !== 2) // Argument must have 2 letters.
115 {
116 throw new InvalidArgumentException('Provided ISO code must have exactly 2 letters');
117 }
118
119 // Database query.
120 $this->db->select('address_format.address_format_id')
121 ->from('address_format')
122 ->join('countries', 'address_format.address_format_id = countries.address_format_id')
123 ->where('countries.countries_iso_code_2', $iso2->asString());
124
125 $data = $this->db->get()->row_array();
126
127 if($data === null)
128 {
129 throw new UnexpectedValueException('No address format ID has been found');
130 }
131
132 $id = (integer)$data['address_format_id'];
133
134 return $id;
135 }
136
137
138 /**
139 * Returns the address format ID based on the ISO-3 country code provided.
140 *
141 * @param StringType $iso3 ISO-3 country code.
142 *
143 * @throws UnexpectedValueException If no results have been found.
144 * @throws InvalidArgumentException If code does not have three letters.
145 *
146 * @return int Address format ID.
147 */
148 public function getByIsoCode3(StringType $iso3)
149 {
150 if(strlen($iso3->asString()) !== 3) // Argument must have 3 letters.
151 {
152 throw new InvalidArgumentException('Provided ISO code must have exactly 3 letters');
153 }
154
155 // Database query.
156 $this->db->select('address_format.address_format_id')
157 ->from('address_format')
158 ->join('countries', 'address_format.address_format_id = countries.address_format_id')
159 ->where('countries.countries_iso_code_3', $iso3->asString());
160
161 $data = $this->db->get()->row_array();
162
163 if($data === null)
164 {
165 throw new UnexpectedValueException('No address format ID has been found');
166 }
167
168 $id = (integer)$data['address_format_id'];
169
170 return $id;
171 }
172
173 }