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.
php-standard-library/vec provides functional array utilities, it lacks native Laravel integration (e.g., no Collection facade compatibility or Eloquent model support). It could complement Laravel’s Collection for domain-specific immutable operations (e.g., ordered data pipelines, DTO transformations) but risks redundancy in monolithic applications already using Collection.Collection methods, tap(), when()), but its strict 0-indexed focus limits use cases involving associative arrays or sparse indices.Vec may introduce overhead for simple operations compared to native arrays or Laravel’s optimized Collection. Prioritize for readability/maintainability over raw performance.Vec::fromArray($data)). Could conflict with Laravel’s Collection if not scoped (e.g., avoid mixing Vec::map() and Collection::map() in the same pipeline).Vec<T>, but runtime enforcement is manual (no PHPDoc/attribute integration). Consider custom PHPDoc blocks for IDE support:
/** @var Vec<int, User> */
$users = Vec::fromArray($rawUsers);
map, filter) may shadow Laravel’s Collection, causing confusion. Mitigate with:
Vec methods in code (e.g., vecMap()) or document strict separation.Vec rejects non-indexed arrays—risk of runtime errors if misused. Add input validation:
Vec::ensureIndexed($array) ?: throw new InvalidArgumentException('Non-indexed array');
Vec solve problems Laravel’s Collection or native arrays cannot? (e.g., immutability, strict typing, or DSL-like syntax for ordered data).array_splice() with Vec::splice() for stateful transformations?Array.prototype methods)? If not, budget for a 1–2 week ramp-up.Vec vs. Laravel’s Collection for target operations (e.g., reduce, chunk) using PHP Benchmark.$vec = Vec::fromArray(range(1, 10000));
$collection = collect(range(1, 10000));
$time = Benchmark::chrono(fn() => $vec->filter(fn($n) => $n % 2 === 0));
Vec instances be tested? Example:
use PhpStandardLibrary\Vec;
use PHPUnit\Framework\TestCase;
class VecTest extends TestCase {
public function testMap(): void {
$vec = Vec::fromArray([1, 2, 3]);
$result = $vec->map(fn($n) => $n * 2);
$this->assertEquals(Vec::fromArray([2, 4, 6]), $result);
}
}
Vec::fromArray($request->input('items'))->map(...)->filter(...)).Vec for request/response transformations).Collection).array or stdClass).Pilot Phase (1–2 Sprints):
Vec for critical paths:
// Before
$processed = array_map(fn($item) => $item->name, $items);
// After
$processed = Vec::fromArray($items)->map(fn($item) => $item->name);
Vec alongside Collection where needed (e.g., Vec::fromCollection($collection)).VecHelper class to standardize conversions:
class VecHelper {
public static function fromInput(array $input, string $key): Vec {
return Vec::fromArray($input[$key] ?? []);
}
}
Facade Layer (2–3 Sprints):
Vec as a singleton (optional):
// app/Providers/VecServiceProvider.php
public function register(): void {
$this->app->singleton(Vec::class, fn() => new Vec());
}
AppServiceProvider:
// app/Providers/AppServiceProvider.php
public function boot(): void {
if (! function_exists('vec')) {
function vec(array $array): Vec {
return Vec::fromArray($array);
}
}
}
Gradual Adoption:
Vec for ordered data transformations.Vec in new features; avoid retrofitting existing modules.Vec in domain objects (e.g., public function getItems(): Vec).$vec = Vec::fromArray($collection->toArray());
$collection = collect($vec->toArray());
Vec for domain logic; convert to Collection only for Laravel-specific operations (e.g., Eloquent relationships).array_* functions via Vec::toArray(), but loses immutability guarantees.// tests/TestCase.php
protected function assertVecEquals(Vec $expected, Vec $actual): void {
$this->assertEquals($expected->toArray(), $actual->toArray());
}
Phase 1 (0–2 Weeks):
php-standard-library/vec to composer.json and run dependency checks.map, filter, reduce).Vec usage guidelines (e.g., "Use Vec for ordered data, not associative arrays").Phase 2 (2–4 Weeks):
Vec (e.g., a batch processing service).Phase 3 (Ongoing):
Vec::fromEloquent()).composer.json (e.g., ^1.0) until adoption stabilizes.How can I help you explore Laravel packages today?