php-standard-library/vec
php-standard-library/vec provides small, focused helpers for working with sequential 0-indexed arrays (lists). Create, map, filter, transform, and compose list operations with predictable behavior and clean APIs—part of the PHP Standard Library collection.
Installation:
composer require php-standard-library/vec
No configuration required—just autoload and use.
First Use Case:
Replace raw arrays with Vec for safer, expressive operations:
use PhpStandardLibrary\Vec;
$users = Vec::fromArray(['Alice', 'Bob', 'Charlie']);
$names = $users->map(fn($name) => strtoupper($name));
// Returns Vec(['ALICE', 'BOB', 'CHARLIE'])
Where to Look First:
Vec::fromArray(), Vec::empty(), and core methods (map, filter, reduce).Functional Pipelines: Chain operations for declarative data processing:
$result = Vec::fromArray([1, 2, 3, 4, 5])
->filter(fn($n) => $n % 2 === 0)
->map(fn($n) => $n * 2)
->reduce(0, fn($sum, $n) => $sum + $n);
// Returns 12 (2+4+6)
Immutable Operations:
Use push(), pop(), or splice() to avoid side effects:
$vec = Vec::fromArray([1, 2, 3]);
$newVec = $vec->push(4); // Original remains unchanged
Laravel Integration:
Vec for specialized ops:
$laravelCollection = collect([1, 2, 3]);
$vec = Vec::fromArray($laravelCollection->toArray());
Vec in DTOs or domain objects for explicit intent:
class UserList {
public function __construct(private Vec $users) {}
}
Validation:
Replace manual array checks with Vec assertions:
$vec = Vec::fromArray([1, 2, 3]);
$vec->assertNotEmpty(); // Throws if empty
Performance Overhead:
Vec is optimized for clarity, not raw speed. Benchmark before use in hot paths (e.g., loops).// Bad: Unwrapping repeatedly
$array = $vec->toArray();
$array = array_map(...);
// Good: Chain Vec methods
$vec->map(...)->filter(...);
Indexing Quirks:
Vec enforces 0-based, contiguous indexing. Sparse arrays (e.g., [1 => 'a']) may behave unexpectedly.Vec::fromAssoc() for associative-like data (though it’s not a primary use case).Laravel-Specific:
Vec's API for consistency.Vec interfaces to implementations if extending:
$this->app->bind(VecInterface::class, fn() => new Vec());
Type Hints:
Use Vec in method signatures to enforce usage:
public function process(Vec $items): Vec { ... }
Immutable Debugging:
Log intermediate Vec states to trace transformations:
$vec->tap(fn($v) => Log::debug($v->toArray()));
Common Errors:
OffsetExistsException: Caused by accessing invalid indices. Use get() with a default:
$vec->get(10, null); // Returns null instead of throwing
Vec (e.g., map does, but toArray() doesn’t).Custom Methods:
Extend Vec via traits or decorators:
trait VecExtras {
public function chunk(int $size): Vec {
return Vec::fromArray(array_chunk($this->toArray(), $size));
}
}
Laravel Service Providers:
Register Vec as a global helper:
Vec::macro('greet', fn() => Vec::fromArray(['Hello', 'Vec!']));
// Usage: Vec::greet()->implode(' ');
Testing:
Use Vec::empty() for test fixtures and assert with:
$this->assertEquals(Vec::fromArray([1, 2]), $result);
How can I help you explore Laravel packages today?