Skip to content

Instantly share code, notes, and snippets.

@pniederlag
Created September 21, 2016 08:57
Show Gist options
  • Save pniederlag/8eef2dfcaa36f593ea81fcdd8b08e3b6 to your computer and use it in GitHub Desktop.
Save pniederlag/8eef2dfcaa36f593ea81fcdd8b08e3b6 to your computer and use it in GitHub Desktop.
sample of a CategoryIterator for categoryTree from news
<?php
namespace Datenbetrieb\D4b6NewsNotification\Utility;
/**
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
/**
* Simple Iterator for CategoryTree from news categoryRepository
*
* example for usage:
*
* $iterator = new \RecursiveIteratorIterator(new CategoryIterator($categoryTree), \RecursiveIteratorIterator::SELF_FIRST);
* foreach($iterator as $key => $value) {
* $categoryId = $value['item']->getUid();
* }
*/
class CategoryIterator extends \ArrayIterator implements \RecursiveIterator
{
public function __construct (array &$data)
{
parent::__construct($data);
}
public function hasChildren()
{
$current = $this->current();
return (isset($current['children']) && count($current['children']) > 0);
}
public function getChildren()
{
$current = $this->current();
return new self($current['children']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment