/** * 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 (!IsAssocArray::test($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, $filter = null) { if (!IsAssocArray::test($array)) { return false; //throw new \Exception('Error: ZfcBase\Model\ModelAbstract::fromArray expects associative array.'); } $classMethods = get_class_methods(get_called_class()); $model = new static(); foreach ($array as $key => $value) { $setter = static::fieldToSetterMethod($key); if (is_callable(array($model, $setter))) { if (is_callable($filter)) { $value = $filter($value); } $model->{$setter}($value); } } return $model; }
/** * Execute the request * * @param MvcEvent $e * @return mixed */ public function execute(MvcEvent $e) { $routeMatch = $e->getRouteMatch(); if (!$routeMatch) { /** * @todo Determine requirements for when route match is missing. * Potentially allow pulling directly from request metadata? */ throw new \DomainException('Missing route matches; unsure how to retrieve action'); } $action = $routeMatch->getParam('action', 'index'); $method = static::getMethodFromAction($action); if (!method_exists($this, $method)) { $method = 'notFoundAction'; } $actionResponse = $this->{$method}(); if (!is_object($actionResponse)) { if (IsAssocArray::test($actionResponse)) { $actionResponse = new ArrayObject($actionResponse, ArrayObject::ARRAY_AS_PROPS); } } $e->setResult($actionResponse); return $actionResponse; }
/** * 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('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('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('dispatch.error', $error); if (count($results)) { $return = $results->last(); } else { $return = $error->getParams(); } } complete: if (!is_object($return)) { if (IsAssocArray::test($return)) { $return = new ArrayObject($return, ArrayObject::ARRAY_AS_PROPS); } } $e->setResult($return); return $return; }
/** * @dataProvider invalidAssocArrays */ public function testInvalidAssocEmptyArraysReturnFalse($test) { $this->assertFalse(IsAssocArray::test($test, true)); }