Completed
Pull Request — develop (#8)
by
unknown
07:18
created

MetaTag::isEachValueAsSeparateTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Leogout\Bundle\SeoBundle\Model;
4
5
/**
6
 * Description of MetaTag.
7
 *
8
 * @author: leogout
9
 */
10
class MetaTag implements RenderableInterface
11
{
12
    const NAME_TYPE = 'name';
13
    const PROPERTY_TYPE = 'property';
14
    const HTTP_EQUIV_TYPE = 'http-equiv';
15
16
    /**
17
     * @var string
18
     */
19
    protected $type = self::NAME_TYPE;
20
21
    /**
22
     * @var string
23
     */
24
    protected $tagName;
25
26
    /**
27
     * @var string
28
     */
29
    protected $content;
30
31
    /**
32
     * If true, render one metadata tag for each value if the value is an array.
33
     * When false, we'll assume that the rendered metadata tag expects comma separated values.
34
     *
35
     * @var bool
36
     */
37
    protected $eachValueAsSeparateTag = true;
38
39
    /**
40
     * @return string
41
     */
42
    public function getType()
43
    {
44
        return $this->type;
45
    }
46
47
    /**
48
     * @param string $type
49
     *
50
     * @return $this
51
     */
52
    public function setType($type)
53
    {
54
        if (!in_array($type, $this->getTypes())) {
55
            throw new \InvalidArgumentException(sprintf('Meta tag of type "%s" doesn\'t exist. Existing types are: name, property and http-equiv.', $type));
56
        }
57
58
        $this->type = $type;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getTagName()
67
    {
68
        return $this->tagName;
69
    }
70
71
    /**
72
     * @param string $tagName
73
     *
74
     * @return $this
75
     */
76
    public function setTagName($tagName)
77
    {
78
        $this->tagName = (string) $tagName;
79
80
        return $this;
81
    }
82
83
    /**
84
     * @deprecated  use getTagName()
85
     *
86
     * @return string
87
     */
88
    public function getValue()
89
    {
90
        return $this->getTagName();
91
    }
92
93
    /**
94
     * @deprecated  use setTagName()
95
     *
96
     * @param $tagName
97
     *
98
     * @return MetaTag
99
     */
100
    public function setValue($tagName)
101
    {
102
        return $this->setTagName($tagName);
103
    }
104
105
    /**
106
     * @return string[]|string
107
     */
108
    public function getContent()
109
    {
110
        return $this->content;
111
    }
112
113
    /**
114
     * @param string[]|string $content
115
     *
116
     * @return $this
117
     */
118
    public function setContent($content)
119
    {
120
        $this->content = $content;
0 ignored issues
show
Documentation Bug introduced by
It seems like $content can also be of type array<integer,string>. However, the property $content is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
121
122
        return $this;
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function getTypes()
129
    {
130
        return [
131
            self::NAME_TYPE,
132
            self::PROPERTY_TYPE,
133
            self::HTTP_EQUIV_TYPE,
134
        ];
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function render()
141
    {
142
        $tagContent = $this->getContent();
143
        if ($tagContent === null) {
144
            return '';
145
        }
146
147
        // Multiple values?
148
        if (is_array($tagContent)) {
149
            // Render one metadata tag for each value
150
            if ($this->isEachValueAsSeparateTag()) {
151
                $rendered = '';
152
                foreach ($tagContent as $tagValue) {
153
                    $rendered .= sprintf(
154
                        '<meta %s="%s" content="%s" />',
155
                        $this->getType(),
156
                        $this->getTagName(),
157
                        $tagValue
158
                    );
159
                }
160
161
                return $rendered;
162
            } else {
163
                // Assume tag expects comma separated value
164
                $tagContent = implode(', ', $tagContent);
165
            }
166
        }
167
168
        return sprintf('<meta %s="%s" content="%s" />', $this->getType(), $this->getTagName(), (string) $tagContent);
169
    }
170
171
    /**
172
     * @return bool
173
     */
174
    public function isEachValueAsSeparateTag()
175
    {
176
        return $this->eachValueAsSeparateTag;
177
    }
178
179
    /**
180
     * @param bool $eachValueAsSeparateTag
181
     *
182
     * @return $this
183
     */
184
    public function setEachValueAsSeparateTag($eachValueAsSeparateTag)
185
    {
186
        $this->eachValueAsSeparateTag = $eachValueAsSeparateTag;
187
188
        return $this;
189
    }
190
191
    /**
192
     * @return string
193
     */
194
    public function __toString()
195
    {
196
        return $this->render();
197
    }
198
}
199