{"id":26013275,"url":"https://github.com/jimbo2150/php-comparable","last_synced_at":"2025-03-06T01:28:02.744Z","repository":{"id":277915973,"uuid":"933836863","full_name":"jimbo2150/php-comparable","owner":"jimbo2150","description":"PHP library to allow for comparison of two objects with a comparison operator.","archived":false,"fork":false,"pushed_at":"2025-03-04T23:56:21.000Z","size":120,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-05T00:28:43.285Z","etag":null,"topics":["compare","comparison","library","object-comparison","operator","operators","php","php8","trait","utility"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jimbo2150.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-16T19:50:18.000Z","updated_at":"2025-03-04T23:56:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"1a1a54de-b339-4a07-a207-901225a2f547","html_url":"https://github.com/jimbo2150/php-comparable","commit_stats":null,"previous_names":["jimbo2150/php-comparable"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimbo2150%2Fphp-comparable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimbo2150%2Fphp-comparable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimbo2150%2Fphp-comparable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimbo2150%2Fphp-comparable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jimbo2150","download_url":"https://codeload.github.com/jimbo2150/php-comparable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242130392,"owners_count":20076623,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compare","comparison","library","object-comparison","operator","operators","php","php8","trait","utility"],"created_at":"2025-03-06T01:28:02.170Z","updated_at":"2025-03-06T01:28:02.723Z","avatar_url":"https://github.com/jimbo2150.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Version](https://img.shields.io/github/v/release/jimbo2150/php-comparable)\n![License](https://img.shields.io/github/license/jimbo2150/php-comparable)\n![PHP Required Version](https://img.shields.io/packagist/dependency-v/jimbo2150/php-comparable/php)\n\n# PHP Comparable Objects Trait \u0026 Custom Comparison Convenience Method\n\nA PHP library to allow for comparison of two objects with a comparison operator.\n\nThis library allows you to compare two objects using a value within the objects rather than the entirety of the object (the default for PHP).\n\n## Installation\n\nThis library is available on Packagist using Composer. Run this command within your composer project directory:\n\n```bash\n$ composer require jimbo2150/php-comparable\n```\n\n## Usage (private value with convenience trait)\n\nThe comparable trait (`ComparableTrait`) requires a protected method, `getComparableValue()`, which does not publically expose the value to be compared. The convenience methods then use reflection to access the other object's value:\n\n```php\nuse Jimbo2150\\PhpComparable\\Trait\\ComparableTrait;\n\n// This class will compare the Person object's $age property.\nclass Person\n{\n\tuse ComparableTrait;\n\n\tpublic function __construct(private string $name, private int $age)\n\t{\n\t\tassert($age \u003e 0);\n\t}\n\n\tpublic function getComparableValue(): mixed\n\t{\n\t\treturn $this-\u003eage;\n\t}\n}\n\n$john = new Person('John', 29);\n$karen = new Person('Karen', 24);\n\n$john-\u003ecompareTo($karen); // False, compares by equals (==) by default\n$karen-\u003ecompareTo($john, Operator::from('\u003c')); // True, comparing with less than\n```\n\n## Usage (private value with custom comparison)\n\nYou can also implement your own custom compare method by creating your own comparison method that utilizes the `customCompareTo(callable $callback, ComparableTrait $compareTo, Operator $operator)` convenience method and provide a callback (callable or Closure) that, when called, is passed the current object's comparison value, the comparison value of the object your are comparing to, and the operator as parameter values:\n\n```php\nuse Jimbo2150\\PhpComparable\\Trait\\ComparableTrait;\n\nclass Score\n{\n\tuse ComparableTrait;\n\n\tpublic const MIN_REQUIRED_SCORE = 1;\n\n\tpublic function __construct(private float $score)\n\t{\n\t}\n\n\tpublic function compareDiff(object $other): bool|int\n\t{\n\t\t$operator = Operator::GREATER_THAN_OR_EQUAL;\n\t\t$diffFunction = fn (\n\t\t\tmixed $leftValue,\n\t\t\tmixed $rightValue,\n\t\t\tOperator $operator,\n\t\t): bool|int =\u003e $operator-\u003ecompare(\n\t\t\t$leftValue - $rightValue,\n\t\t\tself::MIN_REQUIRED_SCORE\n\t\t);\n\n\t\treturn $this-\u003ecustomCompareTo(\n\t\t\t$diffFunction,\n\t\t\t$other,\n\t\t\t$operator\n\t\t);\n\t}\n\n\tpublic function getComparableValue(): mixed\n\t{\n\t\treturn $this-\u003escore;\n\t}\n}\n\n$score1 = new Score(2.4);\n$score2 = new Score(0.7);\n$score3 = new Score(0.2);\n\n$score1-\u003ecompareDiff($score2); // True, score is \u003e= 1\n$score2-\u003ecompareDiff($score3); // False, score is less than 1\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjimbo2150%2Fphp-comparable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjimbo2150%2Fphp-comparable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjimbo2150%2Fphp-comparable/lists"}