- How does this package help with Laravel Collections sorting beyond native `sortBy()`?
- This package provides reusable, deterministic comparators that replace ad-hoc `usort()` or inline logic in Collections. For example, `Comparison::sortBy('price', 'desc')` ensures consistent sorting rules across your app, while native methods may vary in behavior for edge cases like mixed types or null values.
- Can I use this for Eloquent model queries like `orderBy()` or `where()`?
- Direct integration with Eloquent isn’t built-in, but you can wrap comparators in query scopes or global macros. For example, `DB::macro('orderByComparator', fn($field, $direction) => ...)` lets you use `User::orderByComparator('age', 'desc')`. This requires manual setup but avoids duplicating comparison logic.
- Will this work with Laravel 10+ and PHP 8.2+ features like enums or generics?
- The package is designed to be type-aware and works with modern PHP, including enums. However, it doesn’t enforce generics—you’ll need to handle type hints manually in your domain objects. For enums, use `Comparison::is($value, MyEnum::VALUE)` for strict checks.
- How do I handle null values or custom domain objects (e.g., Money, DateRange)?
- The package doesn’t include null safety by default, so you’ll need to extend it or wrap values. For domain objects, create adapters like `Comparison::compareMoney($order1->total, $order2->total)`. Example: `Comparison::extend('money', fn($a, $b) => $a->compareTo($b));`
- Is there a performance impact compared to native PHP operators (e.g., `===`, `>`, `<`)?
- Yes, method calls add minor overhead, but the impact is negligible for most use cases. Benchmark critical paths (e.g., API rate limits) if you’re sorting large datasets. For micro-optimizations, cache comparator results or use native operators where simplicity is key.
- Can I use this in Blade templates or API request filtering?
- Blade templates require helper functions (e.g., `@if(comparison()->isActive($user))`) or custom directives. For APIs, integrate with request classes like `SortableRequest::applyComparator($request->sort)` to standardize filtering/sorting logic in routes.
- What’s the difference between this and Laravel’s built-in Collection methods?
- Laravel’s `sortBy()` is flexible but lacks consistency for edge cases (e.g., mixed types, custom rules). This package enforces deterministic behavior, like `Comparison::sortBy('created_at', 'desc')` always treating nulls or strings uniformly, reducing bugs in complex comparisons.
- How do I migrate from ad-hoc `if`/`switch` comparisons to this package?
- Start by replacing repetitive checks in services/controllers. Example: Replace `if ($user->role === 'admin')` with `if (Comparison::is($user->role, 'admin'))`. Use IDE refactoring tools to find and replace patterns, then test edge cases (e.g., null, empty strings) to validate correctness.
- Does this support fuzzy matching or custom comparison rules (e.g., Levenshtein distance)?
- No, this package focuses on strict, type-aware comparisons. For fuzzy matching, pair it with libraries like `rubix/ml` or extend the package with custom comparators. Example: `Comparison::extend('fuzzy', fn($a, $b) => similar_text($a, $b, $percent));`
- What’s the maintenance status, and are there Laravel-specific integrations planned?
- The package is actively maintained by php-standard-library with a focus on core comparison utilities. Laravel-specific integrations (e.g., Eloquent macros) aren’t planned, but the lightweight design makes it easy to extend. Contribute custom integrations via GitHub if needed.