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