Skip to content

Commit 266fab9

Browse files
committed
upgrade support for php81
1 parent c4a978c commit 266fab9

File tree

11 files changed

+112
-145
lines changed

11 files changed

+112
-145
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/vendor/
22
/docs/
33
.php_cs.cache
4+
.phpunit.result.cache
5+
.idea
46
composer.lock

README.md

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,6 @@ This package is available through Composer/Packagist:
3939
composer require tklein/php-combine-conditions
4040
```
4141

42-
### Manual
43-
44-
[Download](https://github.com/thomas-kl1/php-combine-conditions/zipball/master) this repo,
45-
or the [latest release](https://github.com/thomas-kl1/php-combine-conditions/releases),
46-
and put it somewhere in your project. Then do the following where you want to use the library:
47-
48-
```php
49-
<?php
50-
require_once __DIR__.'/<path_where_it_has_been_extracted>/autoloader.php';
51-
```
52-
5342
## Getting Started
5443

5544
The combine conditions library for PHP is a tool which helps you to build dynamically structured (as tree or graph) conditions.
@@ -63,12 +52,12 @@ The library has been written in order to allows you to implement it to the right
6352
interfaces from the public API, you can build your own combine conditions structure and continue to execute it via the public
6453
API services.
6554

66-
The library is written to follow the PSR1/PSR2 coding standards.
55+
The library is written to follow the PSR12 coding standards.
6756

6857
### Main Library Entrance
6958

7059
For a simply use, you'll only need to use the following class: `\LogicTree\LogicTreeFacade`.
71-
This class allows you to use the mainly features of the library, as to build a combined conditions, to execute somes, and
60+
This class allows you to use the mainly features of the library, as to build a combined conditions, to execute some, and
7261
exports them.
7362

7463
### OperatorPool
@@ -113,24 +102,24 @@ The library allows you to override the default operators and/or to provide new o
113102
$operatorPool = new \LogicTree\Operator\OperatorPool();
114103

115104
$operatorPool->addOperator(
116-
\LogicTree\Operator\OperatorPool::TYPE_LOGICAL,
105+
\LogicTree\Operator\OperatorType::Logical,
117106
\LogicTree\Operator\Logical\AndOperator::CODE,
118107
new \My\Class\AndOperator()
119108
);
120109
$operatorPool->addOperator(
121-
\LogicTree\Operator\OperatorPool::TYPE_COMPARATOR,
110+
\LogicTree\Operator\OperatorType::Comparator,
122111
\LogicTree\Operator\Comparator\EqOperator::CODE,
123112
new \My\Class\EqOperator()
124113
);
125114
$operatorPool->addOperator(
126-
\LogicTree\Operator\OperatorPool::TYPE_COMPARATOR,
115+
\LogicTree\Operator\OperatorType::Comparator,
127116
'my_custom_operator',
128117
new \My\Class\MyCustomOperator()
129118
);
130119

131120
$conditionManager = new \LogicTree\Service\ConditionManager($operatorPool);
132121
```
133-
Now we are able to use these operators in our code and are availables everywhere in the library.
122+
Now we are able to use these operators in our code and are available everywhere in the library.
134123

