/** * @param array|Traversable $config */ public function fromConfig($config) { $params = Param\Node::cast($config); foreach ($params as $key => $node) { $this->setSeed($key, $node->getData()); } return $this; }
/** * @param type $config * @return static */ public static function factory($config) { $route = new static(); $param = Param\Node::cast($config); $route->setSource($param->value('subject')); $route->setPattern($param->value('segment', $param->value('pattern'))); $route->setConstraints($param->value('constraints')); $route->setDefaults($param->value('defaults', $param->value('params'))); if ($param->value('children')) { $route->setPartial(true); } return $route; }
/** * Construct the chain from a configuration array/object * @param array|Config $config * @return static */ public static function factory($config, Route $base = null) { $chain = new static($base); $spec = Config::cast($config); // foreach ($spec as $name => $node) { $prio = $node->value('priority', 0); $children = (array) $node->value('children'); $route = static::element($node); if ($children) { $route = static::factory($children, $route); } $chain->addChild($name, $route, $prio); } return $chain; }
/** * @param mixed $value * @param bool $force * @param bool $silent * @return int Number of errors */ public function filterValue($value, $force = false, $silent = false) { $status = 0; $filtered = $value; // foreach ($this->getFilters(true) as $spec) { $filtered = $this->applyFilter($value, $spec); $status = $this->filterErrorCount($filtered, $spec['options']); if ($status > 0) { $template = $spec['message'] ?: "Invalid data in <{field.name}>"; if (!$silent) { $json = json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $model = Params::cast(['field' => $this, 'data' => $value, 'serialized' => $json]); $this->addError($spec['errno'], $model->render($template)); } break; } } if ($status == 0 || $force) { $this->setValue($filtered); } return $status; }
/** * Renders the whole model * * @param Model|array $model * @return string */ public function render($model) { return $this->renderItem(Model::cast($model)); }
/** * @param Param\Node $defaults * @return static */ public function setDefaults($defaults) { $this->defaults = Param\Node::cast($defaults); return $this; }
/** * @param array|Params $params * @return static */ public function setParams($params) { if ($params !== null) { $params = Params::cast($params); $params->setDelimiters(['{', '}']); } $this->params = $params; return $this; }
/** * @param array|Param\Node $config * @return static */ public function setConfig($config) { $this->setResource(self::CONFIG, Param\Node::cast($config)); return $this; }
/** * @param string $source * @param Data $data */ public function render($source, $data, $referer = null) { $compiler = $this->getCompiler(); $name = ltrim($referer, '/'); $key = static::INLINE; // $this->reset(); $this->outPush(); $program = $compiler->compile($source, $name); $this->sources[$key] = $source; $this->programs[$key] = $program; $model = Data::cast($data, $this->dataOptions()); $this->execProgram($key, $this->prepareData($key, $model)); $this->cleanupData($key, $model); $out = $this->outPop(); // return $out; }