- How do I integrate this package into a Laravel project for sorting Eloquent collections?
- Use the package’s `Comparator` class with Laravel Collections. Replace `sortBy()` with `Comparator::asc('price')` or `Comparator::desc('created_at')` for consistent, reusable sorting. For Eloquent queries, create a query macro to bridge the gap between the comparator and `orderBy()`. Example: `DB::macro('orderByComparator', fn($query, $field, $direction) => $query->orderBy($field, $direction === 'asc' ? 'asc' : 'desc'));`
- Does this package support Laravel’s Query Builder for database-level comparisons?
- No, it’s PHP-centric. For database operations, you’ll need to manually translate comparators to SQL or use query macros/scopes. Complex SQL (e.g., `CASE WHEN`) may still require custom logic. The package excels in application-layer comparisons, not raw SQL.
- What Laravel versions and PHP versions are officially supported?
- The package supports PHP 8.1+ and is designed to work with Laravel 9+. It leverages modern PHP features like enums and named arguments, ensuring compatibility with Laravel’s latest releases. Check the [documentation](https://php-standard-library.dev) for version-specific notes.
- Can I use this for deep comparison of nested objects or arrays?
- No, this package focuses on shallow comparisons. For deep comparison (e.g., nested objects/arrays), consider alternatives like `spatie/laravel-array-to-object` or `phpunit/phpunit`’s `assertEquals()` with recursion. This package prioritizes performance and simplicity for common use cases.
- How does this handle null values in comparisons?
- The package enforces strict comparison rules by default, so `null` values require explicit handling. Use methods like `isNull()` or `isNotNull()` for null checks, or extend the `Comparator` class to add custom null-aware logic. For domain objects with nullable fields, wrap comparisons in `optional()` or add adapters.
- Will this break existing loose-comparison patterns in my Laravel app?
- Yes, the package enforces strict comparison rules (e.g., `equals()` vs. `looseEquals()`). If your app relies on loose comparisons (e.g., `==` for type juggling), you’ll need to refactor or use the `looseEquals()` method explicitly. This trade-off ensures type safety and consistency.
- How can I use this for API response sorting/filtering in Laravel?
- Centralize sorting logic in a service or middleware. For example, parse a `?sort=price:desc` query string into `Comparator::desc('price')` and apply it to collections or Eloquent queries. Pair with Laravel’s `sortBy()` or custom query macros for seamless integration.
- Are there performance concerns when using this for large datasets?
- No, the package introduces minimal overhead. Benchmark critical paths like sorting large collections, but expect performance comparable to native PHP operators. For database operations, the bottleneck will likely be the query itself, not the comparator logic.
- How do I test comparators in Laravel’s PHPUnit tests?
- Test comparators like any other logic: assert expected outcomes with `assertTrue(Comparator::greaterThan($a, $b))` or `assertFalse(Comparator::equals($a, $b))`. For edge cases (e.g., `NaN`, `Infinity`, custom objects), include explicit test cases. Mock dependencies if comparators interact with external services.
- What are the alternatives to this package for Laravel comparison logic?
- For deep comparison, use `spatie/laravel-array-to-object` or `phpunit/phpunit`. For loose comparisons, stick with native PHP operators or `assertEquals()` with custom normalizers. If you need SQL integration, consider `spatie/laravel-query-builder` for query scopes. This package stands out for its type safety and Laravel synergy in application-layer comparisons.