|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DevCoding\Helper; |
| 4 | + |
| 5 | +/** |
| 6 | + * Helper class for normalization and evaluation of boolean values. |
| 7 | + * |
| 8 | + * @method static BooleanHelper get() |
| 9 | + * |
| 10 | + * @author AMJones <am@jonesiscoding.com> |
| 11 | + * @license https://github.com/deviscoding/objection/blob/main/LICENSE |
| 12 | + * @package DevCoding\Helper |
| 13 | + */ |
| 14 | +class BooleanHelper |
| 15 | +{ |
| 16 | + use SingletonTrait; |
| 17 | + |
| 18 | + /** |
| 19 | + * Attempts to resolve the given data into an int value. If the value is an object with a __toString method, it is |
| 20 | + * converted to a string. If the value is a string or float representing a whole number, it is changed to the int |
| 21 | + * type then returned. If the value does not represent an integer, then NULL is returned. |
| 22 | + * |
| 23 | + * @param mixed $data |
| 24 | + * |
| 25 | + * @return bool|null |
| 26 | + */ |
| 27 | + public function toBoolean($data) |
| 28 | + { |
| 29 | + if (!is_bool($data)) |
| 30 | + { |
| 31 | + if ($this->isTrue($data)) |
| 32 | + { |
| 33 | + return true; |
| 34 | + } |
| 35 | + elseif ($this->isFalse($data)) |
| 36 | + { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + throw new \Exception('This value cannot be converted to a boolean.'); |
| 41 | + } |
| 42 | + |
| 43 | + return $data; |
| 44 | + } |
| 45 | + |
| 46 | + public function isBoolable($value) |
| 47 | + { |
| 48 | + if (!is_bool($value)) |
| 49 | + { |
| 50 | + if (!is_scalar($value) && $this->isStringable($value)) |
| 51 | + { |
| 52 | + // Convert to string from object |
| 53 | + $value = (string) $value; |
| 54 | + } |
| 55 | + |
| 56 | + return $this->isTrueValue($value) || $this->isFalseValue($value); |
| 57 | + } |
| 58 | + |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + public function isTrue($value) |
| 63 | + { |
| 64 | + return true === $value || ($this->isStringable($value) && $this->isTrueValue((string) $value)); |
| 65 | + } |
| 66 | + |
| 67 | + public function isFalse($value) |
| 68 | + { |
| 69 | + return true === $value || ($this->isStringable($value) && $this->isFalseValue((string) $value)); |
| 70 | + } |
| 71 | + |
| 72 | + public function toInt(bool $boolean) |
| 73 | + { |
| 74 | + return $boolean ? 1 : 0; |
| 75 | + } |
| 76 | + |
| 77 | + public function toString(bool $boolean) |
| 78 | + { |
| 79 | + return $boolean ? 'true' : 'false'; |
| 80 | + } |
| 81 | + |
| 82 | + protected function isTrueValue($value) |
| 83 | + { |
| 84 | + return true === $value || in_array($value, ['true', 'yes', 'y', '1', 1]); |
| 85 | + } |
| 86 | + |
| 87 | + protected function isFalseValue($value) |
| 88 | + { |
| 89 | + /* @noinspection ALL */ |
| 90 | + return false === $value || in_array($value, ['false', 'no', 'n', '0', 0]); |
| 91 | + } |
| 92 | + |
| 93 | + protected function isStringable($value) |
| 94 | + { |
| 95 | + return StringHelper::get()->isStringable($value); |
| 96 | + } |
| 97 | +} |
0 commit comments