/** * test a parameter * * @param string $type type name to validate * @param mixed $actual data to validate * * @return string|boolean If the test passes, it returns type name. Otherwise, returns FALSE. */ private static function _testType($type, $actual) { switch ($type) { case 'string': return is_string($actual) ? 'string' : FALSE; case 'array': return is_array($actual) ? 'array' : FALSE; case 'int': return is_numeric($actual) ? 'integer' : FALSE; case 'integer': return is_numeric($actual) ? 'integer' : FALSE; case 'float': return is_numeric($actual) ? 'float' : FALSE; case 'bool': return Charcoal_ScalarTrait::is_bool($actual, TRUE) ? 'boolean' : FALSE; case 'boolean': return Charcoal_ScalarTrait::is_bool($actual, TRUE) ? 'boolean' : FALSE; case 'resource': return is_resource($actual) ? 'resource' : FALSE; case 'object': return is_object($actual) ? 'object' : FALSE; default: return $actual instanceof $type ? $type : FALSE; } }
/** * Get as boolean value * * @param array $data array data * @param string $key key string for hash map * @param bool $default_value default value * * @return Charcoal_Boolean|NULL */ public static function getBoolean($data, $key, $default_value = NULL) { $value = isset($data[$key]) ? $data[$key] : NULL; // return default value if the element is null if (NULL === $value) { return NULL === $default_value ? NULL : b($default_value); } // cast to boolean value if (is_scalar($value)) { $value = Charcoal_ScalarTrait::boolVal($value); } // throws exception if the element's type is not match for required type if (!is_bool($value) && !$value instanceof Charcoal_Boolean) { _throw(new Charcoal_BooleanFormatException($key)); } return b($value); }