2017 © Pedro Peláez
 

library recursor

Create recursive array from one level array or iterable object

image

kelemen/recursor

Create recursive array from one level array or iterable object

  • Tuesday, April 7, 2015
  • by kelemen.samuel
  • Repository
  • 2 Watchers
  • 0 Stars
  • 1,368 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 2 Versions
  • 2 % Grown

The README.md

Recursor

Library build recursive array from one level array with some recursive dependency., (*1)

Input array example

$categories = [
    1 => [
        'title' => 'Sport',
        'portal' => 'TopSport',
        'parent_id' => null,
    ],
    2 => [
        'title' => 'Hockey',
        'portal' => 'TopSport',
        'parent_id' => 1,
    ],
    3 => [
        'title' => 'Football',
        'portal' => 'TopSport',
        'parent_id' => 1,
    ],
    4 => [
        'title' => 'World',
        'portal' => 'BestNews',
        'parent_id' => null,
    ]
];

NodeListRecursor

Only lists (elements without children) has title, nodes are arrays with children., (*2)

use Kelemen\Recursor\NodeListRecursor;

$recursor = new NodeListRecursor($categories, 'title', 'parent_id');
# sets maximal recursion depth
$list->setMaxDepth(3);
# get final recursive array  
$list = $recursor->getList();

# result
array(2) {
  [1] => array(2) {
    [2] => string(6) "Hockey"
    [3] => string(8) "Football"
  }
  [4] => string(5) "World"
}

GroupRecursor

Very usefull to generate array for form selects., (*3)

use Kelemen\Recursor\GroupRecursor;

$recursor = new GroupRecursor($categories, 'portal', 'title', 'parent_id');
$list = $recursor->getList();

# result
array(2) {
  ["TopSport"] => array(3) {
    [1] => string(5) "Sport"
    [2] => string(7) "- Hockey"
    [3] => string(9) "- Football"
  }
  ["BestNews"] => array(1) {
    [4] => string(5) "World"
  }
}

Custom recursor

You can create recursor that fits exact you needs. You need to implement 3 abstract methods., (*4)

# Used as item key in result array
abstract protected function getItemKey($item, $key);

# Get parent key for given item
abstract protected function getParentItemKey($item, $key);

# Returns result item structure as array
abstract protected function getItemData($item, $key, $childrenCount, $level);

Every recursor can use 2 hook methods, (*5)

# Called after build every item
protected function afterItemHook(&$item);

# Called after build result array
protected function afterAllHook(&$items);

If you want to create custom recursor with children in result you need to define $subKey class parameter. This param needs to be used in getItemData() method as array., (*6)

Example of custom recursor

This recursor counts recursive item children., (*7)

namespace Kelemen\Recursor;

class CategoryRecursor extends Recursor
{
    /** @var string         Sub key */
    protected $subKey = 'sub';

    /**
     * Returns item key
     * @param $item
     * @param $key
     * @return mixed
     */
    protected function getItemKey($item, $key)
    {
        return $item['id'];
    }

    /**
     * Returns item parent key
     * @param $item
     * @param $key
     * @return mixed
     */
    protected function getParentItemKey($item, $key)
    {
        return $item['parent_id'];
    }

    /**
     * Returns item result data
     * @param $item
     * @param $key
     * @param int $childrenCount
     * @param int $level
     * @return array
     */
    protected function getItemData($item, $key, $childrenCount, $level)
    {
        return [
            'title' => $item['title'],
            'portal_id' => $item['portal_id'],
            'count' => 0,
            $this->subKey => []
        ];
    }

    /**
     * @param array $return
     */
    public function afterItemHook(&$return)
    {
        $count = count($return['sub']);
        foreach ($return['sub'] as $sub) {
            $count += $sub['count'];
        }
        $return['count'] = $count;
    }
}

The Versions

07/04 2015

dev-master

9999999-dev

Create recursive array from one level array or iterable object

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

07/04 2015

v1.0.0

1.0.0.0

Create recursive array from one level array or iterable object

  Sources   Download

MIT

The Requires

  • php >=5.5.0