Completed
Push — master ( 633bdf...3f207b )
by Philip
02:19
created

Entity::populateViaRequest()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 28
rs 5.3846
c 2
b 0
f 0
cc 8
eloc 22
nc 8
nop 1
1
<?php
2
3
/*
4
 * This file is part of the CRUDlex package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CRUDlex;
13
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Represents a single set of data in field value pairs like the row in a
18
 * database. Depends of course on the {@see AbstractData} implementation being used.
19
 * With this objects, the data is passed arround and validated.
20
 */
21
class Entity {
22
23
    /**
24
     * The {@see EntityDefinition} defining how this entity looks like.
25
     */
26
    protected $definition;
27
28
    /**
29
     * Holds the key value data of the entity.
30
     */
31
    protected $entity;
32
33
34
    /**
35
     * Converts a given value to the given type.
36
     *
37
     * @param mixed $value
38
     * the value to convert
39
     * @param string $type
40
     * the type to convert to like 'integer' or 'float'
41
     *
42
     * @return mixed
43
     * the converted value
44
     */
45
    protected function toType($value, $type) {
46
        if (in_array($type, ['integer', 'float']) && $value !== '' && $value !== null) {
47
            settype($value, $type);
48
        } else if ($type == 'boolean') {
49
            $value = (bool)$value;
50
        } else if ($type == 'many') {
51
            $value = $value ?: [];
52
        } else if (in_array($type, ['datetime', 'date', 'reference'])) {
53
            $value = $value === '' ? null : $value;
54
        }
55
        return $value;
56
    }
57
58
59
    /**
60
     * Constructor.
61
     *
62
     * @param EntityDefinition $definition
63
     * the definition how this entity looks
64
     */
65
    public function __construct(EntityDefinition $definition) {
66
        $this->definition = $definition;
67
        $this->entity     = [];
68
    }
69
70
    /**
71
     * Sets a field value pair of this entity.
72
     *
73
     * @param string $field
74
     * the field
75
     * @param mixed $value
76
     * the value
77
     */
78
    public function set($field, $value) {
79
        $this->entity[$field] = $value;
80
    }
81
82
    /**
83
     * Gets the raw value of a field no matter what type it is.
84
     * This is usefull for input validation for example.
85
     *
86
     * @param string $field
87
     * the field
88
     *
89
     * @return mixed
90
     * null on invalid field or else the raw value
91
     */
92
    public function getRaw($field) {
93
        if (!array_key_exists($field, $this->entity)) {
94
            return null;
95
        }
96
        return $this->entity[$field];
97
    }
98
99
    /**
100
     * Gets the value of a field in its specific type.
101
     *
102
     * @param string $field
103
     * the field
104
     *
105
     * @return mixed
106
     * null on invalid field, an integer if the definition says that the
107
     * type of the field is an integer, a boolean if the field is a boolean or
108
     * else the raw value
109
     */
110
    public function get($field) {
111
112
        if ($this->definition->getField($field, 'value') !== null) {
113
            return $this->definition->getField($field, 'value');
114
        }
115
116
        if (!array_key_exists($field, $this->entity)) {
117
            return null;
118
        }
119
120
        $type  = $this->definition->getType($field);
121
        $value = $this->toType($this->entity[$field], $type);
122
        return $value;
123
    }
124
125
    /**
126
     * Gets the entity definition.
127
     *
128
     * @return EntityDefinition
129
     * the definition
130
     */
131
    public function getDefinition() {
132
        return $this->definition;
133
    }
134
135
    /**
136
     * Populates the entities fields from the requests parameters.
137
     *
138
     * @param Request $request
139
     * the request to take the field data from
140
     */
141
    public function populateViaRequest(Request $request) {
142
        $fields = $this->definition->getEditableFieldNames();
143
        foreach ($fields as $field) {
144
            $type = $this->definition->getType($field);
145
            if ($type === 'file') {
146
                $file = $request->files->get($field);
147
                if ($file) {
148
                    $this->set($field, $file->getClientOriginalName());
149
                }
150
            } else if ($type === 'reference') {
151
                $value = $request->get($field);
152
                if ($value === '') {
153
                    $value = null;
154
                }
155
                $this->set($field, ['id' => $value]);
156
            } else if ($type === 'many') {
157
                $array = $request->get($field, []);
158
                if (is_array($array)) {
159
                    $many = array_map(function($id) {
160
                        return ['id' => $id];
161
                    }, $array);
162
                    $this->set($field, $many);
163
                }
164
            } else {
165
                $this->set($field, $request->get($field));
166
            }
167
        }
168
    }
169
170
}
171