3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\View\Console
;
12 use Zend\EventManager\AbstractListenerAggregate
;
13 use Zend\EventManager\EventManagerInterface
as Events
;
14 use Zend\Mvc\MvcEvent
;
15 use Zend\Stdlib\ArrayUtils
;
16 use Zend\View\Model\ConsoleModel
;
18 class CreateViewModelListener
extends AbstractListenerAggregate
23 public function attach(Events
$events, $priority = 1)
25 $this->listeners
[] = $events->attach(MvcEvent
::EVENT_DISPATCH
, [$this, 'createViewModelFromString'], -80);
26 $this->listeners
[] = $events->attach(MvcEvent
::EVENT_DISPATCH
, [$this, 'createViewModelFromArray'], -80);
27 $this->listeners
[] = $events->attach(MvcEvent
::EVENT_DISPATCH
, [$this, 'createViewModelFromNull'], -80);
31 * Inspect the result, and cast it to a ViewModel if a string is detected
36 public function createViewModelFromString(MvcEvent
$e)
38 $result = $e->getResult();
39 if (!is_string($result)) {
43 // create Console model
44 $model = new ConsoleModel
;
46 // store the result in a model variable
47 $model->setVariable(ConsoleModel
::RESULT
, $result);
48 $e->setResult($model);
52 * Inspect the result, and cast it to a ViewModel if an assoc array is detected
57 public function createViewModelFromArray(MvcEvent
$e)
59 $result = $e->getResult();
60 if (!ArrayUtils
::hasStringKeys($result, true)) {
64 $model = new ConsoleModel($result);
65 $e->setResult($model);
69 * Inspect the result, and cast it to a ViewModel if null is detected
74 public function createViewModelFromNull(MvcEvent
$e)
76 $result = $e->getResult();
77 if (null !== $result) {
81 $model = new ConsoleModel
;
82 $e->setResult($model);