azjezz/psl
PSL is a modern, well-typed standard library for PHP 8.4+, inspired by HHVM’s HSL. It offers safer, predictable APIs for async, collections, networking, I/O, crypto, terminal UI, and robust data validation—replacing brittle built-ins with consistent alternatives.
The Tree component provides immutable tree data structures and operations for hierarchical data manipulation.
Trees are fundamental data structures for representing hierarchical relationships. This component provides immutable tree nodes (LeafNode and TreeNode), pure constructor functions, functional operations (map, filter, reduce, fold), tree traversal algorithms, and search utilities.
The Tree component has a simple design:
NodeInterface<T>: Base interface with getValue(): TLeafNode<T>: Leaf nodes (no children)TreeNode<T>: Tree nodes with getChildren(): list<NodeInterface<T>>All tree operations are immutable -- they return new trees rather than modifying existing ones.
Use the tree() and leaf() constructor functions to build trees:
@example('collections/tree-building.php')
You can also build trees from nested arrays or flat lists with parent references, which is especially useful for database records:
@example('collections/tree-from-data.php')
@example('collections/tree-transforming.php')
Three traversal orders are available:
@example('collections/tree-traversal.php')
@example('collections/tree-searching.php')
The traverse() function gives you full control over how the tree is serialized:
@example('collections/tree-serialization.php')
See src/Psl/Tree/ for the full API.
How can I help you explore Laravel packages today?