Field::getDataType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Netdudes\DataSourceryBundle\DataSource\Configuration;
4
5
use Netdudes\DataSourceryBundle\DataSource\Configuration\FieldInterface;
6
use Netdudes\DataSourceryBundle\DataType\DataTypeInterface;
7
8
/**
9
 * This is an extension of the DataSourceField with the specific information needed for
10
 * the implementation of a DataSource backed by a QueryBuilder-constructing Doctrine-based
11
 * database source.
12
 */
13
class Field implements FieldInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $uniqueName;
19
20
    /**
21
     * @var string
22
     */
23
    protected $readableName;
24
25
    /**
26
     * @var string
27
     */
28
    protected $description;
29
30
    /**
31
     * @var \Netdudes\DataSourceryBundle\DataType\DataTypeInterface
32
     */
33
    protected $dataType;
34
35
    /**
36
     * Arbitrary metadata to be passed around for the front-end
37
     * when this instance is JSON-serialized.
38
     *
39
     * @var array
40
     */
41
    protected $metaData;
42
43
    /**
44
     * Returns, if it applies, an array of possible choices for this DataSourceField. This allows for
45
     * complex UXs to be implemented, such as intelligent predictions and autocompletes.
46
     *
47
     * @var null|mixed[]
48
     */
49
    protected $choices = null;
50
51
    /**
52
     * @var string
53
     */
54
    private $databaseFilterQueryField;
55
56
    /**
57
     * @var string|string[]
58
     */
59
    private $databaseSelectAlias;
60
61
    /**
62
     * @param                   $uniqueName
63
     * @param                   $readableName
64
     * @param                   $description
65
     * @param DataTypeInterface $dataType
66
     * @param                   $databaseFilterQueryField
67
     * @param null              $databaseSelectAlias
68
     * @param array             $choices
69
     */
70
    public function __construct(
71
        $uniqueName,
72
        $readableName,
73
        $description,
74
        DataTypeInterface $dataType,
75
        $databaseFilterQueryField = null,
76
        $databaseSelectAlias = null,
77
        array $choices = null
78
    ) {
79
        $this->uniqueName = $uniqueName;
80
        $this->description = $description;
81
        $this->dataType = $dataType;
82
        $this->readableName = $readableName;
83
        $this->metaData = [];
84
        $this->databaseFilterQueryField = $databaseFilterQueryField;
85
        $this->databaseSelectAlias = $databaseSelectAlias;
86
        if (!is_null($choices)) {
87
            $this->choices = $choices;
88
        }
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getUniqueName()
95
    {
96
        return $this->uniqueName;
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getReadableName()
103
    {
104
        return $this->readableName;
105
    }
106
107
    /**
108
     * @return mixed[]|null
109
     */
110
    public function getChoices()
111
    {
112
        return $this->choices;
113
    }
114
115
    /**
116
     * @return DataTypeInterface
117
     */
118
    public function getDataType()
119
    {
120
        return $this->dataType;
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getDescription()
127
    {
128
        return $this->description;
129
    }
130
131
    /**
132
     * (PHP 5 &gt;= 5.4.0)<br/>
133
     * Specify data which should be serialized to JSON
134
     *
135
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
136
     * @return mixed data which can be serialized by <b>json_encode</b>,
137
     *               which is a value of any type other than a resource.
138
     */
139
    public function jsonSerialize()
140
    {
141
        $serialization = [
142
            'uniqueName' => $this->uniqueName,
143
            'readableName' => $this->readableName,
144
            'description' => $this->description,
145
            'type' => $this->dataType->jsonSerialize(),
146
            'metadata' => $this->metaData,
147
        ];
148
149
        if (!is_null($this->choices)) {
150
            $serialization['choices'] = $this->choices;
151
        }
152
153
        return $serialization;
154
    }
155
156
    /**
157
     * @return array
158
     */
159
    public function getMetaData()
160
    {
161
        return $this->metaData;
162
    }
163
164
    /**
165
     * @param array $metaData
166
     */
167
    public function setMetaData($metaData)
168
    {
169
        $this->metaData = $metaData;
170
    }
171
172
    /**
173
     * Returns the string alias or array combination of aliases to retrieve the data from the database
174
     *
175
     * @return string|string[]
176
     */
177
    public function getDatabaseSelectAlias()
178
    {
179
        return is_null($this->databaseSelectAlias) ? str_replace('.', '_', $this->databaseFilterQueryField) : $this->databaseSelectAlias;
180
    }
181
182
    /**
183
     * Returns the database query field used for filtering and searching
184
     *
185
     * @return string
186
     */
187
    public function getDatabaseFilterQueryField()
188
    {
189
        return $this->databaseFilterQueryField;
190
    }
191
}
192