Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AttributeValueRule
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 test
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace pvc\html\rules\category_rules;
4
5use pvc\html\rules\AbstractRule;
6use pvc\interfaces\html\attribute\AttributeInterface;
7use pvc\interfaces\html\attribute\AttributeWithValueInterface;
8use pvc\interfaces\html\content_model\ContentCategory;
9use pvc\interfaces\html\dom\DomNodeInterface;
10use pvc\interfaces\html\rules\CategoryRuleInterface;
11
12/**
13 * @phpstan-import-type ValueType from AttributeWithValueInterface
14 *
15 * This rule tests to see if the proposed content has a certain attribute
16 * which is set to a certain value or set of values
17 */
18class AttributeValueRule extends AbstractRule implements CategoryRuleInterface
19{
20    /**
21     * @param string $attribute,
22     * @param array<ValueType>|ValueType $value
23     * @param  array<ContentCategory>  $categories
24     */
25    public function __construct(
26        protected string $attribute,
27        /**
28         * attributes store their value(s) in an array, even if it is only a
29         * single value.  On the other hand, there is no content rule where
30         * we have to compare multiple values of a single attribute, so this
31         * parameter is not an array
32         */
33        protected $value,
34        protected array $categories,
35    )
36    {
37    }
38
39    /**
40     * @param  DomNodeInterface  $content
41     *
42     * @return array<ContentCategory>
43     */
44    public function test(DomNodeInterface $content): array
45    {
46        $attribute = $content->getDomElement()->getAttribute($this->attribute);
47        if ($attribute === null) return [];
48
49        assert($attribute instanceof AttributeWithValueInterface);
50        if ($attribute->getValue() == $this->value) return $this->categories;
51
52        return [];
53    }
54}