Completed
Push — master ( 4ab504...e81d71 )
by Michael
06:49 queued 04:34
created

View::setBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 */
9
10
namespace miBadger\Mvc;
11
12
use miBadger\Singleton\SingletonTrait;
13
14
/**
15
 * The view class of the MVC pattern.
16
 *
17
 * @see http://en.wikipedia.org/wiki/Model-view-controller
18
 * @since 1.0.0
19
 */
20
class View
21
{
22
	use SingletonTrait;
23
24
	const DIRECTORY_SEPARATOR = \DIRECTORY_SEPARATOR;
25
26
	/** @var string the base path. */
27
	private $basePath;
28
29
	/**
30
	 * Construct a object.
31
	 */
32 4
	protected function __construct()
33
	{
34 4
		$this->basePath = null;
35 4
	}
36
37
	/**
38
	 * Returns the base path.
39
	 *
40
	 * @return string the base path.
41
	 */
42 2
	public static function getBasePath()
43
	{
44 2
		return static::getInstance()->basePath;
45
	}
46
47
	/**
48
	 * Set the base path.
49
	 *
50
	 * @param string $basePath = null
0 ignored issues
show
Documentation introduced by Michael Webbers
Should the type for parameter $basePath not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
51
	 */
52 2
	public static function setBasePath($basePath = null)
53
	{
54 2
		static::getInstance()->basePath = $basePath;
55 2
	}
56
57
	/**
58
	 * Returns the view at the given path with the given data.
59
	 *
60
	 * @param string $path
61
	 * @param string[] $data = []
62
	 * @return string a string representation of the view.
63
	 */
64 2
	public static function get($path, $data = [])
65
	{
66 2
		ob_start();
67
68 2
		extract($data);
69
70
		try {
71 2
			$basePath = static::getInstance()->basePath;
72
73 2
			if ($basePath) {
74 1
				if (mb_substr($path, 0, 1) === static::DIRECTORY_SEPARATOR) {
75
					$this->$path = mb_substr($path, 1);
76
				}
77
78 1
				if (mb_substr($basePath, -1) === static::DIRECTORY_SEPARATOR) {
79
					$this->$basePath = mb_substr($basePath, 0, -1);
80
				}
81
82 1
				$path = $basePath . static::DIRECTORY_SEPARATOR . $path;
83
			}
84
85 2
			include $path;
86 1
		} catch (\Exception $e) {
87 1
			ob_get_clean();
88 1
			throw $e;
89
		}
90
91 1
		return ob_get_clean();
92
	}
93
}
94