Download the PHP package matthijsbreijer/oaktree without Composer
On this page you can find all versions of the php package matthijsbreijer/oaktree. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package oaktree
OakTree
A tree data structure implementation with support for key-based traversal and custom (un)serialization. Useful implementations could include product trees, file/folder structures, routing trees and a lot more. As a package its written from scratch, mostly derived from nicmart/Tree
Tree structure and tree traversal methods
Node creation
A Node can be passed any type of value during instantiation.
Getting and setting values
A node value can be retrieved and altered using Node::getValue()
and Node::setValue()
. Note that Node::setValue()
is fluent and can be daisy-chained.
Adding children to a Node
One or more children can be added using Node::addChild()
. This method is fluent and can be daisy-chained.
Added children to a Node with a specific child key
OakTree keeps track of keys for the Nodes within the tree. As such one can assign a key to a child Node.
Removing a child
Remove the $child1
and $child2
instance from $node
's children. Node::removeChild()
is fluent and can be daisy-chained.
Get all direct children of a Node
Get all keys of direct children Nodes
Get a child Node by its key
OakTree keeps keys intact for the Nodes within the tree. As such one can get a child Node by key.
Get the child and remove child from its Node tree
Using Node::pop()
one can remove/separate a Node (and its descendants) from a tree and alter it independently.
Set children
Please note that this method removes previously present children. Node::setChildren()
sets a (new) array of children Nodes. This method is fluent and can be daisy-chained.
Get the parent node of a child
Get the root Node of a tree
Tree context
Is the Node a leaf?
A leaf is a node with no children.
Is the Node a child?
A child is a node that has a parent.
Is the Node then root Node?
A root Node has no parent.
Tree alterations
OakTree uses the Visitor Pattern to iterate over a tree. The visitor contains the logic to apply on the Nodes in the tree. Visitors must implement the Visitor interface MatthijsBreijer\OakTree\Visitor\VisitorInterface
. Visitors return mixed content depending on their purpose.
LeafVisitor
The LeafVisitor
returns an array of leaves of a tree ($node
's where Node::isLeaf()
returns true).
ClosureVisitor
The ClosureVisitor can be passed a function or\Closure
argument to define its behavior. The example below mimicks the LeafVisitor.
Serialization / Unserialization
OakTree Nodes have a Node::toArray()
and Node::fromArray()
method to allow customized (un)serialization of a tree. This can be used to cache data, to quickly pass around information from the tree to an API or vice versa. The nodes also implement PHP's \JsonSerializable
interface.
Basic tree to array conversion
Basic array to tree conversion
Using the above example the array result $array
can be converted back to a tree as follows.
Closure based tree to array conversion
The Node::toArray()
method accepts a second argument for \Closure
-based conversion to an array, which in turn can be serialized.
Closure based array to tree conversion
Using the $array
variable created in previous example the array result can be converted back to a tree as follows.
Installation
Composer
OakTree can be installed using the PHP Composer package manager using the following command: