JsonAdapter::show()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * Json View adapter
4
 *
5
 * @file      JsonAdapter.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      Втр Апр 29 22:20:05 MSK 2014
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\View\Adapters;
17
18
/**
19
 * Class JsonAdapter
20
 *
21
 * @author  Alexander Yancharuk <alex at itvault dot info>
22
 */
23
class JsonAdapter extends ViewAdapterAbstract
24
{
25
	/**
26
	 * Output method
27
	 *
28
	 * @param string $path Path to template
29
	 */
30 1
	public function show($path)
31
	{
32 1
		if (empty($this->variables)) {
33 1
			return;
34
		}
35
36 1
		header('Content-Type: application/json');
37
38 1
		echo json_encode($this->variables);
39
	}
40
41
	/**
42
	 * Output View into buffer and save it in variable
43
	 *
44
	 * @param string $path Path to template
45
	 *
46
	 * @return string View content
47
	 */
48 1
	public function get($path)
49
	{
50 1
		foreach ($this->variables as $var_name => $value) {
51 1
			$$var_name = $value;
52
		}
53
54 1
		ob_start();
55 1
		echo json_encode($this->variables);
56 1
		$output = ob_get_contents();
57 1
		ob_end_clean();
58
59 1
		return $output;
60
	}
61
62
	/**
63
	 * Check template cache status
64
	 *
65
	 * @param string $tpl Template file
66
	 *
67
	 * @return bool Cache status
68
	 */
69 1
	public function isCached($tpl)
70
	{
71 1
		return false;
72
	}
73
74
	/**
75
	 * Driver initialization
76
	 */
77 3
	public function __construct()
78
	{
79 3
		$this->setDriver($this);
80
	}
81
}
82