• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

harmim / images / #1407

pending completion
#1407

push

harmim
`Nette\Utils\Image::place` does not accept floats

2 of 2 new or added lines in 1 file covered. (100.0%)

190 of 274 relevant lines covered (69.34%)

0.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.05
/src/Template/Macros.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * @author Dominik Harmim <harmim6@gmail.com>
7
 * @copyright Copyright (c) 2017 Dominik Harmim
8
 */
9

10
namespace Harmim\Images\Template;
11

12
use Harmim;
13
use Latte;
14
use Nette;
15

16

17
class Macros extends Latte\Macros\MacroSet
18
{
19
        public static function install(Latte\Compiler $compiler): void
1✔
20
        {
21
                $self = new static($compiler);
1✔
22

23
                $self->addMacro('img', [$self, 'macroImg'], null, [$self, 'attrMacroImg']);
1✔
24
                $self->addMacro('imgLink', [$self, 'macroImgLink']);
1✔
25
        }
1✔
26

27

28
        public function macroImg(Latte\MacroNode $node, Latte\PhpWriter $writer): string
1✔
29
        {
30
                [$img, $type] = $this->getImageFromNode($node);
1✔
31

32
                return sprintf(
1✔
33
                        'echo %s::img(%s, %s);',
1✔
34
                        get_class($this),
1✔
35
                        $writer->formatWord($img),
1✔
36
                        $this->formatMacroArgs($type, $node, $writer)
1✔
37
                );
38
        }
39

40

41
        public function attrMacroImg(Latte\MacroNode $node, Latte\PhpWriter $writer): string
1✔
42
        {
43
                [$img, $type] = $this->getImageFromNode($node);
1✔
44

45
                return sprintf(
1✔
46
                        'echo \' src="\' . %s::imgLink(%s, %s) . \'"\';',
1✔
47
                        get_class($this),
1✔
48
                        $writer->formatWord($img),
1✔
49
                        $this->formatMacroArgs($type, $node, $writer)
1✔
50
                );
51
        }
52

53

54
        public function macroImgLink(Latte\MacroNode $node, Latte\PhpWriter $writer): string
1✔
55
        {
56
                [$img, $type] = $this->getImageFromNode($node);
1✔
57

58
                return sprintf(
1✔
59
                        'echo %s::imgLink(%s, %s);',
1✔
60
                        get_class($this),
1✔
61
                        $writer->formatWord($img),
1✔
62
                        $this->formatMacroArgs($type, $node, $writer)
1✔
63
                );
64
        }
65

66

67
        private function formatMacroArgs(string $type, Latte\MacroNode $node, Latte\PhpWriter $writer): string
1✔
68
        {
69
                return sprintf(
1✔
70
                        '[%s\'storage\' => $imageStorage%s]',
1✔
71
                        $type ? "'type' => '$type', " : '',
1✔
72
                        $node->args ? ", {$writer->formatArgs()}" : ''
1✔
73
                );
74
        }
75

76

77
        private function getImageFromNode(Latte\MacroNode $node): array
1✔
78
        {
79
                $img = $node->tokenizer->fetchWord();
1✔
80
                $type = '';
1✔
81

82
                if ($node->tokenizer->isNext()) {
1✔
83
                        $type = $node->tokenizer->fetchWord();
1✔
84

1✔
85
                        if ($node->tokenizer->isNext() && $node->tokenizer->isNext('=>')) {
1✔
86
                                $node->tokenizer->reset();
1✔
87
                                $img = $node->tokenizer->fetchWord();
1✔
88
                                $type = '';
89
                        }
90
                }
91

1✔
92
                return [(string) $img, (string) $type];
93
        }
94

95

96
        /**
97
         * @param string|Harmim\Images\IImage|mixed $img
98
         * @param array $args
99
         * @return string|null
100
         */
1✔
101
        public static function img($img, array $args): ?string
102
        {
1✔
103
                if ($image = static::getImage($img, $args)) {
1✔
104
                        $lazy = !empty($args['lazy']);
105

1✔
106
                        $args = array_filter($args, function ($key) {
1✔
107
                                foreach (Harmim\Images\DI\ImagesExtension::DEFAULTS['imgTagAttributes'] as $attr) {
1✔
108
                                        if (Nette\Utils\Strings::startsWith($key, $attr)) {
1✔
109
                                                return true;
110
                                        }
111
                                }
112

1✔
113
                                return false;
1✔
114
                        }, ARRAY_FILTER_USE_KEY);
115

1✔
116
                        $classes = explode(' ', $args['class'] ?? '');
1✔
117
                        unset($args['class']);
118

1✔
119
                        $imgTag = Nette\Utils\Html::el('img');
1✔
120
                        $imgTag->src = (string) $image;
1✔
121
                        $imgTag->class = $classes;
1✔
122
                        $imgTag->addAttributes($args);
123

1✔
124
                        if ($lazy) {
1✔
125
                                $lazyImgTag = Nette\Utils\Html::el('img');
1✔
126
                                $lazyImgTag->data('src', (string) $image);
1✔
127
                                array_unshift($classes, 'lazy');
1✔
128
                                $lazyImgTag->class = $classes;
1✔
129
                                $lazyImgTag->addAttributes($args);
130

1✔
131
                                $noscriptTag = Nette\Utils\Html::el('noscript');
1✔
132
                                $noscriptTag->addHtml($imgTag);
133

1✔
134
                                $wrapper = Nette\Utils\Html::el()
1✔
135
                                        ->addHtml($lazyImgTag)
1✔
136
                                        ->addHtml($noscriptTag);
137

1✔
138
                                return (string) $wrapper;
139
                        }
140

1✔
141
                        return (string) $imgTag;
142
                }
143

×
144
                return null;
145
        }
146

147

148
        /**
149
         * @param string|Harmim\Images\IImage|mixed $img
150
         * @param array $args
151
         * @return string|null
152
         */
1✔
153
        public static function imgLink($img, array $args): ?string
154
        {
1✔
155
                if ($image = static::getImage($img, $args)) {
1✔
156
                        return (string) $image;
157
                }
158

×
159
                return null;
160
        }
161

162

163
        /**
164
         * @param string|Harmim\Images\IImage|mixed $img
165
         * @param array $args
166
         * @return Harmim\Images\Image|null
167
         * @throws Nette\InvalidStateException
168
         */
1✔
169
        public static function getImage($img, array &$args): ?Harmim\Images\Image
170
        {
1✔
171
                if (empty($args['storage']) || !$args['storage'] instanceof Harmim\Images\ImageStorage) {
×
172
                        throw new Nette\InvalidStateException(sprintf(
×
173
                                'The template was not forwarded instance of %s to macro img/imgLink, it should have in variable $imageStorage.',
×
174
                                Harmim\Images\ImageStorage::class
175
                        ));
176
                }
177

178
                /** @var Harmim\Images\ImageStorage $imageStorage */
1✔
179
                $imageStorage = $args['storage'];
1✔
180
                unset($args['storage']);
181

182

1✔
183
                $image = $imageStorage->getImage($img, $args);
1✔
184
                $args = $imageStorage->getOptions($args);
185

1✔
186
                return $image;
187
        }
188
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc