Completed
Push — master ( e7ad5b...332fd2 )
by Glynn
14s queued 13s
created

PinkCrab_BladeOne   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 116
rs 10
c 1
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A set_esc_function() 0 8 2
A get_template_paths() 0 2 1
A component() 0 5 1
A view_model() 0 5 1
A e() 0 11 5
1
<?php
2
3
declare( strict_types=1 );
4
5
/**
6
 * Wrapper for BladeOne with HTML enabled
7
 *
8
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
 *
20
 * @author Glynn Quelch <[email protected]>
21
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
22
 * @package PinkCrab\BladeOne_Provider
23
 */
24
25
namespace PinkCrab\BladeOne;
26
27
use php_user_filter;
28
use eftec\bladeone\BladeOne;
29
use eftec\bladeonehtml\BladeOneHtml;
30
use PinkCrab\Perique\Application\App;
31
use PinkCrab\Perique\Services\View\View;
32
use PinkCrab\Perique\Services\View\View_Model;
33
use PinkCrab\Perique\Services\View\Component\Component;
34
35
class PinkCrab_BladeOne extends BladeOne {
36
	use BladeOneHtml;
37
38
	/**
39
	 * Bob the constructor.
40
	 * The folder at $compiled_path is created in case it doesn't exist.
41
	 *
42
	 * @param string|string[] $template_path If null then it uses (caller_folder)/views
43
	 * @param string          $compiled_path If null then it uses (caller_folder)/compiles
44
	 * @param int             $mode         =[BladeOne::MODE_AUTO,BladeOne::MODE_DEBUG,BladeOne::MODE_FAST,BladeOne::MODE_SLOW][$i]
45
	 */
46
	public function __construct( $template_path = null, $compiled_path = null, $mode = 0 ) {
47
		parent::__construct( $template_path, $compiled_path, $mode );
48
49
		// Add the viewModel directive.
50
		$this->directiveRT(
51
			'viewModel',
52
			function( $expression ) {
53
				return $this->view_model( $expression, true );
54
			}
55
		);
56
57
		// Add the component directive.
58
		$this->directiveRT(
59
			'component',
60
			function( $expression ) {
61
				return $this->component( $expression, true );
62
			}
63
		);
64
	}
65
66
	/**
67
	 * The esc function to use
68
	 *
69
	 * @var callable(mixed):string
70
	 */
71
	protected static $esc_function = 'esc_html';
72
73
	/**
74
	 * The default echo format
75
	 *
76
	 * @var string
77
	 */
78
	protected $echoFormat = '\esc_html(%s)'; //phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
79
80
81
	/**
82
	 * Sets the esc function to use
83
	 *
84
	 * @param string $esc_function
85
	 * @return void
86
	 */
87
	public function set_esc_function( string $esc_function ): void {
88
		// Throw exception if not a valid callable.
89
		if ( ! \is_callable( $esc_function ) ) {
90
			throw new \InvalidArgumentException( 'Invalid esc function provided.' );
91
		}
92
93
		static::$esc_function = $esc_function;
94
		$this->echoFormat     = sprintf( '\\%s(%%s)', $esc_function ); //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
95
	}
96
97
	/**
98
	 * Returns the template paths
99
	 *
100
	 * @return string[]
101
	 */
102
	public function get_template_paths(): array {
103
		return $this->templatePath; //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
104
	}
105
106
	/**
107
	 * Escape HTML entities in a string.
108
	 *
109
	 * @param int|float|string|null|mixed[]|object $value
110
	 * @return string
111
	 */
112
	public static function e( $value ): string {
113
		if ( \is_null( $value ) ) {
114
			return '';
115
		}
116
		if ( \is_array( $value ) || \is_object( $value ) ) {
117
			return \call_user_func( static::$esc_function, \print_r( $value, true ) );//phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
118
		}
119
		if ( \is_numeric( $value ) ) {
120
			$value = (string) $value;
121
		}
122
		return \call_user_func( static::$esc_function, $value );
123
	}
124
125
	/**
126
	 * Renders  component
127
	 *
128
	 * @param Component $component
129
	 * @param bool $print
130
	 * @return string|void
131
	 */
132
	public function component( Component $component, bool $print = true ) {
133
		/** @var View */
134
		$view = App::view();
135
136
		return $view->component( $component, $print );
137
	}
138
139
	/**
140
	 * Renders a view model
141
	 *
142
	 * @param View_Model $view_model
143
	 * @param bool $print Print or Return the HTML
144
	 * @return string|void
145
	 */
146
	public function view_model( View_Model $view_model, bool $print = true ) {
147
		/** @var View */
148
		$view = App::view();
149
150
		return $view->view_model( $view_model, $print );
151
	}
152
153
154
}
155