Completed
Push — master ( e24a08...649c71 )
by Joshua
9s
created

AttributesTrait::validateAttribute()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 1
nc 1
1
<?php
2
3
namespace As3\Modlr\Metadata\Traits;
4
5
use As3\Modlr\Exception\MetadataException;
6
use As3\Modlr\Metadata\AttributeMetadata;
7
8
/**
9
 * Common attribute metadata get, set, and add methods.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13
trait AttributesTrait
14
{
15
    /**
16
     * All attribute fields assigned to this metadata object.
17
     * An attribute is a "standard" field, such as a string, integer, array, etc.
18
     *
19
     * @var AttributeMetadata[]
20
     */
21
    public $attributes = [];
22
23
    /**
24
     * Adds an attribute field.
25
     *
26
     * @param   AttributeMetadata   $attribute
27
     * @return  self
28
     */
29
    public function addAttribute(AttributeMetadata $attribute)
30
    {
31
        $this->validateAttribute($attribute);
32
        $this->attributes[$attribute->getKey()] = $attribute;
33
        ksort($this->attributes);
34
        return $this;
35
    }
36
37
    /**
38
     * Determines if an attribute supports autocomplete functionality.
39
     *
40
     * @param   string  $key    The attribute key.
41
     * @return  bool
42
     */
43
    public function attrSupportsAutocomplete($key)
44
    {
45
        return isset($this->getAutocompleteAttributes()[$key]);
46
    }
47
48
    /**
49
     * Gets an attribute field.
50
     * Returns null if the attribute does not exist.
51
     *
52
     * @param   string  $key
53
     * @return  AttributeMetadata|null
54
     */
55
    public function getAttribute($key)
56
    {
57
        if (!isset($this->attributes[$key])) {
58
            return null;
59
        }
60
        return $this->attributes[$key];
61
    }
62
63
    /**
64
     * Gets all attribute fields.
65
     *
66
     * @return  AttributeMetadata[]
67
     */
68
    public function getAttributes()
69
    {
70
        return $this->attributes;
71
    }
72
73
    /**
74
     * Gets all properties that are flagged for autocomplete in search.
75
     *
76
     * @return  AttributeMetadata[]
77
     */
78
    public function getAutocompleteAttributes()
79
    {
80
        static $attrs;
81
        if (null !== $attrs) {
82
            return $attrs;
83
        }
84
85
        $attrs = [];
86
        foreach ($this->getAttributes() as $key => $attribute) {
87
            if (false === $attribute->hasAutocomplete()) {
88
                continue;
89
            }
90
            $attrs[$key] = $attribute;
91
        }
92
        return $attrs;
93
    }
94
95
    /**
96
     * Determines if an attribute field exists.
97
     *
98
     * @param   string  $key
99
     * @return  bool
100
     */
101
    public function hasAttribute($key)
102
    {
103
        return null !== $this->getAttribute($key);
104
    }
105
106
    /**
107
     * Determines any attribute fields exist.
108
     *
109
     * @return  bool
110
     */
111
    public function hasAttributes()
112
    {
113
        return !empty($this->attributes);
114
    }
115
116
    /**
117
     * Validates that the attribute can be added.
118
     *
119
     * @param   AttributeMetadata   $attribute
120
     * @return  self
121
     * @throws  MetadataException
122
     */
123
    abstract protected function validateAttribute(AttributeMetadata $attribute);
124
}
125