-
Notifications
You must be signed in to change notification settings - Fork 428
/
TrinaryLogic.php
153 lines (125 loc) · 2.97 KB
/
TrinaryLogic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php declare(strict_types = 1);
namespace PHPStan;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use function array_column;
use function max;
use function min;
/**
* @api
* @see https://en.wikipedia.org/wiki/Three-valued_logic
*/
class TrinaryLogic
{
private const YES = 1;
private const MAYBE = 0;
private const NO = -1;
/** @var self[] */
private static array $registry = [];
private function __construct(private int $value)
{
}
public static function createYes(): self
{
return self::create(self::YES);
}
public static function createNo(): self
{
return self::create(self::NO);
}
public static function createMaybe(): self
{
return self::create(self::MAYBE);
}
public static function createFromBoolean(bool $value): self
{
return self::create($value ? self::YES : self::NO);
}
private static function create(int $value): self
{
self::$registry[$value] ??= new self($value);
return self::$registry[$value];
}
public function yes(): bool
{
return $this->value === self::YES;
}
public function maybe(): bool
{
return $this->value === self::MAYBE;
}
public function no(): bool
{
return $this->value === self::NO;
}
public function toBooleanType(): BooleanType
{
if ($this->value === self::MAYBE) {
return new BooleanType();
}
return new ConstantBooleanType($this->value === self::YES);
}
public function and(self ...$operands): self
{
$operandValues = array_column($operands, 'value');
$operandValues[] = $this->value;
return self::create(min($operandValues));
}
public function or(self ...$operands): self
{
$operandValues = array_column($operands, 'value');
$operandValues[] = $this->value;
return self::create(max($operandValues));
}
public static function extremeIdentity(self ...$operands): self
{
if ($operands === []) {
throw new ShouldNotHappenException();
}
$operandValues = array_column($operands, 'value');
$min = min($operandValues);
$max = max($operandValues);
return self::create($min === $max ? $min : self::MAYBE);
}
public static function maxMin(self ...$operands): self
{
if ($operands === []) {
throw new ShouldNotHappenException();
}
$operandValues = array_column($operands, 'value');
return self::create(max($operandValues) > 0 ? max($operandValues) : min($operandValues));
}
public function negate(): self
{
return self::create(-$this->value);
}
public function equals(self $other): bool
{
return $this === $other;
}
public function compareTo(self $other): ?self
{
if ($this->value > $other->value) {
return $this;
} elseif ($other->value > $this->value) {
return $other;
}
return null;
}
public function describe(): string
{
static $labels = [
self::NO => 'No',
self::MAYBE => 'Maybe',
self::YES => 'Yes',
];
return $labels[$this->value];
}
/**
* @param mixed[] $properties
*/
public static function __set_state(array $properties): self
{
return self::create($properties['value']);
}
}