Skip to content

Commit 9da6584

Browse files
committed
Add generics support
1 parent 4721070 commit 9da6584

7 files changed

Lines changed: 72 additions & 67 deletions

src/AndSpecification.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22

33
namespace Tanigami\Specification;
44

5+
/**
6+
* @template T
7+
* @extends Specification<T>
8+
*/
59
class AndSpecification extends Specification
610
{
711
/**
8-
* @var Specification
12+
* @var Specification<T>
913
*/
1014
private $one;
1115

1216
/**
13-
* @var Specification
17+
* @var Specification<T>
1418
*/
1519
private $other;
1620

1721
/**
18-
* @param Specification $one
19-
* @param Specification $other
22+
* @param Specification<T> $one
23+
* @param Specification<T> $other
2024
*/
2125
public function __construct(Specification $one, Specification $other)
2226
{
@@ -25,37 +29,32 @@ public function __construct(Specification $one, Specification $other)
2529
}
2630

2731
/**
28-
* {@inheritdoc}
32+
* @param T $object
2933
*/
3034
public function isSatisfiedBy($object): bool
3135
{
3236
return $this->one->isSatisfiedBy($object) && $this->other->isSatisfiedBy($object);
3337
}
3438

35-
/**
36-
* {@inheritdoc}
37-
*/
3839
public function whereExpression(string $alias): string
3940
{
4041
return sprintf(
41-
sprintf(
42-
'(%s) AND (%s)',
43-
$this->one()->whereExpression($alias),
44-
$this->other()->whereExpression($alias)
45-
)
42+
'(%s) AND (%s)',
43+
$this->one()->whereExpression($alias),
44+
$this->other()->whereExpression($alias)
4645
);
4746
}
4847

4948
/**
50-
* @return Specification
49+
* @return Specification<T>
5150
*/
5251
public function one(): Specification
5352
{
5453
return $this->one;
5554
}
5655

5756
/**
58-
* @return Specification
57+
* @return Specification<T>
5958
*/
6059
public function other(): Specification
6160
{

src/AnyOfSpecification.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22

33
namespace Tanigami\Specification;
44

5+
/**
6+
* @template T
7+
* @extends Specification<T>
8+
*/
59
class AnyOfSpecification extends Specification
610
{
711
/**
8-
* @var Specification[]
12+
* @var Specification<T>[]
913
*/
1014
private $specifications;
1115

1216
/**
13-
* @param Specification[] ...$specifications
17+
* @param Specification<T> ...$specifications
1418
*/
1519
public function __construct(Specification ...$specifications)
1620
{
1721
$this->specifications = $specifications;
1822
}
1923

2024
/**
21-
* {@inheritdoc}
25+
* @param T $object
2226
*/
2327
public function isSatisfiedBy($object): bool
2428
{
@@ -31,21 +35,18 @@ public function isSatisfiedBy($object): bool
3135
return true;
3236
}
3337

34-
/**
35-
* {@inheritdoc}
36-
*/
3738
public function whereExpression(string $alias): string
3839
{
3940
return implode(' AND ', array_map(
40-
function (Specification $specification) use ($alias) {
41+
static function (Specification $specification) use ($alias) {
4142
return '(' . $specification->whereExpression($alias) . ')';
4243
},
4344
$this->specifications
4445
));
4546
}
4647

4748
/**
48-
* @return Specification[]
49+
* @return Specification<T>[]
4950
*/
5051
public function specifications(): array
5152
{

src/NoneOfSpecification.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22

33
namespace Tanigami\Specification;
44

5+
/**
6+
* @template T
7+
* @extends Specification<T>
8+
*/
59
class NoneOfSpecification extends Specification
610
{
711
/**
8-
* @var Specification[]
12+
* @var Specification<T>[]
913
*/
1014
private $specifications;
1115

1216
/**
13-
* @param Specification[] ...$specifications
17+
* @param Specification<T> ...$specifications
1418
*/
1519
public function __construct(Specification ...$specifications)
1620
{
1721
$this->specifications = $specifications;
1822
}
1923

2024
/**
21-
* {@inheritdoc}
25+
* @param T $object
2226
*/
2327
public function isSatisfiedBy($object): bool
2428
{
@@ -32,10 +36,10 @@ public function isSatisfiedBy($object): bool
3236
}
3337

3438
/**
35-
* @return Specification[]
39+
* @return Specification<T>[]
3640
*/
3741
public function specifications(): array
3842
{
39-
return $this->specifications();
43+
return $this->specifications;
4044
}
4145
}

src/NotSpecification.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,35 @@
22

33
namespace Tanigami\Specification;
44

5+
/**
6+
* @template T
7+
* @extends Specification<T>
8+
*/
59
class NotSpecification extends Specification
610
{
711
/**
8-
* @var Specification
12+
* @var Specification<T>
913
*/
1014
private $specification;
1115

1216
/**
13-
* @param Specification $specification
17+
* @param Specification<T> $specification
1418
*/
1519
public function __construct(Specification $specification)
1620
{
1721
$this->specification = $specification;
1822
}
1923

2024
/**
21-
* @param mixed $object
22-
* @return bool
25+
* @param T $object
2326
*/
2427
public function isSatisfiedBy($object): bool
2528
{
2629
return !$this->specification->isSatisfiedBy($object);
2730
}
2831

2932
/**
30-
* @return Specification
33+
* @return Specification<T>
3134
*/
3235
public function specification(): Specification
3336
{

src/OneOfSpecification.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22

33
namespace Tanigami\Specification;
44

5+
/**
6+
* @template T
7+
* @extends Specification<T>
8+
*/
59
class OneOfSpecification extends Specification
610
{
711
/**
8-
* @var Specification[]
12+
* @var Specification<T>[]
913
*/
1014
private $specifications;
1115

1216
/**
13-
* @param Specification[] ...$specifications
17+
* @param Specification<T> ...$specifications
1418
*/
1519
public function __construct(Specification ...$specifications)
1620
{
1721
$this->specifications = $specifications;
1822
}
1923

2024
/**
21-
* {@inheritdoc}
25+
* @param T $object
2226
*/
2327
public function isSatisfiedBy($object): bool
2428
{
@@ -31,21 +35,18 @@ public function isSatisfiedBy($object): bool
3135
return false;
3236
}
3337

34-
/**
35-
* {@inheritdoc}
36-
*/
3738
public function whereExpression(string $alias): string
3839
{
3940
return implode(' OR ', array_map(
40-
function (Specification $specification) use ($alias) {
41+
static function (Specification $specification) use ($alias) {
4142
return '(' . $specification->whereExpression($alias) . ')';
4243
},
4344
$this->specifications
4445
));
4546
}
4647

4748
/**
48-
* @return Specification[]
49+
* @return Specification<T>[]
4950
*/
5051
public function specifications(): array
5152
{

src/OrSpecification.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22

33
namespace Tanigami\Specification;
44

5+
/**
6+
* @template T
7+
* @extends Specification<T>
8+
*/
59
class OrSpecification extends Specification
610
{
711
/**
8-
* @var Specification
12+
* @var Specification<T>
913
*/
1014
private $one;
1115

1216
/**
13-
* @var Specification
17+
* @var Specification<T>
1418
*/
1519
private $other;
1620

1721
/**
18-
* @param Specification $one
19-
* @param Specification $other
22+
* @param Specification<T> $one
23+
* @param Specification<T> $other
2024
*/
2125
public function __construct(Specification $one, Specification $other)
2226
{
@@ -25,37 +29,32 @@ public function __construct(Specification $one, Specification $other)
2529
}
2630

2731
/**
28-
* {@inheritdoc}
32+
* @param T $object
2933
*/
3034
public function isSatisfiedBy($object): bool
3135
{
3236
return $this->one->isSatisfiedBy($object) || $this->other->isSatisfiedBy($object);
3337
}
3438

35-
/**
36-
* {@inheritdoc}
37-
*/
3839
public function whereExpression(string $alias): string
3940
{
4041
return sprintf(
41-
sprintf(
42-
'(%s) OR (%s)',
43-
$this->one()->whereExpression($alias),
44-
$this->other()->whereExpression($alias)
45-
)
42+
'(%s) OR (%s)',
43+
$this->one()->whereExpression($alias),
44+
$this->other()->whereExpression($alias)
4645
);
4746
}
4847

4948
/**
50-
* @return Specification
49+
* @return Specification<T>
5150
*/
5251
public function one(): Specification
5352
{
5453
return $this->one;
5554
}
5655

5756
/**
58-
* @return Specification
57+
* @return Specification<T>
5958
*/
6059
public function other(): Specification
6160
{

src/Specification.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,41 @@
44

55
use BadMethodCallException;
66

7+
/**
8+
* @template T
9+
*/
710
abstract class Specification
811
{
912
/**
10-
* @param mixed $object
11-
* @return bool
13+
* @param T $object
1214
*/
1315
abstract public function isSatisfiedBy($object): bool;
1416

15-
/**
16-
* @param string $alias
17-
* @return string
18-
*/
1917
public function whereExpression(string $alias): string
2018
{
2119
throw new BadMethodCallException('Where expression is not supported');
2220
}
2321

2422
/**
25-
* @param Specification $specification
26-
* @return AndSpecification
23+
* @param Specification<T> $specification
24+
* @return AndSpecification<T>
2725
*/
2826
public function and(Specification $specification): AndSpecification
2927
{
3028
return new AndSpecification($this, $specification);
3129
}
3230

3331
/**
34-
* @param Specification $specification
35-
* @return OrSpecification
32+
* @param Specification<T> $specification
33+
* @return OrSpecification<T>
3634
*/
3735
public function or(Specification $specification): OrSpecification
3836
{
3937
return new OrSpecification($this, $specification);
4038
}
4139

4240
/**
43-
* @return NotSpecification
41+
* @return NotSpecification<T>
4442
*/
4543
public function not(): NotSpecification
4644
{

0 commit comments

Comments
 (0)