1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\View\Helper;
use Zend\View\Exception;
use Zend\View\Model\ModelInterface as Model;
/**
* Helper for rendering child view models
*
* Finds children matching "capture-to" values, and renders them using the
* composed view instance.
*/
class RenderChildModel extends AbstractHelper
{
/**
* Current view model
*
* @var Model
*/
protected $current;
/**
* View model helper instance
*
* @var ViewModel
*/
protected $viewModelHelper;
/**
* Invoke as a function
*
* Proxies to {render()}.
*
* @param string $child
* @return string
*/
public function __invoke($child)
{
return $this->render($child);
}
/**
* Render a model
*
* If a matching child model is found, it is rendered. If not, an empty
* string is returned.
*
* @param string $child
* @return string
*/
public function render($child)
{
$model = $this->findChild($child);
if (! $model) {
return '';
}
$current = $this->current;
$view = $this->getView();
$return = $view->render($model);
$helper = $this->getViewModelHelper();
$helper->setCurrent($current);
return $return;
}
/**
* Find the named child model
*
* Iterates through the current view model, looking for a child model that
* has a captureTo value matching the requested $child. If found, that child
* model is returned; otherwise, a boolean false is returned.
*
* @param string $child
* @return false|Model
*/
protected function findChild($child)
{
$this->current = $model = $this->getCurrent();
foreach ($model->getChildren() as $childModel) {
if ($childModel->captureTo() == $child) {
return $childModel;
}
}
return false;
}
/**
* Get the current view model
*
* @throws Exception\RuntimeException
* @return null|Model
*/
protected function getCurrent()
{
$helper = $this->getViewModelHelper();
if (! $helper->hasCurrent()) {
throw new Exception\RuntimeException(sprintf(
'%s: no view model currently registered in renderer; cannot query for children',
__METHOD__
));
}
return $helper->getCurrent();
}
/**
* Retrieve the view model helper
*
* @return ViewModel
*/
protected function getViewModelHelper()
{
if ($this->viewModelHelper) {
return $this->viewModelHelper;
}
if (method_exists($this->getView(), 'plugin')) {
$this->viewModelHelper = $this->view->plugin('view_model');
}
return $this->viewModelHelper;
}
}