|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Larium\Specification; |
|
6
|
|
|
|
|
7
|
|
|
abstract class ValueBoundSpecification extends LeafSpecification |
|
8
|
|
|
{ |
|
9
|
|
|
private $attribute; |
|
10
|
|
|
|
|
11
|
|
|
private $value; |
|
12
|
|
|
|
|
13
|
5 |
|
public function __construct(string $attribute, $value) |
|
14
|
|
|
{ |
|
15
|
5 |
|
$this->attribute = $attribute; |
|
16
|
5 |
|
$this->value = $value; |
|
17
|
5 |
|
} |
|
18
|
|
|
|
|
19
|
5 |
|
public function getAttribute() |
|
20
|
|
|
{ |
|
21
|
5 |
|
return $this->attribute; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
5 |
|
public function getValue() |
|
25
|
|
|
{ |
|
26
|
5 |
|
return $this->value; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
public function isGeneralizationOf(ValueBoundSpecification $specification): bool |
|
30
|
|
|
{ |
|
31
|
1 |
|
return $this->subsumptionProcess('isGeneralizationOf', $specification); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
public function isSpecialCaseOf(ValueBoundSpecification $specification): bool |
|
35
|
|
|
{ |
|
36
|
2 |
|
return $this->subsumptionProcess('isSpecialCaseOf', $specification); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
3 |
|
private function subsumptionProcess(string $type, ValueBoundSpecification $specification): bool |
|
40
|
|
|
{ |
|
41
|
3 |
|
$parentClasses = $this->getBaseNameOfParentClasses(get_class($specification)); |
|
42
|
|
|
|
|
43
|
3 |
|
foreach ($parentClasses as $name) { |
|
44
|
3 |
|
$method = sprintf('%s%s', $type, $name); |
|
45
|
|
|
|
|
46
|
3 |
|
if (is_callable([$this, $method])) { |
|
47
|
3 |
|
return $this->$method($specification); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
3 |
|
private function getBaseNameOfParentClasses(string $specificationClass): array |
|
55
|
|
|
{ |
|
56
|
3 |
|
$parents = class_parents($specificationClass); |
|
57
|
3 |
|
array_unshift($parents, $this->getClassBaseName($specificationClass)); |
|
58
|
|
|
|
|
59
|
3 |
|
return array_map([$this, 'getClassBaseName'], $parents); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
private function getClassBaseName(string $className): string |
|
63
|
|
|
{ |
|
64
|
3 |
|
$parts = explode('\\', $className); |
|
65
|
|
|
|
|
66
|
3 |
|
return end($parts); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|