Completed
Push — master ( 5cfbee...cf7808 )
by Philip
03:04
created

Entity::populateViaRequest()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 15
nc 5
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 == 'integer' ? 'int' : 'float');
48
        } else if ($type == 'boolean') {
49
            $value = $value && $value !== '0';
50
        } else if ($type == 'reference') {
51
            $value = $value !== '' ? $value : null;
52
        }
53
        return $value;
54
    }
55
56
    /**
57
     * Constructor.
58
     *
59
     * @param EntityDefinition $definition
60
     * the definition how this entity looks
61
     */
62
    public function __construct(EntityDefinition $definition) {
63
        $this->definition = $definition;
64
        $this->entity     = [];
65
    }
66
67
    /**
68
     * Sets a field value pair of this entity.
69
     *
70
     * @param string $field
71
     * the field
72
     * @param mixed $value
73
     * the value
74
     */
75
    public function set($field, $value) {
76
        $this->entity[$field] = $value;
77
    }
78
79
    /**
80
     * Gets the raw value of a field no matter what type it is.
81
     * This is usefull for input validation for example.
82
     *
83
     * @param string $field
84
     * the field
85
     *
86
     * @return mixed
87
     * null on invalid field or else the raw value
88
     */
89
    public function getRaw($field) {
90
        if (!array_key_exists($field, $this->entity)) {
91
            return null;
92
        }
93
        return $this->entity[$field];
94
    }
95
96
    /**
97
     * Gets the value of a field in its specific type.
98
     *
99
     * @param string $field
100
     * the field
101
     *
102
     * @return mixed
103
     * null on invalid field, an integer if the definition says that the
104
     * type of the field is an integer, a boolean if the field is a boolean or
105
     * else the raw value
106
     */
107
    public function get($field) {
108
109
        if ($this->definition->getValue($field) !== null) {
110
            return $this->definition->getValue($field);
111
        }
112
113
        if (!array_key_exists($field, $this->entity)) {
114
            return null;
115
        }
116
117
        $type  = $this->definition->getType($field);
118
        $value = $this->toType($this->entity[$field], $type);
119
        return $value;
120
    }
121
122
    /**
123
     * Gets the entity definition.
124
     *
125
     * @return EntityDefinition
126
     * the definition
127
     */
128
    public function getDefinition() {
129
        return $this->definition;
130
    }
131
132
    /**
133
     * Populates the entities fields from the requests parameters.
134
     *
135
     * @param Request $request
136
     * the request to take the field data from
137
     */
138
    public function populateViaRequest(Request $request) {
139
        $fields = $this->definition->getEditableFieldNames();
140
        foreach ($fields as $field) {
141
            $type = $this->definition->getType($field);
142
            if ($type === 'file') {
143
                $file = $request->files->get($field);
144
                if ($file) {
145
                    $this->set($field, $file->getClientOriginalName());
146
                }
147
            } else if ($type === 'many') {
148
                $many = array_map(function($id) {
149
                   return ['id' => $id];
150
                }, $request->get($field));
151
                $this->set($field, $many);
152
            } else {
153
                $this->set($field, $request->get($field));
154
            }
155
        }
156
    }
157
158
}
159