Completed
Push — master ( 206863...5446b2 )
by Adam
02:16
created

Macros::macroGravatar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2.0625
1
<?php
2
/**
3
 * Macros.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
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 Nette;
20
21
use Latte;
22
use Latte\Compiler;
23
use Latte\MacroNode;
24
use Latte\PhpWriter;
25
use Latte\Macros\MacroSet;
26
27
/**
28
 * Gravatar latte macros definition
29
 *
30
 * @package        iPublikuj:Gravatar!
31
 * @subpackage     Latte
32
 *
33
 * @author         Adam Kadlec <[email protected]>
34
 */
35 1
final class Macros extends MacroSet
36
{
37
38
	/**
39
	 * Register latte macros
40
	 *
41
	 * @param Compiler $compiler
42
	 *
43
	 * @return static
44
	 */
45
	public static function install(Compiler $compiler)
46
	{
47 1
		$me = new static($compiler);
48
49
		/**
50
		 * {gravatar $email[, $size]}
51
		 */
52 1
		$me->addMacro('gravatar', [$me, 'macroGravatar'], NULL, [$me, 'macroAttrGravatar']);
53
54 1
		return $me;
55
	}
56
57
58
	/**
59
	 * @param MacroNode $node
60
	 * @param PhpWriter $writer
61
	 *
62
	 * @return string
63
	 *
64
	 * @throws Latte\CompileException
65
	 */
66
	public function macroGravatar(MacroNode $node, PhpWriter $writer) : string
67
	{
68 1
		$arguments = self::prepareMacroArguments($node->args);
69
70 1
		if ($arguments['email'] === NULL) {
71
			throw new Latte\CompileException('Please provide email address.');
72
		}
73
74 1
		return $writer->write('echo property_exists($this, "filters") ? %escape(call_user_func($this->filters->gravatar, ' . $arguments['email'] . ', ' . $arguments['size'] . ')) : $template->getGravatarService()->buildUrl(' . $arguments['email'] . ', ' . $arguments['size'] . ');');
75
	}
76
77
78
	/**
79
	 * @param MacroNode $node
80
	 * @param PhpWriter $writer
81
	 *
82
	 * @return string
83
	 *
84
	 * @throws Latte\CompileException
85
	 */
86
	public function macroAttrGravatar(MacroNode $node, PhpWriter $writer) : string
87
	{
88 1
		$arguments = self::prepareMacroArguments($node->args);
89
90 1
		if ($arguments['email'] === NULL) {
91
			throw new Latte\CompileException('Please provide email address.');
92
		}
93
94 1
		return $writer->write('?> ' . ($node->htmlNode->name === 'a' ? 'href' : 'src') . '="<?php echo property_exists($this, "filters") ? %escape(call_user_func($this->filters->gravatar, ' . $arguments['email'] . ', ' . $arguments['size'] . ')) : $template->getGravatarService()->buildUrl(' . $arguments['email'] . ', ' . $arguments['size'] . ');?>" <?php');
95
	}
96
97
98
	/**
99
	 * @param string $macro
100
	 *
101
	 * @return array
102
	 */
103
	public static function prepareMacroArguments(string $macro) : array
104
	{
105 1
		$arguments = array_map(function ($value) {
106 1
			return trim($value);
107 1
		}, explode(',', $macro));
108
109 1
		$name = $arguments[0];
110 1
		$size = (isset($arguments[1]) && !empty($arguments[1])) ? $arguments[1] : NULL;
111
112
		return [
113 1
			'email' => $name,
114 1
			'size'  => $size,
115
		];
116
	}
117
}
118