1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Stratadox\Specification; |
6
|
|
|
|
7
|
|
|
use Stratadox\Specification\Binary\AndSpecification; |
8
|
|
|
use Stratadox\Specification\Binary\OrSpecification; |
9
|
|
|
use Stratadox\Specification\Binary\XorSpecification; |
10
|
|
|
use Stratadox\Specification\Contract\Satisfiable; |
11
|
|
|
use Stratadox\Specification\Contract\Specifies; |
12
|
|
|
use Stratadox\Specification\Unary\NotSpecification; |
13
|
|
|
|
14
|
|
|
trait Specifying |
15
|
|
|
{ |
16
|
|
|
/** @see Specifies::not() */ |
17
|
|
|
public function not() : Specifies |
18
|
|
|
{ |
19
|
|
|
/** @var Specifying|Satisfiable $this */ |
20
|
|
|
return $this->doesNotSatisfy($this); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** @see Specifies::and() */ |
24
|
|
|
public function and(Satisfiable $other) : Specifies |
25
|
|
|
{ |
26
|
|
|
/** @var Specifying|Satisfiable $this */ |
27
|
|
|
return $this->satisfiesBoth($this, $other); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** @see Specifies::or() */ |
31
|
|
|
public function or(Satisfiable $other) : Specifies |
32
|
|
|
{ |
33
|
|
|
/** @var Specifying|Satisfiable $this */ |
34
|
|
|
return $this->oneOfThese($this, $other); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @see Specifies::xor() */ |
38
|
|
|
public function xor(Satisfiable $other) : Specifies |
39
|
|
|
{ |
40
|
|
|
/** @var Specifying|Satisfiable $this */ |
41
|
|
|
return new XorSpecification($this, $other); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @see Specifies::nor() */ |
45
|
|
|
public function nor(Satisfiable $other) : Specifies |
46
|
|
|
{ |
47
|
|
|
return $this->not()->andNot($other); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @see Specifies::andNot() */ |
51
|
|
|
public function andNot(Satisfiable $other) : Specifies |
52
|
|
|
{ |
53
|
|
|
return $this->and($this->doesNotSatisfy($other)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @see Specifies::butNot() */ |
57
|
|
|
public function butNot(Satisfiable $other) : Specifies |
58
|
|
|
{ |
59
|
|
|
return $this->andNot($other); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @see Specifies::orNot() */ |
63
|
|
|
public function orNot(Satisfiable $other) : Specifies |
64
|
|
|
{ |
65
|
|
|
return $this->or($this->doesNotSatisfy($other)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
// Private |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
private function doesNotSatisfy(Satisfiable $condition) : Specifies |
73
|
|
|
{ |
74
|
|
|
return new NotSpecification($condition); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function satisfiesBoth(Satisfiable $firstCondition, Satisfiable $secondCondition) : Specifies |
78
|
|
|
{ |
79
|
|
|
return new AndSpecification($firstCondition, $secondCondition); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function oneOfThese(Satisfiable $firstCondition, Satisfiable $secondCondition) : Specifies |
83
|
|
|
{ |
84
|
|
|
return new OrSpecification($firstCondition, $secondCondition); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|