Completed
Pull Request — 2.0.x (#6)
by Andrew
02:11
created

ViewTemplateSelector   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 6
c 3
b 0
f 1
lcom 0
cbo 2
dl 0
loc 55
rs 10

3 Methods

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