1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace CRUDlex;
13:
14: use CRUDlex\CRUDEntityDefinition;
15: use CRUDlex\CRUDData;
16:
17: 18: 19: 20: 21:
22: class CRUDEntity {
23:
24: 25: 26:
27: protected $definition;
28:
29: 30: 31:
32: protected $entity = array();
33:
34: 35: 36: 37: 38: 39:
40: public function __construct(CRUDEntityDefinition $definition) {
41: $this->definition = $definition;
42: }
43:
44: 45: 46: 47: 48: 49: 50: 51:
52: public function set($field, $value) {
53: $this->entity[$field] = $value;
54: }
55:
56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66:
67: public function get($field) {
68: if (!key_exists($field, $this->entity)) {
69: return null;
70: }
71: $value = $this->entity[$field];
72: switch ($this->definition->getType($field)) {
73: case 'int':
74: $value = intval($value);
75: break;
76: case 'float':
77: $value = floatval($value);
78: break;
79: case 'bool':
80: $value = $value && $value !== '0';
81: break;
82: }
83: return $value;
84: }
85:
86: 87: 88: 89: 90: 91:
92: public function getDefinition() {
93: return $this->definition;
94: }
95:
96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112:
113: public function validate(CRUDData $data) {
114:
115: $fields = $this->definition->getEditableFieldNames();
116: $errors = array();
117: $valid = true;
118: foreach ($fields as $field) {
119: $errors[$field] = array('required' => false, 'unique' => false, 'input' => false);
120:
121:
122: if ($this->definition->isRequired($field) && (!key_exists($field, $this->entity)
123: || $this->entity[$field] === null
124: || $this->entity[$field] === '')) {
125: $errors[$field]['required'] = true;
126: $valid = false;
127: }
128:
129:
130: if ($this->definition->isUnique($field) && key_exists($field, $this->entity) && $this->entity[$field]) {
131: $params = array($field => $this->entity[$field]);
132: $paramsOperators = array($field => '=');
133: if ($this->entity['id'] !== null) {
134: $params['id'] = $this->entity['id'];
135: $paramsOperators['id'] = '!=';
136: }
137: $amount = intval($data->countBy($this->definition->getTable(), $params, $paramsOperators, true));
138: if ($amount > 0) {
139: $errors[$field]['unique'] = true;
140: $valid = false;
141: }
142: }
143:
144:
145: $type = $this->definition->getType($field);
146: if ($type == 'set' && $this->entity[$field]) {
147: $setItems = $this->definition->getSetItems($field);
148: if (!in_array($this->entity[$field], $setItems)) {
149: $errors[$field]['input'] = true;
150: $valid = false;
151: }
152: }
153:
154:
155: $type = $this->definition->getType($field);
156: if ($type == 'int' && $this->entity[$field] !== '' && (string)(int)$this->entity[$field] != $this->entity[$field]) {
157: $errors[$field]['input'] = true;
158: $valid = false;
159: }
160:
161:
162: $type = $this->definition->getType($field);
163: if ($type == 'float' && $this->entity[$field] !== '' && (string)(float)$this->entity[$field] != $this->entity[$field]) {
164: $errors[$field]['input'] = true;
165: $valid = false;
166: }
167:
168:
169: if ($type == 'date' && $this->entity[$field] && \DateTime::createFromFormat('Y-m-d', $this->entity[$field]) === false) {
170: $errors[$field]['input'] = true;
171: $valid = false;
172: }
173:
174:
175: if ($type == 'datetime' && $this->entity[$field] &&
176: \DateTime::createFromFormat('Y-m-d H:i', $this->entity[$field]) === false &&
177: \DateTime::createFromFormat('Y-m-d H:i:s', $this->entity[$field]) === false) {
178: $errors[$field]['input'] = true;
179: $valid = false;
180: }
181:
182:
183: if ($type == 'reference' && $this->entity[$field] !== '') {
184: $params = array('id' => $this->entity[$field]);
185: $paramsOperators = array('id' => '=');
186: $amount = $data->countBy($this->definition->getReferenceTable($field), $params, $paramsOperators, false);
187: if ($amount == 0) {
188: $errors[$field]['input'] = true;
189: $valid = false;
190: }
191: }
192: }
193: return array('valid' => $valid, 'errors' => $errors);
194: }
195:
196: }
197: