/** * Casts a string to the given type. * * @param string $value the value as string * @param int $type the type - any of <code>Input::TYPE_STRING</code>, * <code>Input::TYPE_INT</code>, <code>Input::TYPE_BOOL</code> or * <code>Input::TYPE_SERIALIZED</code>, * @return string number boolean array mixed null if the value could not be cast */ protected function typecast($value, $type) { switch ($type) { case self::TYPE_STRING: return $value; case self::TYPE_INT: if (TypeCheck::isInt($value)) { return (int) $value; } return null; case self::TYPE_FLOAT: if (TypeCheck::isFloat($value)) { return (double) $value; } return null; case self::TYPE_BOOL: if (is_bool($value)) { return $value; } switch ($value) { case 'true': case '1': return true; case 'false': case '0': return false; default: return null; } case self::TYPE_SERIALIZED: try { $decoded = base64_decode($value); return unserialize($decoded); } catch (ErrorException $e) { return null; } default: return null; } }
/** * Tests if a value is a float. * * @dataProvider isFloatProvider * @covers empire\framework\util\TypeCheck::isFloat * * @param mixed $value the value to check * @param boolean $res if it is an float */ public function testIsFloat($value, $res) { $this->assertSame($res, TypeCheck::isFloat($value)); }