1 <?php
2 3 4 5 6 7 8 9 10
11
12 MainFactory::load_class('CustomerCountryZoneReaderInterface');
13
14 15 16 17 18 19 20 21 22 23
24 class CustomerCountryZoneReader implements CustomerCountryZoneReaderInterface
25 {
26 27 28
29 protected $customerFactory;
30 31 32
33 protected $db;
34
35
36 37 38 39 40 41
42 public function __construct(AbstractCustomerFactory $customerFactory, CI_DB_query_builder $dbQueryBuilder)
43 {
44 $this->customerFactory = $customerFactory;
45 $this->db = $dbQueryBuilder;
46 }
47
48
49 50 51 52 53
54 public function findByName(CustomerCountryZoneNameInterface $countryZoneName)
55 {
56 $zoneDataArray = $this->db->get_where('zones', array('zone_name' => (string)$countryZoneName))->row_array();
57 if(empty($zoneDataArray))
58 {
59 return null;
60 }
61 return $this->_createCountryZoneByArray($zoneDataArray);
62 }
63
64
65 66 67 68 69 70
71 public function findByNameAndCountry(CustomerCountryZoneNameInterface $countryZoneName,
72 CustomerCountryInterface $country)
73 {
74 $zoneDataArray = $this->db->get_where('zones', array(
75 'zone_name' => (string)$countryZoneName,
76 'zone_country_id' => $country->getId()))->row_array();
77
78 if(empty($zoneDataArray))
79 {
80 return null;
81 }
82
83 return $this->_createCountryZoneByArray($zoneDataArray);
84 }
85
86
87 88 89 90 91
92 public function findById(IdType $countryZoneId)
93 {
94 $zoneDataArray = $this->db->get_where('zones', array('zone_id' => (int)(string)$countryZoneId))->row_array();
95 if(empty($zoneDataArray))
96 {
97 return null;
98 }
99 return $this->_createCountryZoneByArray($zoneDataArray);
100 }
101
102
103 104 105 106 107
108 public function findCountryZonesByCountryId(IdType $countryId)
109 {
110 $zonesArray = $this->db->get_where('zones', array('zone_country_id' => (int)(string)$countryId))->result_array();
111 foreach($zonesArray as &$zone)
112 {
113 $zone = $this->_createCountryZoneByArray($zone);
114 }
115 return $zonesArray;
116 }
117
118
119 120 121 122
123 protected function _createCountryZoneByArray(array $zoneDataArray)
124 {
125 $countryZone = $this->customerFactory->createCustomerCountryZone(
126 new IdType((int)$zoneDataArray['zone_id']),
127 MainFactory::create('CustomerCountryZoneName', $zoneDataArray['zone_name']),
128 MainFactory::create('CustomerCountryZoneIsoCode', $zoneDataArray['zone_code']));
129 return $countryZone;
130 }
131 }