ChildViewRenderer::renderChild()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
namespace SpeckCatalog\View\Helper;
3
4
use Zend\View\Helper\HelperInterface;
5
use Zend\View\Model\ViewModel;
6
use Zend\View\Helper\AbstractHelper;
7
use SpeckCatalog\Service\FormServiceAwareInterface;
8
9
/*
10
 * loops through an array of model objects and renders the view for each
11
 */
12
class ChildViewRenderer extends AbstractHelper
13
{
14
    // @todo replace with option
15
    protected $partialDir = '/speck-catalog/catalog-manager/partial/';
16
17
    public function __invoke($name, $views = array())
18
    {
19
        $html = '';
20
        if (!is_array($views) || count($views) === 0) {
21
            return $html;
22
        }
23
        foreach ($views as $view) {
24
            $html .= $this->renderChild($name, $view);
25
        }
26
        return $html;
27
    }
28
29
    public function renderChild($name, $data)
30
    {
31
        $child = new ViewModel(array($this->camel($name) => $data));
32
        $child->setTemplate($this->templateName($name));
33
        return $this->getView()->render($child);
34
    }
35
36
    public function templateName($name)
37
    {
38
        if ($name === 'product') {
39
            return $this->partialDir . 'product-clip';
40
        } else {
41
            return $this->partialDir . $this->dash($name);
42
        }
43
    }
44
45
    public function camel($name)
46
    {
47
        $camel = new \Zend\Filter\Word\UnderscoreToCamelCase;
48
        return lcfirst($camel->__invoke($name));
49
    }
50
51
    public function dash($name)
52
    {
53
        $dash = new \Zend\Filter\Word\UnderscoreToDash;
54
        return $dash->__invoke($name);
55
    }
56
}
57