Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
feat(CategoryCollection): add getByParent and getRoots to CategoryCol…
Browse files Browse the repository at this point in the history
…lection
  • Loading branch information
Jens Schulze committed Sep 8, 2015
1 parent f0caaa1 commit ece9d87
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Model/Category/CategoryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,56 @@
class CategoryCollection extends Collection
{
const SLUG = 'slug';
const PARENT = 'parent';
const ID = 'id';
const ROOTS = 'roots';

protected $type = '\Commercetools\Core\Model\Category\Category';

protected function indexRow($offset, $row)
{
if ($row instanceof Category) {
$slugs = $row->getSlug()->toArray();
$parentId = !is_null($row->getParent()) ? $row->getParent()->getId() : null;
$id = $row->getId();
} else {
$slugs = isset($row[static::SLUG]) ? $row[static::SLUG] : [];
$id = isset($row[static::ID]) ? $row[static::ID] : null;
$parentId = isset($row[static::PARENT][static::ID]) ? $row[static::PARENT][static::ID] : null;
}
foreach ($slugs as $locale => $slug) {
$locale = \Locale::canonicalize($locale);
$this->addToIndex(static::SLUG, $offset, $locale . '-' . $slug);
if (is_null($parentId)) {
$this->addToIndex(static::ROOTS, $offset, $id);
} else {
$this->addToIndex(static::PARENT . '-' . $parentId, $offset, $id);
}
}
}

public function getRoots()
{
$elements = [];
foreach ($this->index[static::ROOTS] as $key => $offset) {
$elements[$key] = $this->getAt($offset);
}
return $elements;
}

public function getByParent($parentId)
{
$elements = [];
if (!isset($this->index[static::PARENT . '-' . $parentId])) {
return $elements;
}

foreach ($this->index[static::PARENT . '-' . $parentId] as $key => $offset) {
$elements[$key] = $this->getAt($offset);
}
return $elements;
}

/**
* @param $locale
* @param $slug
Expand Down

0 comments on commit ece9d87

Please # to comment.