Completed
Push — master ( fea9b0...7304cb )
by Adam
02:22
created

Macros::prepareMacroArguments()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 13
nc 64
nop 1
crap 7
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:Gravatar!
9
 * @subpackage     Latte
10
 * @since          1.0.0
11
 *
12
 * @date           06.04.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Gravatar\Latte;
18
19
use Latte;
20
use Latte\Compiler;
21
use Latte\MacroNode;
22
use Latte\PhpWriter;
23
use Latte\Macros\MacroSet;
24
25
/**
26
 * Gravatar latte macros definition
27
 *
28
 * @package        iPublikuj:Gravatar!
29
 * @subpackage     Latte
30
 *
31
 * @author         Adam Kadlec <[email protected]>
32
 */
33 1
final class Macros extends MacroSet
34
{
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
		 * {gravatar $email[, $size]}
49
		 */
50 1
		$me->addMacro('gravatar', [$me, 'macroGravatar'], NULL, [$me, 'macroAttrGravatar']);
51
52 1
		return $me;
53
	}
54
55
56
	/**
57
	 * @param MacroNode $node
58
	 * @param PhpWriter $writer
59
	 *
60
	 * @return string
61
	 *
62
	 * @throws Latte\CompileException
63
	 */
64
	public function macroGravatar(MacroNode $node, PhpWriter $writer) : string
65
	{
66 1
		$arguments = self::prepareMacroArguments($node->args);
67
68 1
		if ($arguments['email'] === NULL) {
69
			throw new Latte\CompileException('Please provide email address.');
70
		}
71
72 1
		return $writer->write('echo %escape(property_exists($this, "filters") ? call_user_func($this->filters->gravatar, ' . $arguments['email'] . ', ' . $arguments['size'] . ', ' . $arguments['maxRating'] . ', ' . $arguments['defaultImage'] . ') : $template->getGravatarService()->buildUrl(' . $arguments['email'] . ', ' . $arguments['size'] . ', ' . $arguments['maxRating'] . ', ' . $arguments['defaultImage'] . '));');
73
	}
74
75
76
	/**
77
	 * @param MacroNode $node
78
	 * @param PhpWriter $writer
79
	 *
80
	 * @return string
81
	 *
82
	 * @throws Latte\CompileException
83
	 */
84
	public function macroAttrGravatar(MacroNode $node, PhpWriter $writer) : string
85
	{
86 1
		$arguments = self::prepareMacroArguments($node->args);
87
88 1
		if ($arguments['email'] === NULL) {
89
			throw new Latte\CompileException('Please provide email address.');
90
		}
91
92 1
		return $writer->write('?> ' . ($node->htmlNode->name === 'a' ? 'href' : 'src') . '="<?php echo %escape(property_exists($this, "filters") ? call_user_func($this->filters->gravatar, ' . $arguments['email'] . ', ' . $arguments['size'] . ', ' . $arguments['maxRating'] . ', ' . $arguments['defaultImage'] . ') : $template->getGravatarService()->buildUrl(' . $arguments['email'] . ', ' . $arguments['size'] . ', ' . $arguments['maxRating'] . ', ' . $arguments['defaultImage'] . '));?>" <?php');
93
	}
94
95
96
	/**
97
	 * @param string $macro
98
	 *
99
	 * @return array
100
	 */
101
	public static function prepareMacroArguments(string $macro) : array
102
	{
103 1
		$arguments = array_map(function ($value) : string {
104 1
			return trim($value);
105 1
		}, explode(',', $macro));
106
107 1
		$name = $arguments[0];
108 1
		$size = (isset($arguments[1]) && !empty($arguments[1])) ? $arguments[1] : 'NULL';
109 1
		$maxRating = (isset($arguments[2]) && !empty($arguments[2])) ? $arguments[2] : 'NULL';
110 1
		$defaultImage = (isset($arguments[3]) && !empty($arguments[3])) ? $arguments[3] : 'NULL';
111
112
		return [
113 1
			'email'        => $name,
114 1
			'size'         => $size,
115 1
			'maxRating'    => $maxRating,
116 1
			'defaultImage' => $defaultImage
117
		];
118
	}
119
}
120