Macros   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 82.61%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 4
dl 0
loc 83
ccs 19
cts 23
cp 0.8261
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A install() 0 11 1
A macroPhone() 0 10 2
B prepareMacroArguments() 0 25 7
A isPhoneCountry() 0 4 3
1
<?php
2
/**
3
 * Macros.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:PhoneUI!
9
 * @subpackage     Latte
10
 * @since          1.0.0
11
 *
12
 * @date           12.12.15
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\PhoneUI\Latte;
18
19
use Nette;
20
21
use Latte;
22
use Latte\Compiler;
23
use Latte\MacroNode;
24
use Latte\PhpWriter;
25
use Latte\Macros\MacroSet;
26
27
use IPub\Phone;
28
29
/**
30
 * Phone UI number Latte macros
31
 *
32
 * @package        iPublikuj:PhoneUI!
33
 * @subpackage     Latte
34
 *
35
 * @author         Adam Kadlec <[email protected]>
36
 */
37 1
class Macros extends MacroSet
38
{
39
	/**
40
	 * Register latte macros
41
	 *
42
	 * @param Compiler $compiler
43
	 *
44
	 * @return static
45
	 */
46
	public static function install(Compiler $compiler)
47
	{
48 1
		$me = new static($compiler);
49
50
		/**
51
		 * {phone $phoneNumber[, $country, $format]}
52
		 */
53 1
		$me->addMacro('phone', [$me, 'macroPhone']);
54
55 1
		return $me;
56
	}
57
58
	/**
59
	 * @param MacroNode $node
60
	 * @param PhpWriter $writer
61
	 *
62
	 * @return string
63
	 *
64
	 * @throws Latte\CompileException
65
	 */
66
	public function macroPhone(MacroNode $node, PhpWriter $writer)
67
	{
68 1
		$arguments = self::prepareMacroArguments($node->args);
69
70 1
		if ($arguments['phone'] === NULL) {
71
			throw new Latte\CompileException('Please provide phone number.');
72
		}
73
74 1
		return $writer->write('echo %escape(property_exists($this, "filters") ? call_user_func($this->filters->phone, "' . $arguments['phone'] . '", "' . $arguments['country'] . '", ' . $arguments['format'] . ') : $template->getPhoneNumberService()->format("' . $arguments['phone'] . '", "' . $arguments['country'] . '", ' . $arguments['format'] . '));');
75
	}
76
77
	/**
78
	 * @param string $macro
79
	 *
80
	 * @return array
81
	 */
82
	public static function prepareMacroArguments(string $macro) : array
83
	{
84 1
		$arguments = array_map(function ($value) {
85 1
			return trim(trim($value), '\'"');
86 1
		}, explode(",", $macro));
87
88 1
		$phone = $arguments[0];
89 1
		$country = (isset($arguments[1]) && !empty($arguments[1])) ? strtoupper($arguments[1]) : NULL;
90 1
		$format = (isset($arguments[2]) && !empty($arguments[2])) ? $arguments[2] : Phone\Phone::FORMAT_INTERNATIONAL;
91
92 1
		if (!self::isPhoneCountry($country)) {
93
			$format = (int) $country;
94
			$country = 'AUTO';
95
		}
96
97 1
		if ($country === NULL) {
98
			$country = 'AUTO';
99
		}
100
101
		return [
102 1
			'phone'   => (string) $phone,
103 1
			'country' => (string) $country,
104 1
			'format'  => (int) $format,
105
		];
106
	}
107
108
	/**
109
	 * Checks if the supplied string is a valid country code using some arbitrary country validation
110
	 *
111
	 * @param string $country
112
	 *
113
	 * @return bool
114
	 */
115
	protected static function isPhoneCountry(string $country) : bool
116
	{
117 1
		return (strlen($country) === 2 && ctype_alpha($country) && ctype_upper($country));
118
	}
119
}
120