Completed
Push — master ( e3578c...e57194 )
by Philip
06:05 queued 02:46
created

CRUDEntityDefinition::isConstraint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
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
class CRUDEntityDefinition {
15
16
    /**
17
     * The table where the data is stored.
18
     */
19
    protected $table;
20
21
    /**
22
     * Holds all fields in the same structure as in the CRUD YAML file.
23
     */
24
    protected $fields;
25
26
    /**
27
     * The label for the entity.
28
     */
29
    protected $label;
30
31
    /**
32
     * The labels  of the entity in the locales.
33
     */
34
    protected $localeLabels;
35
36
    /**
37
     * An array with the children referencing the entity. All entries are
38
     * arrays with three referencing elements: table, fieldName, entity
39
     */
40
    protected $children;
41
42
    /**
43
     * Labels for the fields "id", "created_at" and "updated_at".
44
     */
45
    protected $standardFieldLabels;
46
47
    /**
48
     * An array containing the fields which should appear in the list view
49
     * of the entity.
50
     */
51
    protected $listFields;
52
53
    /**
54
     * The fields used to display the children on the details page of an entity.
55
     * The keys are the entity names as in the CRUD YAML and the values are the
56
     * field names.
57
     */
58
    protected $childrenLabelFields;
59
60
    /**
61
     * Whether to delete its children when an instance is deleted.
62
     */
63
    protected $deleteCascade;
64
65
    /**
66
     * The amount of items to display per page on the listview.
67
     */
68
    protected $pageSize;
69
70
    /**
71
     * The fields offering to be filtered.
72
     */
73
    protected $filter;
74
75
    /**
76
     * Holds the {@see CRUDServiceProvider}.
77
     */
78
    protected $serviceProvider;
79
80
    /**
81
     * Holds the locale.
82
     */
83
    protected $locale;
84
85
    /**
86
     * Holds the initial sort field.
87
     */
88
    protected $initialSortField;
89
90
    /**
91
     * Holds the initial sort order.
92
     */
93
    protected $initialSortAscending;
94
95
    /**
96
     * Gets the field names exluding the given ones.
97
     *
98
     * @param array $exclude
99
     * the field names to exclude
100
     *
101
     * @return array
102
     * all field names excluding the given ones
103
     */
104
    protected function getFilteredFieldNames(array $exclude) {
105
        $fieldNames = $this->getFieldNames();
106
        $result = array();
107
        foreach ($fieldNames as $fieldName) {
108
            if (!in_array($fieldName, $exclude)) {
109
                $result[] = $fieldName;
110
            }
111
        }
112
        return $result;
113
    }
114
115
    /**
116
     * Gets the value of a field key.
117
     *
118
     * @param string $name
119
     * the name of the field
120
     * @param string $key
121
     * the value of the key
122
     *
123
     * @return mixed
124
     * the value of the field key or null if not existing
125
     */
126
    protected function getFieldValue($name, $key) {
127
        if (array_key_exists($name, $this->fields) && array_key_exists($key, $this->fields[$name])) {
128
            return $this->fields[$name][$key];
129
        }
130
        return null;
131
    }
132
133
    /**
134
     * Sets the value of a field key. If the field or the key in the field
135
     * don't exist, they get created.
136
     *
137
     * @param string $name
138
     * the name of the field
139
     * @param string $key
140
     * the value of the key
141
     * @param mixed $value
142
     * the new value
143
     */
144
    protected function setFieldValue($name, $key, $value) {
145
        if (!array_key_exists($name, $this->fields)) {
146
            $this->fields[$name] = array();
147
        }
148
        $this->fields[$name][$key] = $value;
149
    }
150
151
    /**
152
     * Gets the value of a reference field.
153
     *
154
     * @param string $fieldName
155
     * the field name of the reference
156
     * @param string $key
157
     * the key of the reference value
158
     *
159
     * @return string
160
     * the value of the reference field
161
     */
162
    protected function getReferenceValue($fieldName, $key) {
163
        if ($this->getType($fieldName) != 'reference') {
164
            return null;
165
        }
166
        if (!array_key_exists('reference', $this->fields[$fieldName])) {
167
            return null;
168
        }
169
        if (!array_key_exists($key, $this->fields[$fieldName]['reference'])) {
170
            return null;
171
        }
172
        return $this->fields[$fieldName]['reference'][$key];
173
    }
174
175
    /**
176
     * Checks if the given field has the given constraint.
177
     *
178
     * @param string $fieldName
179
     * the field name maybe having the constraint
180
     * @param string $constraint
181
     * the constraint to check, 'required' or 'unique'
182
     *
183
     * @return boolean
184
     * true if the given field has the given constraint
185
     */
186
    protected function isConstraint($fieldName, $constraint) {
187
        $result = $this->getFieldValue($fieldName, $constraint);
188
        if ($result === null) {
189
            $result = false;
190
        }
191
        return $result;
192
    }
193
194
    /**
195
     * Constructor.
196
     *
197
     * @param string $table
198
     * the table of the entity
199
     * @param array $fields
200
     * the fieldstructure just like the CRUD YAML
201
     * @param string $label
202
     * the label of the entity
203
     * @param array $localeLabels
204
     * the labels  of the entity in the locales
205
     * @param array $standardFieldLabels
206
     * labels for the fields "id", "created_at" and "updated_at"
207
     * @param CRUDServiceProvider $serviceProvider
208
     * The current service provider
209
     */
210
    public function __construct($table, array $fields, $label, $localeLabels, array $standardFieldLabels, CRUDServiceProvider $serviceProvider) {
211
        $this->table = $table;
212
        $this->fields = $fields;
213
        $this->label = $label;
214
        $this->localeLabels = $localeLabels;
215
        $this->standardFieldLabels = $standardFieldLabels;
216
        $this->serviceProvider = $serviceProvider;
217
218
        $this->children = array();
219
        $this->listFields = array();
220
        $this->childrenLabelFields = array();
221
        $this->filter = array();
222
        $this->deleteCascade = false;
223
        $this->pageSize = 25;
224
        $this->locale = null;
225
        $this->initialSortField = 'id';
226
        $this->initialSortAscending = true;
227
    }
228
229
    /**
230
     * Gets all field names, including the implicit ones like "id" or
231
     * "created_at".
232
     *
233
     * @return array
234
     * the field names
235
     */
236
    public function getFieldNames() {
237
        $fieldNames = $this->getReadOnlyFields();
238
        foreach ($this->fields as $field => $value) {
239
            $fieldNames[] = $field;
240
        }
241
        return $fieldNames;
242
    }
243
244
    /**
245
     * Sets the field names to be used in the listview.
246
     *
247
     * @param array $listFields
248
     * the field names to be used in the listview
249
     */
250
    public function setListFieldNames(array $listFields) {
251
        $this->listFields = $listFields;
252
    }
253
254
    /**
255
     * Gets the field names to be used in the listview. If they were not specified,
256
     * all public field names are returned.
257
     *
258
     * @return array
259
     * the field names to be used in the listview
260
     */
261
    public function getListFieldNames() {
262
        if (!empty($this->listFields)) {
263
            return $this->listFields;
264
        }
265
        return $this->getPublicFieldNames();
266
    }
267
268
    /**
269
     * Gets the fields used to display the children on the details page of an
270
     * entity. The keys are the entity names as in the CRUD YAML and the values
271
     * are the field names.
272
     *
273
     * @return array
274
     * the fields used to display the children on the details page
275
     */
276
    public function getChildrenLabelFields() {
277
        return $this->childrenLabelFields;
278
    }
279
280
    /**
281
     * Sets the fields used to display the children on the details page of an
282
     * entity. The keys are the entity names as in the CRUD YAML and the values
283
     * are the field names.
284
     *
285
     * @param array $childrenLabelFields
286
     * the fields used to display the children on the details page
287
     */
288
    public function setChildrenLabelFields(array $childrenLabelFields) {
289
        $this->childrenLabelFields = $childrenLabelFields;
290
    }
291
292
    /**
293
     * Gets whether to delete its children when an instance is deleted.
294
     *
295
     * @return boolean
296
     * true if so
297
     */
298
    public function isDeleteCascade() {
299
        return $this->deleteCascade;
300
    }
301
302
    /**
303
     * Sets whether to delete its children when an instance is deleted.
304
     *
305
     * @param boolean $deleteCascade
306
     * whether to delete its children when an instance is deleted
307
     */
308
    public function setDeleteCascade($deleteCascade) {
309
        $this->deleteCascade = $deleteCascade;
310
    }
311
312
    /**
313
     * Gets the amount of items to display per page on the listview.
314
     *
315
     * @return integer
316
     * the amount of items to display per page on the listview
317
     */
318
    public function getPageSize() {
319
        return $this->pageSize;
320
    }
321
322
    /**
323
     * Sets the amount of items to display per page on the listview.
324
     *
325
     * @param integer $pageSize
326
     * the amount of items to display per page on the listview
327
     */
328
    public function setPageSize($pageSize) {
329
        $this->pageSize = $pageSize;
330
    }
331
332
    /**
333
     * Gets the fields offering a filter.
334
     *
335
     * @return array
336
     * the fields to filter
337
     */
338
    public function getFilter() {
339
        return $this->filter;
340
    }
341
342
    /**
343
     * Sets the fields offering a filter.
344
     *
345
     * @param array $filter
346
     * the fields to filter
347
     */
348
    public function setFilter(array $filter) {
349
        $this->filter = $filter;
350
    }
351
352
    /**
353
     * Gets the service provider.
354
     *
355
     * @return CRUDServiceProvider
356
     * the service provider
357
     */
358
    public function getServiceProvider() {
359
        return $this->serviceProvider;
360
    }
361
362
    /**
363
     * Sets the service provider.
364
     *
365
     * @param CRUDServiceProvider $serviceProvider
366
     * the new service provider
367
     */
368
    public function setServiceProvider(CRUDServiceProvider $serviceProvider) {
369
        return $this->serviceProvider = $serviceProvider;
370
    }
371
372
    /**
373
     * Gets the public field names. The internal fields "version" and
374
     * "deleted_at" are filtered.
375
     *
376
     * @return array
377
     * the public field names
378
     */
379
    public function getPublicFieldNames() {
380
        $exclude = array('version', 'deleted_at');
381
        $result = $this->getFilteredFieldNames($exclude);
382
        return $result;
383
    }
384
385
    /**
386
     * Gets the field names which are editable. Not editable are fields like the
387
     * id or the created_at.
388
     *
389
     * @return array
390
     * the editable field names
391
     */
392
    public function getEditableFieldNames() {
393
        $result = $this->getFilteredFieldNames($this->getReadOnlyFields());
394
        return $result;
395
    }
396
397
    /**
398
     * Gets the read only field names like the id or the created_at.
399
     *
400
     * @return array
401
     * the read only field names
402
     */
403
    public function getReadOnlyFields() {
404
        return array('id', 'created_at', 'updated_at', 'version', 'deleted_at');
405
    }
406
407
    /**
408
     * Gets the type of a field.
409
     *
410
     * @param string $fieldName
411
     * the field name
412
     *
413
     * @return string
414
     * the type or null on invalid field name
415
     */
416
    public function getType($fieldName) {
417
        switch ($fieldName) {
418
            case 'id':
419
                return 'string';
420
            case 'created_at':
421
            case 'updated_at':
422
            case 'deleted_at':
423
                return 'datetime';
424
            case 'version':
425
                return 'int';
426
        }
427
        return $this->getFieldValue($fieldName, 'type');
428
    }
429
430
    /**
431
     * Sets the type of a field.
432
     *
433
     * @param string $fieldName
434
     * the field name
435
     * @param string $value
436
     * the new field type
437
     */
438
    public function setType($fieldName, $value) {
439
        return $this->setFieldValue($fieldName, 'type', $value);
440
    }
441
442
    /**
443
     * Gets whether a field is required.
444
     *
445
     * @param string $fieldName
446
     * the field name
447
     *
448
     * @return boolean
449
     * true if so
450
     */
451
    public function isRequired($fieldName) {
452
        return $this->isConstraint($fieldName, 'required');
453
    }
454
455
    /**
456
     * Sets whether a field is required.
457
     *
458
     * @param string $fieldName
459
     * the field name
460
     * @param boolean $fieldName
461
     * the new required state
462
     */
463
    public function setRequired($fieldName, $value) {
464
        return $this->setFieldValue($fieldName, 'required', $value);
465
    }
466
467
    /**
468
     * Gets whether a field is unique.
469
     *
470
     * @param string $fieldName
471
     * the field name
472
     *
473
     * @return boolean
474
     * true if so
475
     */
476
    public function isUnique($fieldName) {
477
        return $this->isConstraint($fieldName, 'unique');
478
    }
479
480
    /**
481
     * Sets whether a field is unique.
482
     *
483
     * @param string $fieldName
484
     * the field name
485
     *
486
     * @param boolean $value
487
     * true if so
488
     */
489
    public function setUnique($fieldName, $value) {
490
        return $this->setFieldValue($fieldName, 'unique', $value);
491
    }
492
493
    /**
494
     * Gets the table field of a reference.
495
     *
496
     * @param string $fieldName
497
     * the field name of the reference
498
     *
499
     * @return string
500
     * the table field of a reference or null on invalid field name
501
     */
502
    public function getReferenceTable($fieldName) {
503
        return $this->getReferenceValue($fieldName, 'table');
504
    }
505
506
    /**
507
     * Gets the name field of a reference.
508
     *
509
     * @param string $fieldName
510
     * the field name of the reference
511
     *
512
     * @return string
513
     * the name field of a reference or null on invalid field name
514
     */
515
    public function getReferenceNameField($fieldName) {
516
        return $this->getReferenceValue($fieldName, 'nameField');
517
    }
518
519
    /**
520
     * Gets the entity field of a reference.
521
     *
522
     * @param string $fieldName
523
     * the field name of the reference
524
     *
525
     * @return string
526
     * the entity field of a reference or null on invalid field name
527
     */
528
    public function getReferenceEntity($fieldName) {
529
        return $this->getReferenceValue($fieldName, 'entity');
530
    }
531
532
    /**
533
     * Gets the file path of a field.
534
     *
535
     * @param string $fieldName
536
     * the field name
537
     *
538
     * @return string
539
     * the file path of a field or null on invalid field name
540
     */
541
    public function getFilePath($fieldName) {
542
        return $this->getFieldValue($fieldName, 'filepath');
543
    }
544
545
    /**
546
     * Sets the file path of a field.
547
     *
548
     * @param string $fieldName
549
     * the field name
550
     * @param string $value
551
     * the file path of a field or null on invalid field name
552
     */
553
    public function setFilePath($fieldName, $value) {
554
        $this->setFieldValue($fieldName, 'filepath', $value);
555
    }
556
557
    /**
558
     * Gets the value of a fixed field.
559
     *
560
     * @param string $fieldName
561
     * the field name
562
     *
563
     * @return string
564
     * the value of a fixed field or null on invalid field name
565
     */
566
    public function getFixedValue($fieldName) {
567
        return $this->getFieldValue($fieldName, 'fixedvalue');
568
    }
569
570
    /**
571
     * Sets the value of a fixed field.
572
     *
573
     * @param string $fieldName
574
     * the field name
575
     * @param string $value
576
     * the new value for the fixed field
577
     */
578
    public function setFixedValue($fieldName, $value) {
579
        return $this->setFieldValue($fieldName, 'fixedvalue', $value);
580
    }
581
582
    /**
583
     * Gets the items of a set field.
584
     *
585
     * @param string $fieldName
586
     * the field name
587
     *
588
     * @return array
589
     * the items of the set field or null on invalid field name
590
     */
591
    public function getSetItems($fieldName) {
592
        return $this->getFieldValue($fieldName, 'setitems');
593
    }
594
595
    /**
596
     * Sets the items of a set field.
597
     *
598
     * @param string $fieldName
599
     * the field name
600
     * @param string $value
601
     * the new items of the set field
602
     */
603
    public function setSetItems($fieldName, $value) {
604
        return $this->setFieldValue($fieldName, 'setitems', $value);
605
    }
606
607
    /**
608
     * Gets the step size of a float field.
609
     *
610
     * @param string $fieldName
611
     * the field name
612
     *
613
     * @return array
614
     * the step size of a float field or null on invalid field name
615
     */
616
    public function getFloatStep($fieldName) {
617
        return $this->getFieldValue($fieldName, 'floatStep');
618
    }
619
620
    /**
621
     * Sets the step size of a float field.
622
     *
623
     * @param string $fieldName
624
     * the field name
625
     * @param string $value
626
     * the new step size of the float field
627
     */
628
    public function setFloatStep($fieldName, $value) {
629
        $this->setFieldValue($fieldName, 'floatStep', $value);
630
    }
631
632
    /**
633
     * Gets the label of a field.
634
     *
635
     * @param string $fieldName
636
     * the field name
637
     *
638
     * @return string
639
     * the label of the field or the field name if no label is set in the CRUD
640
     * YAML
641
     */
642
    public function getFieldLabel($fieldName) {
643
644
        $result = null;
645
646
        if ($this->locale) {
647
            $result = $this->getFieldValue($fieldName, 'label_'.$this->locale);
648
        }
649
650
        if ($result === null) {
651
            $result = $this->getFieldValue($fieldName, 'label');
652
        }
653
654
        if ($result === null && array_key_exists($fieldName, $this->standardFieldLabels)) {
655
            $result = $this->standardFieldLabels[$fieldName];
656
        }
657
        if ($result === null) {
658
            $result = $fieldName;
659
        }
660
        return $result;
661
    }
662
663
    /**
664
     * Gets the label of a field.
665
     *
666
     * @param string $fieldName
667
     * the field name
668
     * @param string $value
669
     * the new label of the field
670
     */
671
    public function setFieldLabel($fieldName, $value) {
672
        return $this->setFieldValue($fieldName, 'label', $value);
673
    }
674
675
    /**
676
     * Gets the table where the data is stored.
677
     *
678
     * @return string
679
     * the table where the data is stored
680
     */
681
    public function getTable() {
682
        return $this->table;
683
    }
684
685
    /**
686
     * Sets the table where the data is stored.
687
     *
688
     * @param string $table
689
     * the new table where the data is stored
690
     */
691
    public function setTable($table) {
692
        $this->table = $table;
693
    }
694
695
    /**
696
     * Gets the label for the entity.
697
     *
698
     * @return string
699
     * the label for the entity
700
     */
701
    public function getLabel() {
702
        if ($this->locale && array_key_exists($this->locale, $this->localeLabels)) {
703
            return $this->localeLabels[$this->locale];
704
        }
705
        return $this->label;
706
    }
707
708
    /**
709
     * Sets the label for the entity.
710
     *
711
     * @param string $label
712
     * the new label for the entity
713
     */
714
    public function setLabel($label) {
715
        $this->label = $label;
716
    }
717
718
    /**
719
     * Gets the description of a field.
720
     *
721
     * @param string $fieldName
722
     * the field name
723
     *
724
     * @return string
725
     * the description of the field
726
     */
727
    public function getDescription($fieldName) {
728
        return $this->getFieldValue($fieldName, 'description');
729
    }
730
731
    /**
732
     * Sets the description of a field.
733
     *
734
     * @param string $fieldName
735
     * the field name
736
     * @param string $value
737
     * the new description of the field
738
     */
739
    public function setDescription($fieldName, $value) {
740
        $this->setFieldValue($fieldName, 'description', $value);
741
    }
742
743
    /**
744
     * Adds a child to this definition in case the other
745
     * definition has a reference to this one.
746
     *
747
     * @param string $table
748
     * the table of the referencing definition
749
     * @param string $fieldName
750
     * the field name of the referencing definition
751
     * @param string $entity
752
     * the entity of the referencing definition
753
     */
754
    public function addChild($table, $fieldName, $entity) {
755
        $this->children[] = array($table, $fieldName, $entity);
756
    }
757
758
    /**
759
     * Gets the referencing children to this definition.
760
     *
761
     * @return array
762
     * an array with the children referencing the entity. All entries are arrays
763
     * with three referencing elements: table, fieldName, entity
764
     */
765
    public function getChildren() {
766
        return $this->children;
767
    }
768
769
    /**
770
     * Sets the locale to be used.
771
     *
772
     * @param string $locale
773
     * the locale to be used.
774
     */
775
    public function setLocale($locale) {
776
        $this->locale = $locale;
777
    }
778
779
    /**
780
     * Sets the initial sort field.
781
     *
782
     * @param string $initialSortField
783
     * the new initial sort field
784
     */
785
    public function setInitialSortField($initialSortField) {
786
        $this->initialSortField = $initialSortField;
787
    }
788
789
    /**
790
     * Gets the initial sort field.
791
     *
792
     * @return string
793
     * the initial sort field
794
     */
795
    public function getInitialSortField() {
796
        return $this->initialSortField;
797
    }
798
799
    /**
800
     * Sets the initial sort order.
801
     *
802
     * @param boolean $initialSortAscending
803
     * the initial sort order, true if ascending
804
     */
805
    public function setInitialSortAscending($initialSortAscending) {
806
        $this->initialSortAscending = $initialSortAscending;
807
    }
808
809
    /**
810
     * Gets the initial sort order.
811
     *
812
     * @return boolean
813
     * the initial sort order, true if ascending
814
     */
815
    public function getInitialSortAscending() {
816
        return $this->initialSortAscending;
817
    }
818
}
819