php-standard-library/range
Range types for integer sequences with iteration support, part of the PHP Standard Library. Provides typed, iterable integer ranges for clean sequence generation in PHP applications. Install via Composer and use alongside other php-standard-library packages.
Precision for Laravel Use Cases:
chunk() and queue workers via Range::lazy(), enabling memory-efficient iteration over large datasets (e.g., user IDs, order batches). Ideal for background jobs processing millions of records without memory overload./users?ids=100-200:2 for even IDs) by leveraging Range::step() and Range::fromString() (if extended). Reduces boilerplate for dynamic filtering in APIs.Range objects, improving code reliability and reducing runtime errors.Range::from(1, 100)->each(fn($id) => User::factory()), reducing flakiness in test suites.Range::from($page * $perPage, ($page + 1) * $perPage)).Laravel Synergy:
whereIn() with dynamic range generation, enabling queries like whereIn('id', Range::from($start, $end)->toArray()). Could extend to a custom whereInRange() method for cleaner syntax.Collection methods, allowing operations like Range::from(1, 5)->map(fn($x) => User::find($x)) without manual array construction.lazy() method is critical for memory efficiency in queue jobs, enabling processing of large ranges (e.g., 1M+ records) in chunks without exhausting memory.RangeHelper) for global accessibility across the application.Anti-Patterns:
for ($i = 0; $i < 5; $i++)), where native PHP loops may be more performant. Benchmarking is recommended for performance-critical paths.lazy(), step(), skip(), take()) that may require refactoring existing range logic. Requires developer training or documentation.1.5-3.2), limiting use cases involving non-integer sequences.Laravel Compatibility:
Range type support and new features (lazy(), step()). Laravel 8.x+ is fully compatible.Key Features for Laravel:
Range::from(1, 1_000_000)->lazy()). Reduces memory usage by avoiding loading entire ranges into memory.skip(), take(), and step() support functional programming patterns, making code more composable and readable (e.g., Range::from(1, 100)->skip(10)->take(5)).Deprecations and Backward Compatibility:
Low Risk:
<5% overhead compared to native loops, with lazy() significantly reducing memory usage for large ranges. Suitable for most Laravel applications.assertValid() method prevents runtime errors (e.g., invalid steps, float ranges), improving robustness.Mitigable Risks:
assertValid()) and clear error messages.Range objects may not be serializable by default (e.g., for caching). Test with cache()->put('range', Range::from(1, 10)) and implement custom serialization if needed.Open Questions:
lazy() work seamlessly with Laravel’s query builder? Test for N+1 query issues when iterating over lazy ranges in database operations:
Range::from(1, 1000)->lazy()->each(fn($id) => User::find($id));
Solution: Use Range::toArray() for small ranges or batch queries for large ranges.Range objects be cached or stored in sessions? Implement custom serialization logic if needed:
$serialized = serialize(Range::from(1, 10)->toArray());
Range type support. Require PHP 8.0+ for full functionality.PHP 8.0+: Mandatory for Range type support and new features (lazy(), step()). Laravel 8.x+ is fully compatible.
Laravel-Specific Enhancements:
whereInRange() method to enable range-based queries:
DB::table('users')->whereInRange('id', 1, 100, 2); // WHERE id IN (1, 3, 5, ...)
Implementation:
DB::macro('whereInRange', function ($column, $start, $end, $step = 1) {
return $this->whereIn($column, Range::from($start, $end, $step)->toArray());
});
range() macro to merge ranges into collections:
Collection::macro('range', function ($start, $end, $step = 1) {
return $this->merge(Range::from($start, $end, $step)->toArray());
});
Usage:
collect([])->range(1, 5, 2); // [1, 3, 5]
lazy() for memory-efficient batch jobs:
ProcessUsers::dispatch(Range::from(1, 1_000_000)->lazy());
Ensure jobs handle lazy iterators gracefully (e.g., avoid closing iterators prematurely).$this->app->singleton('range', function () {
return new \RangeHelper();
});
Facade:
facade(RangeHelper::class);
RangeHelper::lazy(1, 1000)->each(...);
Alternatives Considered:
Range (8.1+): Lacks lazy() and step() functionality, making this package a more feature-rich alternative.step() and skip().How can I help you explore Laravel packages today?