Completed
Push — develop ( 3f13ed...151ba0 )
by Zack
30:06 queued 21:30
created

Template_Context::from_template()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 2
dl 0
loc 25
ccs 13
cts 13
cp 1
crap 5
rs 9.2088
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * A template Context class.
11
 *
12
 * This is provided to most template files as a global.
13
 */
14
class Template_Context extends Context {
15
	/**
16
	 * @var \GV\Template The template.
17
	 */
18
	public $template;
19
20
	/**
21
	 * @var \GV\View The view.
22
	 */
23
	public $view;
24
25
	/**
26
	 * @var \GV\Entry The entry. If single-entry view.
27
	 */
28
	public $entry;
29
30
	/**
31
	 * @var \GV\Entry_Collection The entries. If directory view.
32
	 */
33
	public $entries;
34
35
	/**
36
	 * @var \GV\Field_Collection The fields.
37
	 */
38
	public $fields;
39
40
	/**
41
	 * @var \GV\Field The field. When rendering a single field.
42
	 */
43
	public $field;
44
45
	/**
46
	 * @var \GV\Source The data source for a field.
47
	 */
48
	public $source;
49
50
	/**
51
	 * @var mixed The display value for a field.
52
	 */
53
	public $display_value;
54
55
	/**
56
	 * @var mixed The raw value for a field.
57
	 */
58
	public $value;
59
60
	/**
61
	 * @var \GV\Request The request.
62
	 */
63
	public $request;
64
65
	/**
66
	 * Create a context from a Template
67
	 *
68
	 * @param \GV\Template|array $template The template or array with values expected in a template
69
	 * @param array $data Additional data not tied to the template object.
70
	 *
71
	 * @return \GV\Template_Context The context holder.
72
	 */
73 94
	public static function from_template( $template, $data = array() ) {
74 94
		$context = new self();
75
76 94
		$context->template = $template;
77
78
		/**
79
		 * Data.
80
		 */
81 94
		$context->display_value = Utils::get( $data, 'display_value' );
82 94
		$context->value = Utils::get( $data, 'value' );
83
84
		/**
85
		 * Shortcuts.
86
		 */
87 94
		$context->view = Utils::get( $template, 'view' );
88 94
		$context->source = Utils::get( $template, 'source' );
89 94
		$context->field = Utils::get( $template, 'field' ) ? : Utils::get( $data, 'field' );
90 94
		$context->entry = Utils::get( $template, 'entry' ) ? : Utils::get( $data, 'entry' );
91 94
		$context->request = Utils::get( $template, 'request' );
92
93 94
		$context->entries = Utils::get( $template, 'entries' ) ? $template->entries : null;
94 94
		$context->fields = $context->view ? $context->view->fields : null;
95
96 94
		return $context;
97
	}
98
}
99