slpxxv / stdlib
An extension to the Standard PHP Library (SPL), introducing abstract data types like stacks, maps, and hash maps. Enjoy the benefits of class-based functions in PHP with an intuitive and object-oriented API.
Fund package maintenance!
Requires
- php: >= 8.2
- ext-dom: *
- ext-libxml: *
Requires (Dev)
- phpunit/phpunit: ^10.5 || ^11.5
- rector/rector: ^2.0
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.13 || ^4.0
- vimeo/psalm: ^5.26 || ^6.0
README
StdLib is an extension to the Standard PHP Library (SPL), introducing abstract data types like stacks, maps, and hash maps. Enjoy the benefits of class-based functions in PHP with an intuitive and object-oriented API.
Features
- Extended Abstract Data Types: Augment your PHP experience with extended abstract data types, such as stacks, maps, and hash maps, providing powerful and versatile tools for data manipulation.
- Class-Based Functions: Enjoy the benefits of PHP class-based functions, offering a more organized and object-oriented approach to handling various tasks.
- Object-Oriented API: Leverage an intuitive and object-oriented API for seamless integration and utilization of the extended SPL features.
Installation
Install the Standard Library for PHP using Composer:
composer require slpxxv/stdlib
Collections
ArrayCollection preserves keys and provides non-mutating functional operations:
use Slpxxv\Stdlib\ArrayCollection; $values = ArrayCollection::fromIterable(['a' => 1, 'b' => 2, 'c' => 3]); $result = $values ->filter(static fn(int $value): bool => $value > 1) ->map(static fn(int $value): int => $value * 10); $result->toArray(); // ['b' => 20, 'c' => 30]
StdArray remains available as a compatibility layer. New code should prefer
ArrayCollection and isEmpty() over StdArray and empty().
Maps and sets
Maps distinguish a missing key from a key containing null:
use Slpxxv\Stdlib\Map\Map; $map = new Map(['answer' => null]); $map->has('answer'); // true $map->get('answer', 42); // null $map->get('missing', 42); // 42 $map->require('missing'); // KeyNotFoundException
Sets reject duplicate values and support standard set operations:
use Slpxxv\Stdlib\Set\Set; $left = new Set([1, 2]); $right = new Set([2, 3]); $left->union($right)->all(); // [1, 2, 3] $left->intersect($right)->all(); // [2] $left->diff($right)->all(); // [1]
Lazy sequences
LazySequence evaluates its source only during iteration:
use Slpxxv\Stdlib\LazySequence; $lines = new LazySequence(static function (): iterable { $file = new SplFileObject('large.csv'); foreach ($file as $line) { yield $line; } }); $firstTenNonEmptyLines = $lines ->filter(static fn(string $line): bool => trim($line) !== '') ->take(10) ->toArray();
The factory is called for each iteration. Pass a factory instead of a bare
Iterator when the source needs to be repeatable.
XML
Canonicalization algorithms have a typed representation:
use Slpxxv\Stdlib\XML\CanonicalizationAlgorithm; use Slpxxv\Stdlib\XML\XmlCanonicalizer; $canonicalXml = XmlCanonicalizer::canonicalize( $document, CanonicalizationAlgorithm::Exclusive, );
DOMDocumentFactory disables network access by default, rejects DOCTYPE, and
restores the global libxml error state after success or failure.
Development
composer install composer check
Development commands:
composer cs:check # coding standard composer cs:fix # automatically fix formatting composer analyse # Psalm level 1 composer test # PHPUnit composer test-coverage # coverage report in build/ composer refactor:check # Rector dry run composer refactor # apply Rector refactorings composer security:check # Composer security audit composer check:all # all non-mutating checks composer mutation # Infection mutation tests
Rector is installed with project dependencies. Infection should be installed as an isolated PHAR or global tool because Composer releases compatible with PHP 8.1 conflict with the parser versions required by the current PHPUnit and Psalm. The official Infection PHAR is the recommended distribution:
phive install infection
# or install the verified infection.phar and expose it as `infection`
The repository includes .editorconfig, rector.php and infection.json5, so
editors and tools use the same project settings locally and in CI.
License
The MIT License (MIT). Please see License for more information.