Completed
Pull Request — master (#47)
by Jacob
02:45
created

FieldMetadata::shouldSave()   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
     * Determines whether this propety is stored in search.
45
     *
46
     * @var bool
47
     */
48
    public $searchProperty = false;
49
50
    /**
51
     * Constructor.
52
     *
53
     * @param   string  $key
54
     * @param   bool    $mixin
55
     */
56
    public function __construct($key, $mixin = false)
57
    {
58
        $this->validateKey($key);
59
        $this->mixin = (Boolean) $mixin;
60
        $this->key = $key;
61
    }
62
63
    /**
64
     * Enables or disables saving of this property.
65
     *
66
     * @param   bool    $bit
67
     * @return  self
68
     */
69
    public function enableSave($bit = true)
70
    {
71
        $this->save = (bool) $bit;
72
        return $this;
73
    }
74
75
    /**
76
     * Gets the field key.
77
     *
78
     * @return  string
79
     */
80
    public function getKey()
81
    {
82
        return $this->key;
83
    }
84
85
    /**
86
     * Determines whether this propety is stored in search.
87
     *
88
     * @return  bool
89
     */
90
    public function isSearchProperty()
91
    {
92
        return $this->searchProperty;
93
    }
94
95
    /**
96
     * Sets whether this property is stored in search.
97
     *
98
     * @param   bool    $bit
99
     * @return  self
100
     */
101
    public function setSearchProperty($bit = true)
102
    {
103
        $this->searchProperty = (Boolean) $bit;
104
        return $this;
105
    }
106
107
    /**
108
     * Whether this property should be saved/persisted to the data layer.
109
     *
110
     * @return  bool
111
     */
112
    public function shouldSave()
113
    {
114
        return $this->save;
115
    }
116
117
    /**
118
     * Validates that the field key is not reserved.
119
     *
120
     * @param   string  $key
121
     * @throws  MetadataException
122
     */
123
    protected function validateKey($key)
124
    {
125
        $reserved = ['type', 'id'];
126
        if (in_array(strtolower($key), $reserved)) {
127
            throw MetadataException::reservedFieldKey($key, $reserved);
128
        }
129
    }
130
}
131