1 <?php
2
3 4 5 6 7 8 9 10 11
12
13 14 15 16 17 18 19
20 class ProductAttributeRepositoryReader implements ProductAttributeRepositoryReaderInterface
21 {
22 23 24
25 private $db;
26
27 28 29
30 private $factory;
31
32
33 34 35 36 37 38
39 public function __construct(CI_DB_query_builder $db, ProductAttributeFactoryInterface $factory)
40 {
41 $this->db = $db;
42 $this->factory = $factory;
43 }
44
45
46 47 48 49 50 51 52 53 54 55
56 public function getAttributeById(IdType $productAttributeId)
57 {
58 $dataArray = $this->db->get_where('products_attributes',
59 array('products_attributes_id' => $productAttributeId->asInt()))->row_array();
60
61 if(null === $dataArray)
62 {
63 throw new UnexpectedValueException('The requested product attribute was not found in database (ID:'
64 . $productAttributeId->asInt()
65 . ')');
66 }
67
68 $storedProductAttribute = $this->factory->createStoredProductAttribute($productAttributeId);
69 $this->_callStoredProductAttributeSetter($storedProductAttribute, $dataArray);
70
71 return $storedProductAttribute;
72 }
73
74
75 76 77 78 79 80 81 82 83 84 85
86 public function getAttributesByProductId(IdType $productId)
87 {
88 $dataArray =
89 $this->db->get_where('products_attributes', array('products_id' => $productId->asInt()))->result_array();
90
91 if(count($dataArray) === 0)
92 {
93 throw new UnexpectedValueException('The requested product attribute was not found in database (ID:'
94 . $productId->asInt()
95 . ')');
96 }
97
98 $storedProductAttributesArray = array();
99
100 foreach($dataArray as $data)
101 {
102 $storedProductAttribute = $this->factory->createStoredProductAttribute(new IdType($data['products_id']));
103 $this->_callStoredProductAttributeSetter($storedProductAttribute, $data);
104
105 $storedProductAttributesArray[] = $storedProductAttribute;
106 }
107
108 return MainFactory::create('StoredProductAttributeCollection', $storedProductAttributesArray);
109 }
110
111
112 113 114 115 116 117 118 119 120 121
122 protected function _callStoredProductAttributeSetter(StoredProductAttributeInterface $storedProductAttribute, $data)
123 {
124 $storedProductAttribute->setOptionId(new IdType($data['options_id']));
125 $storedProductAttribute->setOptionValueId(new IdType($data['options_values_id']));
126 $storedProductAttribute->setPrice(new DecimalType($data['options_values_price']));
127 $storedProductAttribute->setPriceType(new StringType((string)$data['price_prefix']));
128 $storedProductAttribute->setAttributeModel(new StringType((string)$data['attributes_model']));
129 $storedProductAttribute->setStock(new DecimalType((float)$data['attributes_stock']));
130 $storedProductAttribute->setWeight(new DecimalType($data['options_values_weight']));
131 $storedProductAttribute->setWeightType(new StringType((string)$data['weight_prefix']));
132 $storedProductAttribute->setSortOrder(new IntType((int)$data['sortorder']));
133 $storedProductAttribute->setVpeId(new IdType($data['products_vpe_id']));
134 $storedProductAttribute->setVpeValue(new DecimalType($data['gm_vpe_value']));
135 $storedProductAttribute->setAttributeEan(new StringType((string)$data['gm_ean']));
136
137 return $this;
138 }
139 }