/** * Renders values as JSON * * @todo Determine what use case exists for accepting only $nameOrModel * @param string|Model $nameOrModel The script/resource process, or a view model * @param null|array|\ArrayAccess $values Values to use during rendering * @throws Exception\InvalidArgumentException * @return string The script output. */ public function render($nameOrModel, $values = null) { if ($nameOrModel instanceof Model) { // Use case 1: View Model provided // Non-FeedModel: cast to FeedModel if (!$nameOrModel instanceof FeedModel) { $vars = $nameOrModel->getVariables(); $options = $nameOrModel->getOptions(); $type = $this->getFeedType(); if (isset($options['feed_type'])) { $type = $options['feed_type']; } else { $this->setFeedType($type); } $nameOrModel = new FeedModel($vars, array('feed_type' => $type)); } } elseif (is_string($nameOrModel)) { // Use case 2: string $nameOrModel + array|Traversable|Feed $values $nameOrModel = new FeedModel($values, (array) $nameOrModel); } else { // Use case 3: failure throw new Exception\InvalidArgumentException(sprintf('%s expects a ViewModel or a string feed type as the first argument; received "%s"', __METHOD__, is_object($nameOrModel) ? get_class($nameOrModel) : gettype($nameOrModel))); } // Get feed and type $feed = $nameOrModel->getFeed(); $type = $nameOrModel->getFeedType(); if (!$type) { $type = $this->getFeedType(); } else { $this->setFeedType($type); } // Render feed return $feed->export($type); }
/** * Recursively processes all ViewModels and returns output. * * @param string|ModelInterface $model A ViewModel instance. * @param null|array|\Traversable $values Values to use when rendering. If none * provided, uses those in the composed * variables container. * @return string Console output. */ public function render($model, $values = null) { if (!$model instanceof ModelInterface) { return ''; } $result = ''; $options = $model->getOptions(); foreach ($options as $setting => $value) { $method = 'set' . $setting; if (method_exists($this, $method)) { $this->{$method}($value); } unset($method, $setting, $value); } unset($options); $values = $model->getVariables(); if (isset($values['result'])) { // filter and append the result $result .= $this->getFilterChain()->filter($values['result']); } if ($model->hasChildren()) { // recursively render all children foreach ($model->getChildren() as $child) { $result .= $this->render($child, $values); } } return $result; }
/** * Recursively processes all ViewModels and returns output. * * @param string|ModelInterface $model A ViewModel instance. * @param null|array|\Traversable $values Values to use when rendering. If * none provided, uses those in the composed variables container. * @return string Console output. */ public function render($model, $values = null) { $result = ''; if (!$model instanceof ModelInterface) { // View model is required by this renderer return $result; } // If option keys match setters, pass values to those methods. foreach ($model->getOptions() as $setting => $value) { $method = 'set' . $setting; if (method_exists($this, $method)) { $this->{$method}($value); } } // Render children first if ($model->hasChildren()) { // recursively render all children foreach ($model->getChildren() as $child) { $result .= $this->render($child, $values); } } // Render the result, if present. $values = $model->getVariables(); if (isset($values['result']) && !isset($this->filterChain)) { // append the result verbatim $result .= $values['result']; } if (isset($values['result']) && isset($this->filterChain)) { // filter and append the result $result .= $this->getFilterChain()->filter($values['result']); } return $result; }