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

nepada / meta-control / 5089637756

pending completion
5089637756

push

github

xificurk
Fix cs

60 of 64 relevant lines covered (93.75%)

0.94 hits per line

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

100.0
/src/MetaControl/MetaControl.php
1
<?php
2
declare(strict_types = 1);
3

4
namespace Nepada\MetaControl;
5

6
use Nette;
7
use Nette\Utils\Html;
8

9
class MetaControl extends Nette\Application\UI\Component
10
{
11

12
    private const META_AUTHOR = 'author';
13
    private const META_DESCRIPTION = 'description';
14
    private const META_KEYWORDS = 'keywords';
15
    private const META_ROBOTS = 'robots';
16

17
    private ?string $charset = null;
18

19
    /**
20
     * @var string[]
21
     */
22
    private array $metadata = [];
23

24
    /**
25
     * @var string[]
26
     */
27
    private array $properties = [];
28

29
    /**
30
     * @var string[]
31
     */
32
    private array $pragmas = [];
33

34
    public function render(): void
35
    {
36
        if ($this->charset !== null) {
1✔
37
            echo Html::el('meta', ['charset' => $this->charset]) . "\n";
1✔
38
        }
39
        foreach ($this->metadata as $name => $content) {
1✔
40
            echo Html::el('meta', ['name' => $name, 'content' => $content]) . "\n";
1✔
41
        }
42
        foreach ($this->properties as $name => $content) {
1✔
43
            echo Html::el('meta', ['property' => $name, 'content' => $content]) . "\n";
1✔
44
        }
45
        foreach ($this->pragmas as $httpEquiv => $content) {
1✔
46
            echo Html::el('meta', ['http-equiv' => $httpEquiv, 'content' => $content]) . "\n";
1✔
47
        }
48
    }
1✔
49

50
    public function getCharset(): ?string
51
    {
52
        return $this->charset;
1✔
53
    }
54

55
    public function setCharset(?string $charset): void
1✔
56
    {
57
        $this->charset = $charset;
1✔
58
    }
1✔
59

60
    public function getMetadata(string $name): ?string
1✔
61
    {
62
        return Nette\Utils\Arrays::get($this->metadata, $name, null);
1✔
63
    }
64

65
    public function setMetadata(string $name, ?string $content): void
1✔
66
    {
67
        if ($content === null) {
1✔
68
            unset($this->metadata[$name]);
1✔
69
        } else {
70
            $this->metadata[$name] = $content;
1✔
71
        }
72
    }
1✔
73

74
    public function getProperty(string $name): ?string
1✔
75
    {
76
        return Nette\Utils\Arrays::get($this->properties, $name, null);
1✔
77
    }
78

79
    public function setProperty(string $property, ?string $content): void
1✔
80
    {
81
        if ($content === null) {
1✔
82
            unset($this->properties[$property]);
1✔
83
        } else {
84
            $this->properties[$property] = $content;
1✔
85
        }
86
    }
1✔
87

88
    public function getPragma(string $name): ?string
1✔
89
    {
90
        return Nette\Utils\Arrays::get($this->pragmas, $name, null);
1✔
91
    }
92

93
    public function setPragma(string $httpEquiv, ?string $content): void
1✔
94
    {
95
        if ($content === null) {
1✔
96
            unset($this->pragmas[$httpEquiv]);
1✔
97
        } else {
98
            $this->pragmas[$httpEquiv] = $content;
1✔
99
        }
100
    }
1✔
101

102
    public function getAuthor(): ?string
103
    {
104
        return $this->getMetadata(self::META_AUTHOR);
1✔
105
    }
106

107
    public function setAuthor(?string $author): void
1✔
108
    {
109
        $this->setMetadata(self::META_AUTHOR, $author);
1✔
110
    }
1✔
111

112
    public function getDescription(): ?string
113
    {
114
        return $this->getMetadata(self::META_DESCRIPTION);
1✔
115
    }
116

117
    public function setDescription(?string $description): void
1✔
118
    {
119
        $this->setMetadata(self::META_DESCRIPTION, $description);
1✔
120
    }
1✔
121

122
    /**
123
     * @return string[]
124
     */
125
    public function getKeywords(): array
126
    {
127
        $keywords = $this->getMetadata(self::META_KEYWORDS);
1✔
128
        return $keywords === null ? [] : array_map('trim', explode(',', $keywords));
1✔
129
    }
130

131
    public function setKeywords(string ...$keywords): void
1✔
132
    {
133
        if ($keywords === []) {
1✔
134
            $this->setMetadata(self::META_KEYWORDS, null);
1✔
135
        } else {
136
            $keywords = array_map('trim', $keywords);
1✔
137
            $keywords = array_unique($keywords);
1✔
138
            $this->setMetadata(self::META_KEYWORDS, implode(', ', $keywords));
1✔
139
        }
140
    }
1✔
141

142
    public function addKeyword(string $keyword): void
1✔
143
    {
144
        $keywords = $this->getKeywords();
1✔
145
        $keywords[] = $keyword;
1✔
146
        $this->setKeywords(...$keywords);
1✔
147
    }
1✔
148

149
    public function getRobots(): ?string
150
    {
151
        return $this->getMetadata(self::META_ROBOTS);
1✔
152
    }
153

154
    public function setRobots(?string $robots): void
1✔
155
    {
156
        $this->setMetadata(self::META_ROBOTS, $robots);
1✔
157
    }
1✔
158

159
}
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