AnnotationRegistry::getAnnotation()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 6
c 1
b 1
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\FrameworkExtraBundle\Helper;
7
8
/**
9
 * Provides a global registry for page "annotations", which can be used for meta data such as meta tags, title or
10
 * OpenGraph data. This can be utilized using the Twig {% annotate %} and {% annotations %} {% endannotations %}
11
 * constructs.
12
 */
13
class AnnotationRegistry
14
{
15
    /**
16
     * The annotations
17
     *
18
     * @var array
19
     */
20
    private $annotations = array();
21
22
23
    /**
24
     * Constructor.
25
     */
26
    public function __construct()
27
    {
28
        $this->annotations = array();
29
    }
30
31
32
    /**
33
     * Add an annotation.
34
     *
35
     * @param string $name
36
     * @param mixed $value
37
     * @param int $priority
38
     * @return void
39
     */
40
    public function addAnnotation($name, $value, $priority = 0)
41
    {
42
        $annotation = $this->getAnnotation($name);
43
        $new_annotation = array('name' => $name, 'value' => $value, 'priority' => $priority);
44
        if (!empty($annotation)) {
45
            if ($priority > $annotation['value']['priority']) {
46
                $this->setAnnotation($annotation['key'], $new_annotation);
0 ignored issues
show
Bug introduced by
$new_annotation of type array<string,integer|mixed|string> is incompatible with the type string expected by parameter $annotation of Zicht\Bundle\FrameworkEx...gistry::setAnnotation(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
                $this->setAnnotation($annotation['key'], /** @scrutinizer ignore-type */ $new_annotation);
Loading history...
47
            }
48
        } else {
49
            $this->annotations[]= $new_annotation;
50
        }
51
    }
52
53
    /**
54
     * Get annotation
55
     *
56
     * @param string $name
57
     * @return array
58
     */
59
    public function getAnnotation($name)
60
    {
61
        $match = array();
62
        foreach ($this->getAnnotations() as $key => $annotation) {
63
            if ($annotation['name'] == $name) {
64
                $match['key'] = $key;
65
                $match['value'] = $annotation;
66
            }
67
        }
68
        return $match;
69
    }
70
71
    /**
72
     * Set annotation
73
     *
74
     * @param string $key
75
     * @param string $annotation
76
     */
77
    public function setAnnotation($key, $annotation)
78
    {
79
        if (array_key_exists($key, $this->annotations)) {
80
            $this->annotations[$key] = $annotation;
81
        }
82
    }
83
84
    /**
85
     * Get the annotations.
86
     *
87
     * @return array
88
     */
89
    public function getAnnotations()
90
    {
91
        return $this->annotations;
92
    }
93
94
95
    /**
96
     * Add multiple annotations, or an annotated object that implements the getPublicAnnotations() method.
97
     *
98
     * @param mixed $values
99
     * @return void
100
     */
101
    public function addAnnotations($values)
102
    {
103
        // TODO interface
104
        if (method_exists($values, 'getPublicAnnotations')) {
105
            $this->addAnnotations($values->getPublicAnnotations());
106
        } else {
107
            foreach ($values as $name => $value) {
108
                $this->addAnnotation($name, $value);
109
            }
110
        }
111
    }
112
}
113