1: <?php
2:
3: namespace Chromabits\Nucleus\Meditation\Constraints;
4:
5: use Chromabits\Nucleus\Strings\Rope;
6:
7: 8: 9: 10: 11: 12:
13: class BooleanConstraint extends AbstractConstraint
14: {
15: 16: 17: 18: 19: 20: 21: 22:
23: public function check($value, array $context = [])
24: {
25: if (is_string($value) || $value instanceof Rope) {
26: $lower = Rope::of($value)->toLower();
27:
28: return $lower->equals(Rope::of('true'))
29: || $lower->equals(Rope::of('false'));
30: } elseif (is_int($value)) {
31: return $value === 0 || $value === 1;
32: } elseif (is_float($value)) {
33: return $value === 0.0 || $value === 1.0;
34: }
35:
36: return is_bool($value);
37: }
38:
39: 40: 41: 42: 43:
44: public function toString()
45: {
46: return '{boolean}';
47: }
48:
49: 50: 51:
52: public function getDescription()
53: {
54: return 'The value is expected to be boolean-like: ' .
55: '0, 1, 0.0, 1.0, true, false, True, False';
56: }
57: }
58: