Passed
Push — master ( 87ba60...d80a49 )
by Alain
02:43
created

Views::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Bright Nucleus View Component.
4
 *
5
 * @package   BrightNucleus\View
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus;
13
14
use BrightNucleus\Config\ConfigInterface;
15
use BrightNucleus\View\Location\Location;
16
use BrightNucleus\View\View;
17
use BrightNucleus\View\ViewBuilder;
18
19
/**
20
 * Class Views.
21
 *
22
 * @since   0.1.0
23
 *
24
 * @package BrightNucleus\View
25
 * @author  Alain Schlesser <[email protected]>
26
 */
27
class Views
28
{
29
30
    /**
31
     * ViewBuilder Instance.
32
     *
33
     * @since 0.1.0
34
     *
35
     * @var ViewBuilder
36
     */
37
    protected static $viewBuilder;
38
39
    /**
40
     * Add a location to the ViewBuilder.
41
     *
42
     * @since 0.1.0
43
     *
44
     * @param Location $location Location to add.
45
     */
46 15
    public static function addLocation(Location $location)
47
    {
48 15
        $viewBuilder = static::getViewBuilder();
49 15
        $viewBuilder->addLocation($location);
50 15
    }
51
52
    /**
53
     * Get the ViewBuilder instance.
54
     *
55
     * @since 0.1.0
56
     *
57
     * @return ViewBuilder
58
     */
59 15
    public static function getViewBuilder()
60
    {
61 15
        if (null === static::$viewBuilder) {
62 1
            static::$viewBuilder = static::instantiateViewBuilder();
63
        }
64
65 15
        return static::$viewBuilder;
66
    }
67
68
    /**
69
     * Instantiate the ViewBuilder.
70
     *
71
     * @since 0.1.0
72
     *
73
     * @param ConfigInterface|null $config Optional. Configuration to pass into the ViewBuilder.
74
     *
75
     * @return ViewBuilder Instance of the ViewBuilder.
76
     */
77 1
    public static function instantiateViewBuilder(ConfigInterface $config = null)
78
    {
79 1
        return static::$viewBuilder = new ViewBuilder($config);
80
    }
81
82
    /**
83
     * Create a new view for a given URI.
84
     *
85
     * @since 0.1.0
86
     *
87
     * @param string      $view View identifier to create a view for.
88
     * @param string|null $type Type of view to create.
89
     *
90
     * @return View Instance of the requested view.
91
     */
92 8
    public static function create($view, $type = null)
93
    {
94 8
        $viewBuilder = static::getViewBuilder();
95
96 8
        return $viewBuilder->create($view, $type);
97
    }
98
99
    /**
100
     * Render a view for a given URI.
101
     *
102
     * @since 0.1.0
103
     *
104
     * @param string      $view    View identifier to create a view for.
105
     * @param array       $context Optional. The context in which to render the view.
106
     * @param string|null $type    Type of view to create.
107
     *
108
     * @return string Rendered HTML content.
109
     */
110 7
    public static function render($view, array $context = [], $type = null)
111
    {
112 7
        $viewBuilder = static::getViewBuilder();
113 7
        $viewObject  = $viewBuilder->create($view, $type);
114
115 7
        return $viewObject->render($context);
116
    }
117
}
118