phrity/comparison
Lightweight PHP interfaces and a helper trait for object comparisons. Implement Equalable/Comparable (or just compare() via ComparisonTrait) to get equals/greater/less helpers, plus a Comparator utility for sorting and filtering. Compatible with PHP 7.1+.
User, Product, Order) to eliminate inconsistencies in sorting/filtering operations, reducing technical debt.usort/array_filter implementations.Comparable/Equalable interfaces.Order::equals($request->order)) to prevent duplicate submissions.Comparator->min($orders) for lowest-priority items).Adopt if:
User->greaterThan($threshold)) to replace unreliable >/< operators or usort callbacks.Look elsewhere if:
Collection->sortBy(), Query Builder).ORDER BY, WHERE)—this package is for in-memory operations only.For Executives/Stakeholders: *"This package standardizes how we compare and sort objects in our app—like products, users, or orders—so we can build features faster and with fewer bugs. For example:
It’s a 10-line drop-in that replaces hours of custom code, reduces technical debt, and makes our backend more predictable. The tradeoff? A tiny dependency (~100 LOC) that only adds value where we’re comparing objects—not for simple cases. For features like dynamic sorting or complex filtering, this is a no-brainer."*
For Engineers/Developers:
*"The phrity/comparison package gives us two powerful tools:
Interfaces/traits (Equalable, Comparable, ComparisonTrait) to enforce consistent comparison methods across our domain models. For example:
class Product implements Comparable {
use ComparisonTrait;
public compare($other): int { /* custom logic */ }
}
Now Product objects can be sorted/filtered anywhere with $product->greaterThan($threshold) or $comparator->sort($products).
A Comparator utility to sort/filter collections of these objects with clean, reusable methods:
$comparator = new Comparator($users);
$highBalances = $comparator->greaterThan($minBalance);
$sorted = $comparator->sort();
This replaces messy usort/array_filter calls and works seamlessly with Laravel Collections or Eloquent results.
Why use it?
> work here?’—every object follows the same comparison rules.User or Order), reuse everywhere.Where to avoid it?
Query Builder or raw SQL).Integration tips:
use ComparisonTrait to your models for instant comparison support.Collection to include Comparator methods (e.g., Collection::compare()).Comparator in API controllers or admin panels for dynamic sorting/filtering.Risk: Minimal—this is a battle-tested, lightweight utility. The only cost is adopting the interfaces, but the payoff is cleaner, faster development for comparison-heavy features."*
How can I help you explore Laravel packages today?