/** * Is the next thing a speudo filter? * * @return false|CssParserFilterPseudo */ protected function pseudoFilter() { if (!$this->match("/^\\:/")) { return false; } if (!(list($name) = $this->is("identifier"))) { throw new TextParserException("Invalid identifier", $this); } $filter = ArrHelper::get($this->_pseudoFilters, $name, null); if ($filter === null) { throw new TextParserException("Unknown pseudo-filter", $this); } $input = ""; if ($this->eq("(")) { if (!($input = $this->is($filter["entity"]))) { throw new TextParserException("Invalid input", $this); } if (is_array($input)) { $input = $input[0]; } if (!$this->eq(")")) { throw new TextParserException("Invalid expression", $this); } } $pseudoFilter = CssParserFilterPseudoFactory::getInstance($filter["classname"], $input, $filter["user_def_function"]); return $pseudoFilter; }
/** * Fetches elements from an array. * * This function is especially suitable for getting optional arguments. * * For example: * ```php * function test($title, $message, $x, $y, $options) { * $args = ArrHelper::fetch(func_get_args(), array( * // `title` is a required string * "title" => "string", * // `message` is an optional string and has a default value * "message" => array( * "type" => "string", * "default" => "Default message ..." * ), * // `x` is an optional string or number and has a default value * "x" => array( * "type" => "string|number", * "default" => 0 * ), * // `y` is an optional string or number and has a default value * "y" => array( * "type" => "string|number", * "default" => 0 * ), * // `options` is an optional array * "options" => array( * "type" => "array", * required => false * ) * ); * print_r($args); * } * // this throws an InvalidArgumentException, as 'title' is required. * test(120, 250); * ``` * * @param array $arr Array of mixed elements * @param array $descriptors Associative array of descriptors. * * @throws InvalidArgumentException * @return array */ public static function fetch($arr, $descriptors) { $args = new ArrArguments($arr); foreach ($descriptors as $name => $descriptor) { $types = array(); $default = null; $required = false; if (is_string($descriptor)) { $types = explode("|", $descriptor); } elseif (is_array($descriptor)) { $types = explode("|", ArrHelper::get($descriptor, "type")); $default = ArrHelper::get($descriptor, "default"); $required = ArrHelper::get($descriptor, "required", !ArrHelper::is($descriptor, "default")); } $desc = new ArrArgumentsDescriptor($types, $default, $required); $args->registerDescriptor($name, $desc); } return $args->fetch(); }