/** * Handle a knit include * @return string * @throws KnitException */ public function parseIncludeKnit() : string { // exp: include knit="file.knit" $pieces = preg_split('/\\s/', $this->token); $knit = substr($pieces[1], 6, -1); // Return parsed template file try { return (new Template($this->compiler, $knit))->getParsed(); } catch (KnitException $e) { throw KnitException::parseError(sprintf('%1$s included in "%2$s" on line # %4$d', $e->getMessage(), basename($this->file), $this->file, $this->lineNum)); } }
/** * @param array $args * @return array * @throws KnitException */ protected function assertArgs(array $args) : array { // Count passed argument $numArgs = count($args); // Minimum number of arguments if (static::MIN_ARGS > $numArgs) { throw KnitException::parseError(sprintf('expects minimum of %d args, %d passed', static::MIN_ARGS, $numArgs)); } // Maximum number of arguments if (static::MAX_ARGS < $numArgs) { throw KnitException::parseError(sprintf('expects maximum of %d args, %d passed', static::MAX_ARGS, $numArgs)); } // Typecast $asserted = []; foreach (static::ARGS as $index => $arg) { // Check if arg. was passed if (!array_key_exists($index, $args)) { // Not passed, check if default value is available if (!array_key_exists(1, $arg)) { // No default value, throw exception throw KnitException::parseError(sprintf('missing argument %d', $index + 1)); } else { // Set default value $value = $arg[1]; } } else { // Passed value $value = $args[$index]; } // Check if casts match $valueType = $this->getType($value); if ($arg[0] !== "~") { if ($valueType === "str" && preg_match('/^\\$[a-z\\_][a-z0-9\\_\\[\\]\\"\'\\-\\>]+$/i', $value)) { // Variable, ...do nothing? } else { $value = call_user_func($arg[0] . "val", $value); // Cast } } $asserted[] = $value; } // Return args return $asserted; }
/** * @param string $message * @throws KnitException */ private function throwException(string $message) { throw KnitException::parseError(sprintf('Parsing error "%1$s" in template file "%2$s" on line # %4$d near "%5$s"', $message, basename($this->file), $this->file, $this->lineNum, substr($this->token, 0, 16) . "...")); }