1 <?php
2
3 4 5 6 7 8 9 10 11
12
13 14 15 16 17 18 19 20 21 22
23 class CategoryRepositoryReader implements CategoryRepositoryReaderInterface
24 {
25 26 27 28 29
30 protected $db;
31
32 33 34 35 36
37 protected $categoryFactory;
38
39
40 41 42 43 44 45
46 public function __construct(CI_DB_query_builder $db, CategoryFactoryInterface $categoryFactory)
47 {
48 $this->db = $db;
49 $this->categoryFactory = $categoryFactory;
50 }
51
52
53 54 55 56 57 58 59 60 61
62 public function getById(IdType $categoryId)
63 {
64
65 $categoryData = $this->db->get_where('categories', array(
66 'categories_id' => $categoryId->asInt()
67 ))->row_array();
68
69
70 $categoryDescriptionQuery = $this->db->select('categories_description.*, languages.code AS language_code')
71 ->from('categories_description')
72 ->join('languages',
73 'languages.languages_id = categories_description.language_id',
74 'inner')
75 ->where('categories_description.categories_id', $categoryId->asInt());
76
77 $categoryDescription = $categoryDescriptionQuery->get()->result_array();
78
79 if($categoryData === null)
80 {
81 throw new UnexpectedValueException('The requested category was not found in database (ID:'
82 . $categoryId->asInt() . ')');
83 }
84
85 if($categoryDescription === null)
86 {
87 throw new UnexpectedValueException('The requested category description was not found in database (ID:'
88 . $categoryId->asInt() . ')');
89 }
90
91 $category = $this->_createCategoryByArray($categoryData, $categoryDescription);
92
93 return $category;
94 }
95
96
97 98 99 100 101 102 103
104 public function getByParentId(IdType $parentId)
105 {
106 $subCategories = array();
107 $result = $this->db->select('categories_id')
108 ->get_where('categories', array('parent_id' => $parentId->asInt()))
109 ->result_array();
110
111 foreach($result as $row)
112 {
113 $categoryId = new IdType($row['categories_id']);
114 $subCategories[] = $categoryId;
115 }
116
117 $idCollection = new IdCollection($subCategories);
118
119 return $idCollection;
120 }
121
122
123 124 125 126 127 128 129 130
131 protected function _createCategoryByArray(array $categoryData, array $categoryDescriptionData)
132 {
133 $category = $this->categoryFactory->createStoredCategory(new IdType($categoryData['categories_id']));
134 $category->setActive(new BoolType((boolean)$categoryData['categories_status']));
135 $category->setParentId(new IdType($categoryData['parent_id']));
136 $category->setSortOrder(new IntType((int)$categoryData['sort_order']));
137 $category->setAddedDateTime(new DateTime($categoryData['date_added']));
138 $category->setLastModifiedDateTime(new DateTime($categoryData['last_modified']));
139 $category->setImage(new StringType((string)$categoryData['categories_image']));
140 $category->setIcon(new StringType((string)$categoryData['categories_icon']));
141
142
143 foreach($categoryDescriptionData as $row)
144 {
145 $languageCode = new LanguageCode(new NonEmptyStringType((string)$row['language_code']));
146
147 $category->setName(new StringType((string)$row['categories_name']), $languageCode);
148 $category->setHeadingTitle(new StringType((string)$row['categories_heading_title']), $languageCode);
149 $category->setDescription(new StringType((string)$row['categories_description']), $languageCode);
150 $category->setMetaTitle(new StringType((string)$row['categories_meta_title']), $languageCode);
151 $category->setMetaDescription(new StringType((string)$row['categories_meta_description']), $languageCode);
152 $category->setMetaKeywords(new StringType((string)$row['categories_meta_keywords']), $languageCode);
153 $category->setUrlKeywords(new StringType((string)$row['gm_url_keywords']), $languageCode);
154 $category->setImageAltText(new StringType((string)$row['gm_alt_text']), $languageCode);
155 }
156
157 return $category;
158 }
159 }