|
16 | 16 | class CategoryCollection extends Collection |
17 | 17 | { |
18 | 18 | const SLUG = 'slug'; |
| 19 | + const PARENT = 'parent'; |
| 20 | + const ID = 'id'; |
| 21 | + const ROOTS = 'roots'; |
19 | 22 |
|
20 | 23 | protected $type = '\Commercetools\Core\Model\Category\Category'; |
21 | 24 |
|
22 | 25 | protected function indexRow($offset, $row) |
23 | 26 | { |
24 | 27 | if ($row instanceof Category) { |
25 | 28 | $slugs = $row->getSlug()->toArray(); |
| 29 | + $parentId = !is_null($row->getParent()) ? $row->getParent()->getId() : null; |
| 30 | + $id = $row->getId(); |
26 | 31 | } else { |
27 | 32 | $slugs = isset($row[static::SLUG]) ? $row[static::SLUG] : []; |
| 33 | + $id = isset($row[static::ID]) ? $row[static::ID] : null; |
| 34 | + $parentId = isset($row[static::PARENT][static::ID]) ? $row[static::PARENT][static::ID] : null; |
28 | 35 | } |
29 | 36 | foreach ($slugs as $locale => $slug) { |
30 | 37 | $locale = \Locale::canonicalize($locale); |
31 | 38 | $this->addToIndex(static::SLUG, $offset, $locale . '-' . $slug); |
| 39 | + if (is_null($parentId)) { |
| 40 | + $this->addToIndex(static::ROOTS, $offset, $id); |
| 41 | + } else { |
| 42 | + $this->addToIndex(static::PARENT . '-' . $parentId, $offset, $id); |
| 43 | + } |
32 | 44 | } |
33 | 45 | } |
34 | 46 |
|
| 47 | + public function getRoots() |
| 48 | + { |
| 49 | + $elements = []; |
| 50 | + foreach ($this->index[static::ROOTS] as $key => $offset) { |
| 51 | + $elements[$key] = $this->getAt($offset); |
| 52 | + } |
| 53 | + return $elements; |
| 54 | + } |
| 55 | + |
| 56 | + public function getByParent($parentId) |
| 57 | + { |
| 58 | + $elements = []; |
| 59 | + if (!isset($this->index[static::PARENT . '-' . $parentId])) { |
| 60 | + return $elements; |
| 61 | + } |
| 62 | + |
| 63 | + foreach ($this->index[static::PARENT . '-' . $parentId] as $key => $offset) { |
| 64 | + $elements[$key] = $this->getAt($offset); |
| 65 | + } |
| 66 | + return $elements; |
| 67 | + } |
| 68 | + |
35 | 69 | /** |
36 | 70 | * @param $locale |
37 | 71 | * @param $slug |
|
0 commit comments