3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 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\Http
;
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\ViewModel
;
18 class CreateViewModelListener
extends AbstractListenerAggregate
23 public function attach(Events
$events)
25 $this->listeners
[] = $events->attach('dispatch', array($this, 'createViewModelFromArray'), -80);
26 $this->listeners
[] = $events->attach('dispatch', array($this, 'createViewModelFromNull'), -80);
30 * Inspect the result, and cast it to a ViewModel if an assoc array is detected
35 public function createViewModelFromArray(MvcEvent
$e)
37 $result = $e->getResult();
38 if (!ArrayUtils
::hasStringKeys($result, true)) {
42 $model = new ViewModel($result);
43 $e->setResult($model);
47 * Inspect the result, and cast it to a ViewModel if null is detected
52 public function createViewModelFromNull(MvcEvent
$e)
54 $result = $e->getResult();
55 if (null !== $result) {
59 $model = new ViewModel
;
60 $e->setResult($model);