/** * @param $value * @return bool * @throws \Doctrineum\Boolean\Exceptions\UnexpectedValueToConvert */ protected function convertToEnumValue($value) { try { return ToBoolean::toBoolean($value, true); } catch (\Granam\Boolean\Tools\Exceptions\WrongParameterType $exception) { // wrapping exception by a local one throw new Exceptions\UnexpectedValueToConvert($exception->getMessage(), $exception->getCode(), $exception); } }
/** * @test */ public function I_can_use_to_string_object() { $toStringObject = new TestToBooleanToString($integerZero = 0); self::assertSame((bool) $integerZero, ToBoolean::toBoolean($toStringObject)); $toStringObject = new TestToBooleanToString($integer = 12345); self::assertSame((bool) $integer, ToBoolean::toBoolean($toStringObject)); $toStringObject = new TestToBooleanToString($stringInteger = '98765'); self::assertSame((bool) $stringInteger, ToBoolean::toBoolean($toStringObject)); $toStringObject = new TestToBooleanToString($float = 123.456); self::assertSame((bool) $float, ToBoolean::toBoolean($toStringObject)); $toStringObject = new TestToBooleanToString($stringFloat = '987.654'); self::assertSame((bool) $stringFloat, ToBoolean::toBoolean($toStringObject)); $toStringObject = new TestToBooleanToString($emptyString = ''); self::assertSame((bool) $emptyString, ToBoolean::toBoolean($toStringObject)); $toStringObject = new TestToBooleanToString($someString = 'foo'); self::assertSame((bool) $someString, ToBoolean::toBoolean($toStringObject)); }
private function normalizeValueForType($value, $type) { $value = trim($value); switch ($type) { case self::BOOLEAN: return ToBoolean::toBoolean($value, false); case self::INTEGER: return $value === '' ? false : ToInteger::toInteger($this->normalizeMinus($value)); case self::POSITIVE_INTEGER: return $value === '' ? false : ToInteger::toPositiveInteger($this->normalizeMinus($value)); case self::NEGATIVE_INTEGER: return $value === '' ? false : ToInteger::toNegativeInteger($this->normalizeMinus($value)); case self::FLOAT: return $value === '' ? false : ToFloat::toFloat($this->normalizeMinus($value)); default: // string return $value; } }
/** * @param mixed $value * @param bool $strict = true Can suppress raising of an exception on NULL */ public function __construct($value, $strict = true) { parent::__construct(ToBoolean::toBoolean($value, $strict)); }