/** * @param AssetsOptions $options * @throws \RuntimeException */ public function setOptions($options) { if (!$options instanceof AssetsOptions && !ArrayUtils::hasStringKeys($options)) { throw new \RuntimeException("Options must be an array with string keys"); } if (is_array($options)) { $options = new AssetsOptions($options); } //Set the controller resolver $controllerLoader = $this->serviceLocator->get("ControllerLoader"); $controllerLoader->setInvokableClass("AssetsController", "Assets\\AssetsController"); //Set the route $router = $this->serviceLocator->get('Router'); $route = new Segment($options->getRoute(), array('asset' => '.*'), array('controller' => 'AssetsController', 'action' => 'index')); $router->addRoute("assets", $route); $filterManager = $this->serviceLocator->get('AssetsFilterManager'); foreach ($options->getFilters() as $asset => $filters) { foreach ($filters as $filter) { if (!$filterManager->has($filter)) { throw new \RuntimeException("Filter {$filter} does not exist"); } if (!isset($this->filters[$asset])) { $this->filters[$asset] = array(); } $this->filters[$asset][] = $filterManager->get($filter); } } foreach ($options->getPaths() as $path) { $this->getResolver()->addPath($path); } }
/** * Inspect the result, and cast it to a ViewModel if an assoc array is detected * * @param MvcEvent $e * @return void */ public function createViewModelFromArray(MvcEvent $e) { $result = $e->getResult(); if (!ArrayUtils::hasStringKeys($result, true)) { return; } $model = new ViewModel($result); $e->setResult($model); }
/** * Convert an array to an instance of a model class * * @param array $array * @return ZfcBase\Model\ModelAbstract */ public static function fromArray($array) { if (!ArrayUtils::hasStringKeys($array)) { throw new InvalidArgumentException('ModelAbstract::fromArray() expects associative array.'); } $model = new static(); $model->exchangeArray($array); return $model; }
public function testEmptyArrayReturnsFalse() { $test = array(); $this->assertFalse(ArrayUtils::hasStringKeys($test, false)); $this->assertFalse(ArrayUtils::hasIntegerKeys($test, false)); $this->assertFalse(ArrayUtils::hasNumericKeys($test, false)); $this->assertFalse(ArrayUtils::isList($test, false)); $this->assertFalse(ArrayUtils::isHashTable($test, false)); }
/** * Complete the dispatch * * @param mixed $return * @param MvcEvent $event * @return mixed */ protected function complete($return, MvcEvent $event) { if (!is_object($return)) { if (ArrayUtils::hasStringKeys($return)) { $return = new ArrayObject($return, ArrayObject::ARRAY_AS_PROPS); } } $event->setResult($return); return $return; }
/** * Determines if table will has a header row. * * A header row can be set explicitly using setHeaderRow() or by passing an * associative array for the table rows. * * @return bool */ public function hasHeaderRow() { // If no header row is provided, see if we can get headers from row keys. if (!isset($this->headerRow)) { return count($this->rows) ? ArrayUtils::hasStringKeys($this->rows[0]) : false; } return true; }
/** * Dispatch the matched route * * @param MvcEvent $e * @return mixed */ public function dispatch(MvcEvent $e) { $locator = $this->getLocator(); if (!$locator) { throw new Exception\MissingLocatorException('Cannot dispatch without a locator'); } $routeMatch = $e->getRouteMatch(); $controllerName = $routeMatch->getParam('controller', 'not-found'); $events = $this->events(); try { $controller = $locator->get($controllerName); } catch (ClassNotFoundException $exception) { $error = clone $e; $error->setError(static::ERROR_CONTROLLER_NOT_FOUND)->setController($controllerName)->setParam('exception', $exception); $results = $events->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $error); if (count($results)) { $return = $results->last(); } else { $return = $error->getParams(); } goto complete; } if ($controller instanceof LocatorAware) { $controller->setLocator($locator); } if (!$controller instanceof Dispatchable) { $error = clone $e; $error->setError(static::ERROR_CONTROLLER_INVALID)->setController($controllerName)->setControllerClass(get_class($controller)); $results = $events->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $error); if (count($results)) { $return = $results->last(); } else { $return = $error->getParams(); } goto complete; } $request = $e->getRequest(); $response = $this->getResponse(); if ($controller instanceof InjectApplicationEvent) { $controller->setEvent($e); } try { $return = $controller->dispatch($request, $response); } catch (\Exception $ex) { $error = clone $e; $error->setError(static::ERROR_EXCEPTION)->setController($controllerName)->setControllerClass(get_class($controller))->setParam('exception', $ex); $results = $events->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $error); if (count($results)) { $return = $results->last(); } else { $return = $error->getParams(); } } complete: if (!is_object($return)) { if (ArrayUtils::hasStringKeys($return)) { $return = new ArrayObject($return, ArrayObject::ARRAY_AS_PROPS); } } $e->setResult($return); return $return; }
/** * Inspect the result, and cast it to a ViewModel * * @param MvcEvent $e * @return void */ public function canonizeActionResult(MvcEvent $e) { $result = $e->getResult(); if ($result instanceof Response) { return; } // MVC controller $controller = $e->getTarget(); // Controller view $controllerVew = $controller->plugin('view'); // Collect captured contents if ($this->obStarted) { $content = ob_get_clean(); if ($content) { if (null === $result && !$controllerVew->hasViewModel()) { $result = $content; } elseif (Pi::service()->hasService('log')) { Pi::service('log')->info('Captured content: ' . $content); } } } // Skip scalar result if (!$this->type && is_scalar($result) && !is_bool($result)) { if ($controllerVew->hasViewModel()) { $viewModel = $controllerVew->getViewModel(); } else { $viewModel = new ViewModel(); } $viewModel->setTemplate('__NULL__'); $viewModel->setVariable($viewModel->captureTo(), $result); $e->setResult($viewModel); return; } // ViewModel generated by controller $viewModel = null; // Cast controller ViewModel if ($controllerVew->hasViewModel()) { $viewModel = $controllerVew->getViewModel(); $template = $viewModel->getTemplate(); // Controller ViewModel is as the main model if if if is specified // with template, MvcEvent result is converted to variables of // the ViewModel if ($viewModel instanceof ViewModel && !$viewModel instanceof JsonModel && !$viewModel instanceof FeedModel && $template && '__NULL__' != $template) { $variables = array(); $options = array(); if ($result instanceof ViewModel) { $variables = $result->getVariables(); $options = $result->getOptions(); } elseif ($result instanceof FeedDataModel) { $variables = (array) $result; $options = array('feed_type' => $result->getType()); } elseif (ArrayUtils::hasStringKeys($result, true)) { $variables = array_merge_recursive($variables, $result); } $viewModel->setVariables($variables); $viewModel->setOptions($options); $e->setResult($viewModel); return; } } // Set type to json if no template is specified if (!$this->type && null !== $result) { $skip = false; if ($result instanceof ViewModel && $result->getTemplate() && $result->getTemplate() != '__NULL__' || $result instanceof JsonModel || $result instanceof FeedModel) { $skip = true; } if (!$skip) { $this->type = 'json'; Pi::service('log')->mute(); } } // Cast controller view model to result ViewModel switch ($this->type) { // For Feed case 'feed': if ($result instanceof FeedModel) { $model = $result; } else { $variables = array(); //$options = array(); if ($result instanceof ViewModel) { $variables = $result->getVariables(); $options = $result->getOptions(); } elseif ($result instanceof FeedDataModel) { $variables = (array) $result; $options = array('feed_type' => $result->getType()); } else { if ($viewModel) { $variables = $viewModel->getVariables(); //$options = $viewModel->getOptions(); } if (ArrayUtils::hasStringKeys($result, true)) { $variables = array_merge_recursive((array) $variables, (array) $result); } $model = new FeedDataModel($variables); $variables = (array) $model; $options = array('feed_type' => $model->getType()); } $model = new FeedModel($variables, $options); } break; // For Json // For Json case 'json': if ($result instanceof JsonModel) { $model = $result; } else { $options = array(); //$data = array(); if ($result instanceof ViewModel) { $data = $result->getVariables(); $options = $result->getOptions(); } elseif (ArrayUtils::hasStringKeys($result, true)) { if ($viewModel) { $variables = $viewModel->getVariables(); $options = $viewModel->getOptions(); } $data = array_merge_recursive((array) $variables, $result); } else { $data = $result; } $model = new JsonModel($data, $options); } break; // For AJAX/Flash // For AJAX/Flash case 'ajax': // MISC // MISC default: if ($viewModel) { $model = $viewModel; } elseif ($result instanceof ViewModel) { $model = $result; $result = null; } else { $model = new ViewModel(); } if (null !== $result) { $template = $model->getTemplate(); if ($this->type && (!$template || '__NULL_' == $template)) { $model->setVariable('content', is_scalar($result) ? $result : json_encode($result)); } elseif (ArrayUtils::hasStringKeys($result, true)) { $model->setVariables($result); } elseif (is_scalar($result)) { $model->setVariable('content', $result); } else { $model->setVariable('content', json_encode($result)); } } if ($this->type) { $model->terminate(true); } break; } $e->setResult($model); }