@param array $nodes
@param int   $depth

@return array
480284
protected function getHierarchicalChoicesAttributes($nodes, $depth = 0)
    {
        $choices = array();
        $lastNode = end($nodes);
        foreach ($nodes as $node) {
            $choices[$node['node']['nodeId']] = array(
                'data-depth' => $depth,
                'data-last' => $node === $lastNode,
            );
            if (array_key_exists('child', $node)) {
                $choices = array_merge($choices, $this->getHierarchicalChoicesAttributes($node['child'], $depth + 1));
            }
        }

        return $choices;

    }
820178
private function searchTree($depth = 0)
    {
        $this->depth = $depth;
        foreach ($this->children->getList() as $node) {
            if ($depth > count($this->treePath)) {
                break;
            }

            if ($this->treePath[$depth] != $node->key) {
                continue;
            }

            /**
             * @var $node TreeNode
             */
            if (count($this->treePath) > $depth && $this->allowedToDiveDeeper($depth)) {
                $node->setTreepath($this->treePath);

                return $node->searchTree(++$depth);
            }
        }

        return $this;
    }
132873
public static function getCategoryTreeIds($categoryId, $depth = 1)
    {
        $result = \is_array($categoryId) ? $categoryId : [ $categoryId ];

        if ($depth > 1) {
            $categories = self::create()
                ->filterByParent($categoryId, Criteria::IN)
                ->withColumn('id')
                ->find();

            foreach ($categories as $category) {
                $result = array_merge(
                    $result,
                    self::getCategoryTreeIds($category->getId(), $depth - 1)
                );
            }
        }

        return $result;
    }
957729
protected function wrapOptions(array $nodes)
	{
		$ret = array();

		foreach ( $nodes as $node_element ) {
			/** @var SelectOption $option */
			$option = SelectOption::fromNodeElement($node_element, $this->getPageFactory());
			$ret[] = $option->setSelect($this);
		}

		return $ret;
	}
503359
protected function getGrantedNodes(array $nodes)
    {
        /**
         * @var ReadNodeInterface $node
         */
        foreach ($nodes as $key => $node) {
            if (!$this->isGrantedNode($node)) {
                unset($nodes[$key]);
            }
        }

        return $nodes;
    }
1221386
public function load($entities, $depth = 1)
    {
        if (!\is_array($entities) && !$entities instanceof \Traversable) {
            throw new \InvalidArgumentException(self::E_MSG);
        }

        $entitiesByValueClassByIds = $this->sortEntitiesByValueClass($entities);
        foreach ($entitiesByValueClassByIds as $valueClass => $entitiesById) {
            $this->loadEntityValues($valueClass, $entitiesById);
        }

        $this->loadRelatedEntities($entities, $depth);
    }
378120
public function populateTree($depth = null, $with = null)
    {
        /** @var ActiveRecord[]|static[] $nodes */
        $query = $this->getDescendants($depth);
        if ($with) {
            $query->with($with);
        }
        $nodes = $query->all();

        $key = $this->owner->getAttribute($this->leftAttribute);
        $relates = [];
        $parents = [$key];
        $prev = $this->owner->getAttribute($this->depthAttribute);
        foreach($nodes as $node)
        {
            $level = $node->getAttribute($this->depthAttribute);
            if ($level <= $prev) {
                $parents = array_slice($parents, 0, $level - $prev - 1);
            }

            $key = end($parents);
            if (!isset($relates[$key])) {
                $relates[$key] = [];
            }
            $relates[$key][] = $node;

            $parents[] = $node->getAttribute($this->leftAttribute);
            $prev = $level;
        }

        $ownerDepth = $this->owner->getAttribute($this->depthAttribute);
        $nodes[] = $this->owner;
        foreach ($nodes as $node) {
            $key = $node->getAttribute($this->leftAttribute);
            if (isset($relates[$key])) {
                $node->populateRelation('children', $relates[$key]);
            } elseif ($depth === null || $ownerDepth + $depth > $node->getAttribute($this->depthAttribute)) {
                $node->populateRelation('children', []);
            }
        }

        return $this->owner;
    }
957402
public function toArray(Entity $entity, $depth=0) {
		$res = [];

		foreach($entity->getDefinition()->properties() as $name=>$property) {
			if($entity->getDefinition()->property($name) instanceof Property\EntityProperty) {
				if($depth < 1)
					continue;
				if($entity->getDefinition()->property($name)->get('many')) {
					foreach($entity->get($name, null, false) as $subentity)
						$res[$name][] = $subentity->toArray($depth-1);
				}
				else {
					if($subentity = $entity->get($name, null, false))
						$res[$name] = $subentity->toArray($depth-1);
					else
						$res[$name] = null;
				}
			}
			elseif($property->get('many')) {
				$res[$name] = [];
				foreach($entity->get($name) as $k=>$v)
					$res[$name][$k] = $this->propertyToArray($v, $property);
			}
			else
				$res[$name] = $this->propertyToArray($entity->get($name), $property);
		}

		return $res;
	}
