From 4fb2c313f86a3735afd342aab2a7b5d48d25e8ab Mon Sep 17 00:00:00 2001 From: Marcel Frankruijter Date: Thu, 10 Feb 2022 19:12:41 +0100 Subject: [PATCH] feat: implemented php8.1 features --- .travis.yml | 2 +- CHANGELOG.md | 10 ++- composer.json | 3 +- src/Common/ComparatorEnum.php | 87 ++++++++------------ src/Common/DirectionEnum.php | 27 +++--- src/Common/OperatorEnum.php | 33 ++++---- src/Component/Criteria/Filter.php | 18 +--- src/Component/Criteria/FilterGroup.php | 2 +- src/Component/Criteria/Limiter.php | 2 +- src/Component/Criteria/Pager.php | 10 +-- src/Component/Criteria/SearchCriteria.php | 2 +- src/Component/Criteria/Sorter.php | 16 ++-- tests/Component/Criteria/FilterGroupTest.php | 2 +- tests/Component/Criteria/FilterTest.php | 5 +- tests/Component/Criteria/SorterTest.php | 4 +- 15 files changed, 96 insertions(+), 127 deletions(-) diff --git a/.travis.yml b/.travis.yml index 07342d0..8bba3ea 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: php php: - - '8.0' + - '8.1' before_script: - composer install diff --git a/CHANGELOG.md b/CHANGELOG.md index 90ddce2..e3a85ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 3.0.0 - 2022-02-10 +### Added +- PHP 8.1 feature implementation + +### Removed +- `grizz-it/enum`, replaced by the native enums. + ## 2.1.0 - 2021-02-23 ### Added - Compatibility for PHP 8.0 @@ -26,7 +33,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Initial implementation of the package. -- [2.1.0 > Unreleased](https://github.com/grizz-it/search/compare/2.1.0...HEAD) +- [3.0.0 > Unreleased](https://github.com/grizz-it/search/compare/3.0.0...HEAD) +- [2.1.0 > 3.0.0](https://github.com/grizz-it/search/compare/2.1.0...3.0.0) - [2.0.1 > 2.1.0](https://github.com/grizz-it/search/compare/2.0.1...2.1.0) - [2.0.0 > 2.0.1](https://github.com/grizz-it/search/compare/2.0.0...2.0.1) - [1.0.0 > 2.0.0](https://github.com/grizz-it/search/compare/1.0.0...2.0.0) diff --git a/composer.json b/composer.json index 666d580..4afb2c6 100644 --- a/composer.json +++ b/composer.json @@ -7,8 +7,7 @@ "prefer-stable": true, "minimum-stability": "stable", "require": { - "php": "^8.0", - "grizz-it/enum": "^1.1" + "php": "^8.1" }, "authors": [ { diff --git a/src/Common/ComparatorEnum.php b/src/Common/ComparatorEnum.php index a57c86a..bff3c7d 100644 --- a/src/Common/ComparatorEnum.php +++ b/src/Common/ComparatorEnum.php @@ -7,117 +7,100 @@ namespace GrizzIt\Search\Common; -use GrizzIt\Enum\Enum; - -/** - * @method static ComparatorEnum EQ() - * @method static ComparatorEnum NEQ() - * @method static ComparatorEnum GT() - * @method static ComparatorEnum GTEQ() - * @method static ComparatorEnum LT() - * @method static ComparatorEnum LTEQ() - * @method static ComparatorEnum IN() - * @method static ComparatorEnum NIN() - * @method static ComparatorEnum LIKE() - * @method static ComparatorEnum NOT_LIKE() - * @method static ComparatorEnum IS_NULL() - * @method static ComparatorEnum NOT_NULL() - */ -class ComparatorEnum extends Enum +enum ComparatorEnum { /** * Equals comparator "=". * Checks if the field equals the value. - * - * @var string */ - public const EQ = 'eq'; + case EQ; /** * Not equals comparator "!=". * Checks if the field does not equal the value. - * - * @var string */ - public const NEQ = 'neq'; + case NEQ; /** * Greater than comparator ">". * Checks if the field is greater than the value. - * - * @var string */ - public const GT = 'gt'; + case GT; /** * Greater than or equals comparator ">=". * Checks if the field is greater than or equals the value. - * - * @var string */ - public const GTEQ = 'gteq'; + case GTEQ; /** * Less than comparator "<". * Checks if the field is less than the value. - * - * @var string */ - public const LT = 'lt'; + case LT; /** * Less than or equals comparator "<=". * Checks if the field is less than or equals the value. - * - * @var string */ - public const LTEQ = 'lteq'; + case LTEQ; /** * In comparator. * Checks if the field occurs in the value. - * - * @var string */ - public const IN = 'in'; + case IN; /** * Not in comparator. * Checks if the field does not occur in the value. - * - * @var string */ - public const NIN = 'nin'; + case NIN; /** * Like comparator. * Checks if the field looks like the value. - * - * @var string */ - public const LIKE = 'like'; + case LIKE; /** * Not like comparator. * Checks if the field does not look like the value. - * - * @var string */ - public const NOT_LIKE = 'not_like'; + case NOT_LIKE; /** * Is null comparator. * Checks if the field is null. - * - * @var string */ - public const IS_NULL = 'is_null'; + case IS_NULL; /** * Is not null comparator. * Checks if the field is not null. + */ + case NOT_NULL; + + /** + * Retrieves the comparator name from the enum. * - * @var string + * @return string */ - public const NOT_NULL = 'not_null'; + public function name(): string + { + return match($this) { + ComparatorEnum::EQ => 'eq', + ComparatorEnum::NEQ => 'neq', + ComparatorEnum::GT => 'gt', + ComparatorEnum::GTEQ => 'gteq', + ComparatorEnum::LT => 'lt', + ComparatorEnum::LTEQ => 'lteq', + ComparatorEnum::IN => 'in', + ComparatorEnum::NIN => 'nin', + ComparatorEnum::LIKE => 'like', + ComparatorEnum::NOT_LIKE => 'not_like', + ComparatorEnum::IS_NULL => 'is_null', + ComparatorEnum::NOT_NULL => 'not_null', + }; + } } diff --git a/src/Common/DirectionEnum.php b/src/Common/DirectionEnum.php index d92dfac..acb4959 100644 --- a/src/Common/DirectionEnum.php +++ b/src/Common/DirectionEnum.php @@ -7,25 +7,28 @@ namespace GrizzIt\Search\Common; -use GrizzIt\Enum\Enum; - -/** - * @method static DirectionEnum ASC() - * @method static DirectionEnum DESC() - */ -class DirectionEnum extends Enum +enum DirectionEnum { /** * Directs the sorting in ascending order. - * - * @var string */ - public const ASC = 'asc'; + case ASC; /** * Directs the sorting in descending order. + */ + case DESC; + + /** + * Retrieves the direction name from the enum. * - * @var string + * @return string */ - public const DESC = 'desc'; + public function name(): string + { + return match($this) { + DirectionEnum::ASC => 'asc', + DirectionEnum::DESC => 'desc', + }; + } } diff --git a/src/Common/OperatorEnum.php b/src/Common/OperatorEnum.php index be5d1e4..b1cfb66 100644 --- a/src/Common/OperatorEnum.php +++ b/src/Common/OperatorEnum.php @@ -7,33 +7,34 @@ namespace GrizzIt\Search\Common; -use GrizzIt\Enum\Enum; - -/** - * @method static OperatorEnum AND() - * @method static OperatorEnum OR() - * @method static OperatorEnum XOR() - */ -class OperatorEnum extends Enum +enum OperatorEnum { /** * All checks should return true when joined with this operator. - * - * @var string */ - public const AND = 'and'; + case AND; /** * Any check should return true when joined with this operator. - * - * @var string */ - public const OR = 'or'; + case OR; /** * One of the checks should return true when joined with this operator. + */ + case XOR; + + /** + * Retrieves the operator name from the enum. * - * @var string + * @return string */ - public const XOR = 'xor'; + public function name(): string + { + return match($this) { + OperatorEnum::AND => 'and', + OperatorEnum::OR => 'or', + OperatorEnum::XOR => 'xor', + }; + } } diff --git a/src/Component/Criteria/Filter.php b/src/Component/Criteria/Filter.php index d405132..db97358 100644 --- a/src/Component/Criteria/Filter.php +++ b/src/Component/Criteria/Filter.php @@ -12,15 +12,6 @@ class Filter implements FilterInterface { - /** @var string */ - private string $field; - - /** @var ComparatorEnum */ - private ComparatorEnum $comparator; - - /** @var mixed */ - private mixed $value; - /** * Constructor * @@ -29,13 +20,10 @@ class Filter implements FilterInterface * @param mixed $value */ public function __construct( - string $field, - ComparatorEnum $comparator, - mixed $value + private string $field, + private ComparatorEnum $comparator, + private mixed $value ) { - $this->field = $field; - $this->comparator = $comparator; - $this->value = $value; } /** diff --git a/src/Component/Criteria/FilterGroup.php b/src/Component/Criteria/FilterGroup.php index 2cf22c5..27039b2 100644 --- a/src/Component/Criteria/FilterGroup.php +++ b/src/Component/Criteria/FilterGroup.php @@ -26,7 +26,7 @@ class FilterGroup implements FilterGroupInterface */ public function __construct(OperatorEnum $operator = null) { - $this->operator = $operator ?? OperatorEnum::AND(); + $this->operator = $operator ?? OperatorEnum::AND; } /** diff --git a/src/Component/Criteria/Limiter.php b/src/Component/Criteria/Limiter.php index ce5985a..4520f7e 100644 --- a/src/Component/Criteria/Limiter.php +++ b/src/Component/Criteria/Limiter.php @@ -12,7 +12,7 @@ class Limiter implements LimiterInterface { /** @var array */ - private array $fields = []; + private array $fields; /** * Constructor diff --git a/src/Component/Criteria/Pager.php b/src/Component/Criteria/Pager.php index 66ea95d..6affca1 100644 --- a/src/Component/Criteria/Pager.php +++ b/src/Component/Criteria/Pager.php @@ -11,22 +11,14 @@ class Pager implements PagerInterface { - /** @var int */ - private int $page; - - /** @var int */ - private int $amount; - /** * Constructor * * @param int $page * @param int $amount */ - public function __construct(int $page, int $amount) + public function __construct(private int $page, private int $amount) { - $this->page = $page; - $this->amount = $amount; } /** diff --git a/src/Component/Criteria/SearchCriteria.php b/src/Component/Criteria/SearchCriteria.php index a877f5d..d278f55 100644 --- a/src/Component/Criteria/SearchCriteria.php +++ b/src/Component/Criteria/SearchCriteria.php @@ -30,7 +30,7 @@ class SearchCriteria implements SearchCriteriaInterface /** * Adds a filter group to the criteria. * - * @param FilterGroupInterface $filter + * @param FilterGroupInterface $filterGroup * * @return void */ diff --git a/src/Component/Criteria/Sorter.php b/src/Component/Criteria/Sorter.php index ab54cf8..f5dfcba 100644 --- a/src/Component/Criteria/Sorter.php +++ b/src/Component/Criteria/Sorter.php @@ -12,22 +12,16 @@ class Sorter implements SorterInterface { - /** @var DirectionEnum */ - private DirectionEnum $direction; - - /** @var string */ - private string $field; - /** * Constructor * * @param string $field - * @param string $direction + * @param DirectionEnum $direction */ - public function __construct(string $field, DirectionEnum $direction) - { - $this->field = $field; - $this->direction = $direction; + public function __construct( + private string $field, + private DirectionEnum $direction + ) { } /** diff --git a/tests/Component/Criteria/FilterGroupTest.php b/tests/Component/Criteria/FilterGroupTest.php index 8d0aa2d..77ddc84 100644 --- a/tests/Component/Criteria/FilterGroupTest.php +++ b/tests/Component/Criteria/FilterGroupTest.php @@ -35,7 +35,7 @@ public function testFilter(): void $filterMock = $this->createMock(FilterInterface::class); $subject->addFilter($filterMock); $subject->addFilterGroup($filterGroupMock); - $this->assertEquals(OperatorEnum::AND(), $subject->getOperator()); + $this->assertEquals(OperatorEnum::AND, $subject->getOperator()); $this->assertEquals( [$filterMock, $filterGroupMock], $subject->getFilters() diff --git a/tests/Component/Criteria/FilterTest.php b/tests/Component/Criteria/FilterTest.php index 36da8b0..0024a9a 100644 --- a/tests/Component/Criteria/FilterTest.php +++ b/tests/Component/Criteria/FilterTest.php @@ -26,10 +26,11 @@ class FilterTest extends TestCase */ public function testFilter(): void { - $subject = new Filter('foo', ComparatorEnum::EQ(), 'bar'); + $subject = new Filter('foo', ComparatorEnum::EQ, 'bar'); $this->assertInstanceOf(Filter::class, $subject); $this->assertEquals('foo', $subject->getField()); - $this->assertEquals(ComparatorEnum::EQ(), $subject->getComparator()); + $this->assertEquals(ComparatorEnum::EQ, $subject->getComparator()); + $this->assertEquals(ComparatorEnum::EQ->name(), $subject->getComparator()->name()); $this->assertEquals('bar', $subject->getValue()); } } diff --git a/tests/Component/Criteria/SorterTest.php b/tests/Component/Criteria/SorterTest.php index 6ddc6be..60de45f 100644 --- a/tests/Component/Criteria/SorterTest.php +++ b/tests/Component/Criteria/SorterTest.php @@ -25,11 +25,11 @@ class SorterTest extends TestCase */ public function testSorter(): void { - $subject = new Sorter('foo', DirectionEnum::ASC()); + $subject = new Sorter('foo', DirectionEnum::ASC); $this->assertInstanceOf(Sorter::class, $subject); $this->assertEquals('foo', $subject->getField()); $this->assertEquals( - DirectionEnum::ASC(), + DirectionEnum::ASC, $subject->getDirection() ); }