Completed
Push — master ( dd6356...9040ae )
by Jacob
9s
created

FieldMetadata::shouldSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace As3\Modlr\Metadata;
4
5
use As3\Modlr\Exception\MetadataException;
6
7
/**
8
 * Abstract field metadata class used for all Entity fields (attributes and relationships).
9
 * Should be loaded using the MetadataFactory, not instantiated directly.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13
abstract class FieldMetadata
14
{
15
    /**
16
     * A friendly description of the field.
17
     *
18
     * @var string
19
     */
20
    public $description;
21
22
    /**
23
     * The field key.
24
     *
25
     * @var string
26
     */
27
    public $key;
28
29
    /**
30
     * Determines if this field came from a mixin.
31
     *
32
     * @var bool
33
     */
34
    public $mixin;
35
36
    /**
37
     * Whether this property should be persisted.
38
     *
39
     * @var bool
40
     */
41
    public $save = true;
42
43
    /**
44
     * Whether this property should be serialized.
45
     *
46
     * @var bool
47
     */
48
    public $serialize = true;
49
50
    /**
51
     * Determines whether this propety is stored in search.
52
     *
53
     * @var bool
54
     */
55
    public $searchProperty = false;
56
57
    /**
58
     * Constructor.
59
     *
60
     * @param   string  $key
61
     * @param   bool    $mixin
62
     */
63
    public function __construct($key, $mixin = false)
64
    {
65
        $this->validateKey($key);
66
        $this->mixin = (Boolean) $mixin;
67
        $this->key = $key;
68
    }
69
70
    /**
71
     * Enables or disables saving of this property.
72
     *
73
     * @param   bool    $bit
74
     * @return  self
75
     */
76
    public function enableSave($bit = true)
77
    {
78
        $this->save = (bool) $bit;
79
        return $this;
80
    }
81
82
    /**
83
     * Enables or disables serialization of this property.
84
     *
85
     * @param   bool    $bit
86
     * @return  self
87
     */
88
    public function enableSerialize($bit = true)
89
    {
90
        $this->serialize = (bool) $bit;
91
        return $this;
92
    }
93
94
    /**
95
     * Gets the field key.
96
     *
97
     * @return  string
98
     */
99
    public function getKey()
100
    {
101
        return $this->key;
102
    }
103
104
    /**
105
     * Determines whether this propety is stored in search.
106
     *
107
     * @return  bool
108
     */
109
    public function isSearchProperty()
110
    {
111
        return $this->searchProperty;
112
    }
113
114
    /**
115
     * Sets whether this property is stored in search.
116
     *
117
     * @param   bool    $bit
118
     * @return  self
119
     */
120
    public function setSearchProperty($bit = true)
121
    {
122
        $this->searchProperty = (Boolean) $bit;
123
        return $this;
124
    }
125
126
    /**
127
     * Whether this property should be saved/persisted to the data layer.
128
     *
129
     * @return  bool
130
     */
131
    public function shouldSave()
132
    {
133
        return $this->save;
134
    }
135
136
    /**
137
     * Whether this property should be serialized.
138
     *
139
     * @return  bool
140
     */
141
    public function shouldSerialize()
142
    {
143
        return $this->serialize;
144
    }
145
146
    /**
147
     * Validates that the field key is not reserved.
148
     *
149
     * @param   string  $key
150
     * @throws  MetadataException
151
     */
152
    protected function validateKey($key)
153
    {
154
        $reserved = ['type', 'id'];
155
        if (in_array(strtolower($key), $reserved)) {
156
            throw MetadataException::reservedFieldKey($key, $reserved);
157
        }
158
    }
159
}
160