135124
### Condition and Combine
136125

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525
},
2626
"prefer-stable": true,
2727
"require": {
28-
"php": "^8.0"
28+
"php": "^8.1"
2929
},
3030
"require-dev": {
3131
"phpunit/phpunit": "^9.5",
32-
"phpmd/phpmd": "*",
33-
"sebastian/phpcpd": "*",
34-
"squizlabs/php_codesniffer": "*",
35-
"friendsofphp/php-cs-fixer": "*",
36-
"pdepend/pdepend": "*",
37-
"phpstan/phpstan": "*"
32+
"phpmd/phpmd": "^2.11",
33+
"sebastian/phpcpd": "^6.0",
34+
"squizlabs/php_codesniffer": "^3.6",
35+
"friendsofphp/php-cs-fixer": "^3.4",
36+
"pdepend/pdepend": "^2.1",
37+
"phpstan/phpstan": "^1.3"
3838
},
3939
"autoload": {
4040
"psr-4": {

examples/index.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use LogicTree\Node\Condition;
1212
use LogicTree\Operator\OperatorInterface;
1313
use LogicTree\Operator\OperatorPool;
14+
use LogicTree\Operator\OperatorType;
1415

1516
/**
1617
* Class CustomOperator
@@ -40,7 +41,7 @@ public function execute(...$expressions): bool
4041
$logicTree = new Combine('and', false, [$expr1, $expr2]);
4142

4243
// Add new operator
43-
$logicTreeFacade->addOperator(OperatorPool::TYPE_COMPARATOR, 'custom_op', new CustomOperator());
44+
$logicTreeFacade->addOperator(OperatorType::Comparator, 'custom_op', new CustomOperator());
4445

4546
// Execute combine conditions
4647
var_dump($logicTreeFacade->executeCombineConditions($logicTree, $dataSource));

src/DataSource.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* Copyright © Thomas Klein, All rights reserved.
44
* See LICENSE bundled with this library for license details.
55
*/
6-
declare(strict_types=1);
76

87
namespace LogicTree;
98

src/LogicTreeFacade.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* Copyright © Thomas Klein, All rights reserved.
44
* See LICENSE bundled with this library for license details.
55
*/
6-
declare(strict_types=1);
76

87
namespace LogicTree;
98

109
use LogicTree\Node\NodeInterface;
1110
use LogicTree\Operator\OperatorInterface;
1211
use LogicTree\Operator\OperatorPool;
12+
use LogicTree\Operator\OperatorType;
1313
use LogicTree\Service\ConditionManager;
1414

1515
class LogicTreeFacade
1616
{
17-
private ConditionManager $conditionManager;
18-
19-
private OperatorPool $operatorPool;
20-
21-
public function __construct(?ConditionManager $conditionManager = null, ?OperatorPool $operatorPool = null)
22-
{
17+
public function __construct(
18+
private ?ConditionManager $conditionManager = null,
19+
private ?OperatorPool $operatorPool = null
20+
) {
2321
$this->operatorPool = $operatorPool ?? new OperatorPool();
2422
$this->conditionManager = $conditionManager ?? new ConditionManager($this->operatorPool);
2523
}
2624

27-
public function addOperator(string $type, string $operatorCode, OperatorInterface $operator): LogicTreeFacade
25+
public function addOperator(OperatorType $type, string $operatorCode, OperatorInterface $operator): LogicTreeFacade
2826
{
2927
$this->operatorPool->addOperator($type, $operatorCode, $operator);
28+
3029
return $this;
3130
}
3231

src/Node/Combine.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@ final class Combine extends AbstractNode implements CombineInterface
1414
/**
1515
* @var NodeInterface[]
1616
*/
17-
private array $nodes;
17+
private array $nodes = [];
1818

19-
/**
20-
* @param string $operator
21-
* @param bool $isInvert [optional] Is false by default.
22-
* @param NodeInterface[] $children [optional] Is empty by default.
23-
*/
2419
public function __construct(
2520
private string $operator,
2621
private bool $isInvert = false,
27-
private array $children = []
22+
array $children = []
2823
) {
2924
$this->setOperator($operator);
3025
$this->setIsInvert($isInvert);

src/Node/Condition.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,10 @@
88

99
final class Condition extends AbstractNode implements ConditionInterface
1010
{
11-
/**
12-
* @var string
13-
*/
14-
private $operator;
15-
16-
/**
17-
* @var string
18-
*/
19-
private $valueIdentifier;
20-
21-
/**
22-
* @var mixed
23-
*/
24-
private $valueCompare;
25-
2611
public function __construct(
27-
string $valueIdentifier,
28-
string $operator,
29-
$valueCompare
12+
private string $valueIdentifier,
13+
private string $operator,
14+
private mixed $valueCompare
3015
) {
3116
$this->setValueIdentifier($valueIdentifier);
3217
$this->setOperator($operator);
@@ -59,7 +44,7 @@ public function setValueIdentifier(string $identifier): ConditionInterface
5944

6045
public function getValueCompare()
6146
{
62-
return $this->valueCompare;
47+
return $this->valueCompare;//ToDo: study to be fetched from datasource also?
6348
}
6449

6550
public function setValueCompare($value): ConditionInterface

src/Operator/OperatorPool.php

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -41,86 +41,73 @@ final class OperatorPool
4141
public const TYPE_LOGICAL = 'logical';
4242
public const TYPE_COMPARATOR = 'comparator';
4343

44-
/**
45-
* Default operators code and class name listed by types
46-
*
47-
* @var array
48-
*/
49-
private const DEFAULT_OPERATORS = [
50-
OperatorPool::TYPE_COMPARATOR => [
51-
EmptyOperator::CODE => EmptyOperator::class,
52-
EqOperator::CODE => EqOperator::class,
53-
GteqOperator::CODE => GteqOperator::class,
54-
GtOperator::CODE => GtOperator::class,
55-
IdenOperator::CODE => IdenOperator::class,
56-
InIdenOperator::CODE => InIdenOperator::class,
57-
InOperator::CODE => InOperator::class,
58-
LteqOperator::CODE => LteqOperator::class,
59-
LtOperator::CODE => LtOperator::class,
60-
NeqOperator::CODE => NeqOperator::class,
61-
NidenOperator::CODE => NidenOperator::class,
62-
NinIdenOperator::CODE => NinIdenOperator::class,
63-
NinOperator::CODE => NinOperator::class,
64-
NotNullOperator::CODE => NotNullOperator::class,
65-
NullOperator::CODE => NullOperator::class,
66-
RegexpOperator::CODE => RegexpOperator::class,
67-
],
68-
OperatorPool::TYPE_LOGICAL => [
69-
AndOperator::CODE => AndOperator::class,
70-
OrOperator::CODE => OrOperator::class,
71-
XorOperator::CODE => XorOperator::class,
72-
NandOperator::CODE => NandOperator::class,
73-
NorOperator::CODE => NorOperator::class,
74-
XnorOperator::CODE => XnorOperator::class,
75-
],
76-
];
77-
7844
/**
7945
* @var array[\LogicTree\Operator\OperatorInterface[]]
8046
*/
81-
private $operators = [];
47+
private array $operators = [];
48+
49+
public static function defaultOperators(): array
50+
{
51+
return [
52+
OperatorType::Comparator->value => [
53+
EmptyOperator::CODE => EmptyOperator::class,
54+
EqOperator::CODE => EqOperator::class,
55+
GteqOperator::CODE => GteqOperator::class,
56+
GtOperator::CODE => GtOperator::class,
57+
IdenOperator::CODE => IdenOperator::class,
58+
InIdenOperator::CODE => InIdenOperator::class,
59+
InOperator::CODE => InOperator::class,
60+
LteqOperator::CODE => LteqOperator::class,
61+
LtOperator::CODE => LtOperator::class,
62+
NeqOperator::CODE => NeqOperator::class,
63+
NidenOperator::CODE => NidenOperator::class,
64+
NinIdenOperator::CODE => NinIdenOperator::class,
65+
NinOperator::CODE => NinOperator::class,
66+
NotNullOperator::CODE => NotNullOperator::class,
67+
NullOperator::CODE => NullOperator::class,
68+
RegexpOperator::CODE => RegexpOperator::class,
69+
],
70+
OperatorType::Logical->value => [
71+
AndOperator::CODE => AndOperator::class,
72+
OrOperator::CODE => OrOperator::class,
73+
XorOperator::CODE => XorOperator::class,
74+
NandOperator::CODE => NandOperator::class,
75+
NorOperator::CODE => NorOperator::class,
76+
XnorOperator::CODE => XnorOperator::class,
77+
],
78+
];
79+
}
8280

8381
public function __construct(array $operators = [])
8482
{
8583
$typeOperators = array_merge_recursive($this->retrieveDefaultOperators(), $operators);
8684

8785
foreach ($typeOperators as $type => $operatorList) {
8886
foreach ($operatorList as $operatorCode => $operator) {
89-
$this->addOperator($type, $operatorCode, $operator);
87+
$this->addOperator(OperatorType::from($type), $operatorCode, $operator);
9088
}
9189
}
9290
}
9391

94-
public function getOperator(string $type, string $operatorCode): OperatorInterface
92+
public function getOperator(OperatorType $type, string $operatorCode): OperatorInterface
9593
{
96-
if (!isset($this->operators[$type][$operatorCode])) {
97-
throw new LogicException(
98-
sprintf('No registered operator for the type "%s" and code "%s".', $type, $operatorCode)
99-
);
100-
}
101-
102-
return $this->operators[$type][$operatorCode];
94+
return $this->operators[$type->value][$operatorCode] ?? throw new LogicException(
95+
sprintf('No registered operator for the type "%s" and code "%s".', $type, $operatorCode)
96+
);
10397
}
10498

105-
public function addOperator(string $type, string $operatorCode, OperatorInterface $operator): OperatorPool
99+
public function addOperator(OperatorType $type, string $operatorCode, OperatorInterface $operator): OperatorPool
106100
{
107-
if (!isset($this->operators[$type][$operatorCode])) {
108-
if (!isset($this->operators[$type])) {
109-
$this->operators[$type] = [];
110-
}
111-
112-
$this->operators[$type][$operatorCode] = $operator;
113-
}
101+
$this->operators[$type->value][$operatorCode] = $operator;
114102

115103
return $this;
116104
}
117105

118106
private function retrieveDefaultOperators(): array
119107
{
120-
return array_map(static function ($operators) {
121-
return array_map(static function ($operator) {
122-
return new $operator();
123-
}, $operators);
124-
}, self::DEFAULT_OPERATORS);
108+
return array_map(
109+
static fn ($operators) => array_map(static fn ($operator) => new $operator(), $operators),
110+
static::defaultOperators()
111+
);
125112
}
126113
}

src/Operator/OperatorType.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
*/
5+
6+
namespace LogicTree\Operator;
7+
8+
enum OperatorType: string
9+
{
10+
case Comparator = 'comparator';
11+
12+
case Logical = 'logical';
13+
}

0 commit comments

Comments
 (0)