Macros::macroIsTablet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Macros.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:MobileDetect!
9
 * @subpackage     Latte
10
 * @since          1.0.0
11
 *
12
 * @date           22.04.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\MobileDetect\Latte;
18
19
use Latte\Compiler;
20
use Latte\MacroNode;
21
use Latte\PhpWriter;
22
use Latte\Macros\MacroSet;
23
24
use IPub\MobileDetect\Exceptions;
25
26
/**
27
 * Mobile detect latte macros definition
28
 *
29
 * @package        iPublikuj:MobileDetect!
30
 * @subpackage     Latte
31
 *
32
 * @author         Adam Kadlec <[email protected]>
33
 */
34 1
final class Macros extends MacroSet
35
{
36
	/**
37
	 * Register latte macros
38
	 *
39
	 * @param Compiler $compiler
40
	 *
41
	 * @return static
42
	 */
43
	public static function install(Compiler $compiler)
44
	{
45 1
		$me = new static($compiler);
46
47
		/**
48
		 * {isMobile /}, {isNotMobile /}
49
		 */
50 1
		$me->addMacro('isMobile', [$me, 'macroIsMobile'], '}');
51 1
		$me->addMacro('isNotMobile', [$me, 'macroIsNotMobile'], '}');
52
53
		/**
54
		 * {isPhone /}, {isNotPhone /}
55
		 */
56 1
		$me->addMacro('isPhone', [$me, 'macroIsPhone'], '}');
57 1
		$me->addMacro('isNotPhone', [$me, 'macroIsNotPhone'], '}');
58
59
		/**
60
		 * {isTablet /}, {isNotTablet /}
61
		 */
62 1
		$me->addMacro('isTablet', [$me, 'macroIsTablet'], '}');
63 1
		$me->addMacro('isNotTablet', [$me, 'macroIsNotTablet'], '}');
64
65
		/**
66
		 * {isMobileDevice 'device_name'}
67
		 */
68 1
		$me->addMacro('isMobileDevice', [$me, 'macroIsDevice'], '}');
69
70
		/**
71
		 * {isMobileOs 'device_name'}
72
		 */
73 1
		$me->addMacro('isMobileOs', [$me, 'macroIsOS'], '}');
74
75
		/**
76
		 * {isFullView /}, {isMobileView /}, {isTabletView /}, {isNotMobileView /}
77
		 */
78 1
		$me->addMacro('isFullView', [$me, 'macroIsFullView'], '}');
79 1
		$me->addMacro('isMobileView', [$me, 'macroIsMobileView'], '}');
80 1
		$me->addMacro('isPhoneView', [$me, 'macroIsPhoneView'], '}');
81 1
		$me->addMacro('isTabletView', [$me, 'macroIsTabletView'], '}');
82 1
		$me->addMacro('isNotMobileView', [$me, 'macroIsNotMobileView'], '}');
83
84 1
		return $me;
85
	}
86
87
	/**
88
	 * {isMobile /}
89
	 *
90
	 * @param MacroNode $node
91
	 * @param PhpWriter $writer
92
	 *
93
	 * @return string
94
	 */
95
	public function macroIsMobile(MacroNode $node, PhpWriter $writer) : string
96
	{
97 1
		return $writer->write('
98
			$_resultMD = property_exists($this, "filters") ? %escape(call_user_func($this->filters->isMobile)) : $template->getMobileDetectService()->isMobile() && !$template->getMobileDetectService()->isTablet();
99
			if ($_resultMD) {
100
			');
101
	}
102
103
	/**
104
	 * {isNotMobile /}
105
	 *
106
	 * @param MacroNode $node
107
	 * @param PhpWriter $writer
108
	 *
109
	 * @return string
110
	 */
111
	public function macroIsNotMobile(MacroNode $node, PhpWriter $writer) : string
112
	{
113
		return $writer->write('
114
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isMobile)) : !$template->getMobileDetectService()->isMobile();
115
			if ($_resultMD) {
116
			');
117
	}
118
119
	/**
120
	 * {isPhone /}
121
	 *
122
	 * @param MacroNode $node
123
	 * @param PhpWriter $writer
124
	 *
125
	 * @return string
126
	 */
127
	public function macroIsPhone(MacroNode $node, PhpWriter $writer) : string
128
	{
129 1
		return $writer->write('
130
			$_resultMD = property_exists($this, "filters") ? %escape(call_user_func($this->filters->isMobile) && !call_user_func($this->filters->isTablet)) : $template->getMobileDetectService()->isMobile() && !$template->getMobileDetectService()->isTablet();
131
			if ($_resultMD) {
132
			');
133
	}
134
135
	/**
136
	 * {isNotPhone /}
137
	 *
138
	 * @param MacroNode $node
139
	 * @param PhpWriter $writer
140
	 *
141
	 * @return string
142
	 */
143
	public function macroIsNotPhone(MacroNode $node, PhpWriter $writer) : string
144
	{
145
		return $writer->write('
146
			$_resultMD = property_exists($this, "filters") ? %escape((call_user_func($this->filters->isMobile) && call_user_func($this->filters->isTablet)) || !call_user_func($this->filters->isMobile)) : ($template->getMobileDetectService()->isMobile() && $template->getMobileDetectService()->isTablet()) || !$template->getMobileDetectService()->isMobile();
147
			if ($_resultMD) {
148
			');
149
	}
150
151
	/**
152
	 * {isTablet /}
153
	 *
154
	 * @param MacroNode $node
155
	 * @param PhpWriter $writer
156
	 *
157
	 * @return string
158
	 */
159
	public function macroIsTablet(MacroNode $node, PhpWriter $writer) : string
160
	{
161 1
		return $writer->write('
162
			$_resultMD = property_exists($this, "filters") ? %escape(call_user_func($this->filters->isTablet)) : $template->getMobileDetectService()->isTablet();
163
			if ($_resultMD) {
164
			');
165
	}
166
167
	/**
168
	 * {isNotTablet /}
169
	 *
170
	 * @param MacroNode $node
171
	 * @param PhpWriter $writer
172
	 *
173
	 * @return string
174
	 */
175
	public function macroIsNotTablet(MacroNode $node, PhpWriter $writer) : string
176
	{
177
		return $writer->write('
178
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isTablet)) : !$template->getMobileDetectService()->isTablet();
179
			if ($_resultMD) {
180
			');
181
	}
182
183
	/**
184
	 * @param MacroNode $node
185
	 * @param PhpWriter $writer
186
	 *
187
	 * @return string
188
	 *
189
	 * @throws Exceptions\CompileException
190
	 */
191 View Code Duplication
	public function macroIsDevice(MacroNode $node, PhpWriter $writer) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
	{
193 1
		$arguments = self::prepareMacroArguments($node->args);
194
195 1
		if ($arguments['device'] === NULL) {
196
			throw new Exceptions\CompileException('Please provide device name.');
197
		}
198
199
		// Create magic method name
200 1
		$magicMethodName = 'is' . ucfirst(strtolower((string) $arguments["device"]));
201
202 1
		return $writer->write('
203 1
			$_resultMD = property_exists($this, "filters") ? %escape(call_user_func($this->filters->isDevice, "' . $arguments['device'] . '")) : $template->getMobileDetectService()->'. $magicMethodName.'();
204
			if ($_resultMD) {
205
			');
206
	}
207
208
	/**
209
	 * @param MacroNode $node
210
	 * @param PhpWriter $writer
211
	 *
212
	 * @return string
213
	 *
214
	 * @throws Exceptions\CompileException
215
	 */
216 View Code Duplication
	public function macroIsOS(MacroNode $node, PhpWriter $writer) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
	{
218 1
		$arguments = self::prepareMacroArguments($node->args);
219
220 1
		if ($arguments['os'] === NULL) {
221
			throw new Exceptions\CompileException('Please provide OS name.');
222
		}
223
224
		// Create magic method name
225 1
		$magicMethodName = 'is' . ucfirst(strtolower((string) $arguments["os"]));
226
227 1
		return $writer->write('
228 1
			$_resultMD = property_exists($this, "filters") ? %escape(call_user_func($this->filters->isOs, "' . $arguments['os'] . '")) : $template->getMobileDetectService()->'. $magicMethodName.'();
229
			if ($_resultMD) {
230
			');
231
	}
232
233
	/**
234
	 * {isFullView /}
235
	 *
236
	 * @param MacroNode $node
237
	 * @param PhpWriter $writer
238
	 *
239
	 * @return string
240
	 */
241
	public function macroIsFullView(MacroNode $node, PhpWriter $writer) : string
242
	{
243
		return $writer->write('
244
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isFullView)) : $template->getDeviceViewService()->isFullView();
245
			if ($_resultMD) {
246
			');
247
	}
248
249
	/**
250
	 * {isMobileView /}
251
	 *
252
	 * @param MacroNode $node
253
	 * @param PhpWriter $writer
254
	 *
255
	 * @return string
256
	 */
257
	public function macroIsMobileView(MacroNode $node, PhpWriter $writer) : string
258
	{
259
		return $writer->write('
260
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isMobileView)) : $template->getDeviceViewService()->isMobileView();
261
			if ($_resultMD) {
262
			');
263
	}
264
265
	/**
266
	 * {isPhoneView /}
267
	 *
268
	 * @param MacroNode $node
269
	 * @param PhpWriter $writer
270
	 *
271
	 * @return string
272
	 */
273
	public function macroIsPhoneView(MacroNode $node, PhpWriter $writer) : string
274
	{
275
		return $writer->write('
276
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isPhoneView)) : $template->getDeviceViewService()->isPhoneView();
277
			if ($_resultMD) {
278
			');
279
	}
280
281
	/**
282
	 * {isTabletView /}
283
	 *
284
	 * @param MacroNode $node
285
	 * @param PhpWriter $writer
286
	 *
287
	 * @return string
288
	 */
289
	public function macroIsTabletView(MacroNode $node, PhpWriter $writer) : string
290
	{
291
		return $writer->write('
292
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isTabletView)) : $template->getDeviceViewService()->isTabletView();
293
			if ($_resultMD) {
294
			');
295
	}
296
297
	/**
298
	 * {isNotMobileView /}
299
	 *
300
	 * @param MacroNode $node
301
	 * @param PhpWriter $writer
302
	 *
303
	 * @return string
304
	 */
305
	public function macroIsNotMobileView(MacroNode $node, PhpWriter $writer) : string
306
	{
307
		return $writer->write('
308
			$_resultMD = property_exists($this, "filters") ? %escape(!call_user_func($this->filters->isNotMobileView)) : $template->getDeviceViewService()->isNotMobileView();
309
			if ($_resultMD) {
310
			');
311
	}
312
313
	/**
314
	 * @param string $macro
315
	 *
316
	 * @return array
317
	 */
318
	public static function prepareMacroArguments($macro) : array
319
	{
320 1
		$arguments = array_map(function ($value) : string {
321 1
			return trim($value);
322 1
		}, explode(',', $macro));
323
324 1
		$device = $os = $arguments[0];
325
326
		return [
327 1
			'device' => $device,
328 1
			'os'     => $os,
329
		];
330
	}
331
}
332