664672
public function filterByDepth($depth){
        return $this->filter(function(Link $link) use($depth){
            return $link->getCrawlDepth() == $depth;
        })->map(function(Link $link){
            return [
                'source_page' => $link->getParentUrl(),
                'link' => $link->getAbsoluteUrl(),
            ];
        });
    }
1250118
public static function flattenAssoc(array $array, $depth = INF) : array
    {
        $result = [];
        $depth = is_int($depth) ? max($depth, 0) : INF;

        foreach ($array as $key => $value) {
            if (is_array($value) && $depth >= 1) {
                $result = self::flattenAssoc($value, $depth - 1) + $result;
            } else {
                $result[$key] = $value;
            }
        }

        return $result;
    }
495357
public function findChildren(callable $callback, $depth = null, $level = null)
    {

        $level = $level ?: 0;

        foreach ($this->children as $child) {

            /** @var NodeInterface $child */
            if ($child->is($callback))
                yield $child;

            if ($depth === null || $level < $depth) {

                if ($child instanceof NodeInterface)
                    foreach ($child->findChildren($callback, $depth, $level + 1) as $subChild)
                        yield $subChild;
            }
        }
    }
1041254
public function setNodes(ArrayCollection $nodes)
    {
        $finalNodes = array();

        foreach ($nodes as $node) {
            if ($this->checkChildNode($node)) {
                $node->setParent($this);
                $finalNodes[] = $node;
            }
        }

        $this->nodes = $finalNodes;

        return $this;
    }
1143480
public function discardNodes(array $nodes)
    {
        $discardedNodeIdentifiers = [];
        foreach ($nodes as $node) {
            $this->doDiscardNode($node, $discardedNodeIdentifiers);
        }
    }
791195
protected function _extractNodes(array $chunks, array $wrapper = array(), $depth = 0) {
        $chunks = $this->_cleanChunks($chunks, $wrapper);
        $nodes = array();
        $tag = array();
        $openIndex = -1;
        $openCount = -1;
        $closeCount = -1;
        $count = count($chunks);
        $i = 0;

        while ($i < $count) {
            $chunk = $chunks[$i];

            // Check for an empty tag as we only need to match the open and closing tags
            // The inner chunks will be extracted once a match is found
            if ($chunk['type'] === self::TAG_NONE && empty($tag)) {
                $nodes[] = $chunk['text'];

            } else if ($chunk['type'] === self::TAG_SELF_CLOSE && empty($tag)) {
                $chunk['children'] = array();
                $nodes[] = $chunk;

            } else if ($chunk['type'] === self::TAG_OPEN) {
                $openCount++;

                if (empty($tag)) {
                    $openIndex = $i;
                    $tag = $chunk;
                }

            } else if ($chunk['type'] === self::TAG_CLOSE) {
                $closeCount++;

                if ($openCount === $closeCount && $chunk['tag'] === $tag['tag']) {
                    $index = $i - $openIndex;
                    $tag = array();

                    // Only reduce if not last index
                    if ($index !== $count) {
                        $index = $index - 1;
                    }

                    // Slice a section of the array if the correct closing tag is found
                    $node = $chunks[$openIndex];
                    $node['depth'] = $depth;
                    $node['children'] = $this->_extractNodes(array_slice($chunks, ($openIndex + 1), $index), $chunks[$openIndex], $depth + 1);
                    $nodes[] = $node;

                // There is no opening or a broken opening tag, which means
                // $closeCount should not have been incremented before >> revert
                } else if (empty($tag)) {
                    $closeCount--;
                }
            }

            $i++;
        }

        return $nodes;
    }
1189843
public function nodeMove($value, $depth) {
        $this->node = $this->owner;
        parent::moveNode($value, $depth);
    }
388124
public function getChildrenNodes(Node $node)
    {
        $result = $this->search($node->getDn(), null, false);

        $nodes = array();
        foreach ($result as $node) {
            $nodes[] = $node;
        }

        return $nodes;
    }
959748
public static function arrayToJSON(array $entities, $depth=0) {
		foreach($entities as $k=>$entity)
			$entities[$k] = $entity->toArray($depth);
		return json_encode($entities);
	}