Completed
Pull Request — 2.0.x (#6)
by Andrew
04:08
created

calculateTemplateFromClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
namespace Ingenerator\KohanaView;
3
4
/**
5
 * The ViewTemplateSelector maps ViewModel classes to the appropriate template file. By default this is done by
6
 * converting the class name to a lowercased file path such that:
7
 *
8
 *   View_Model_Something_WithMixedCase => model/something/with_mixed_case
9
 *   \View\Stuff\In\NameSpace\Thing     => stuff/in/name_space/thing
10
 *   \Other\View\Hierarchy              => other/view/hierarchy
11
 *   \Other\View\HierarchyView          => other/view/hierarchy
12
 *   \Other\View\HierarchyViewModel     => other/view/hierarchy
13
 *
14
 * Views can also implement TemplateSpecifyingViewModel to provide a custom template file name when required.
15
 *
16
 * @author     Andrew Coulton <[email protected]>
17
 * @copyright  2015 inGenerator Ltd
18
 * @license    http://kohanaframework.org/license
19
 */
20
class ViewTemplateSelector
21
{
22
23
    /**
24
     * @param ViewModel $view
25
     *
26
     * @return string
27
     */
28
    public function getTemplateName(ViewModel $view)
29
    {
30
        if ($view instanceof TemplateSpecifyingViewModel) {
31
            return $this->validateSpecifiedTemplateName($view);
32
        } else {
33
            return $this->calculateTemplateFromClassName($view);
34
        }
35
    }
36
37
    /**
38
     * @param TemplateSpecifyingViewModel $view
39
     *
40
     * @return mixed
41
     * @throws \UnexpectedValueException if no template is provided
42
     */
43
    protected function validateSpecifiedTemplateName(TemplateSpecifyingViewModel $view)
44
    {
45
        $template   = $view->getTemplateName();
46
        $view_class = get_class($view);
47
        if ( ! $template) {
48
            throw new \UnexpectedValueException(
49
                "$view_class::getTemplateName() must return a template name, empty value returned"
50
            );
51
        }
52
53
        if ( ! is_string($template)) {
54
            $type = is_object($template) ? get_class($template) : gettype($template);
55
            throw new \UnexpectedValueException(
56
                "$view_class::getTemplateName() must return a string template name, $type value returned"
57
            );
58
        }
59
60
        return $template;
61
    }
62
63
    /**
64
     * @param ViewModel $view
65
     *
66
     * @return string
67
     */
68
    protected function calculateTemplateFromClassName(ViewModel $view)
69
    {
70
        $template = get_class($view);
71
        $template = preg_replace('/\\\\|_/', '/', $template);
72
        $template = preg_replace('#(^view/?(model)?/)|(?<!/)(view/?(model)?$)#i', '', $template);
73
        $template = preg_replace('/([a-z])([A-Z])/', '\1_\2', $template);
74
        $template = strtolower($template);
75
76
        return $template;
77
    }
78
79
}
80