|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the SexyField package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dion Snoeijen <[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
|
|
|
declare (strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Tardigrades\FieldType; |
|
15
|
|
|
|
|
16
|
|
|
use ReflectionClass; |
|
17
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
19
|
|
|
use Tardigrades\Entity\SectionInterface; |
|
20
|
|
|
use Tardigrades\SectionField\Generator\CommonSectionInterface; |
|
21
|
|
|
use Tardigrades\SectionField\Service\ReadSectionInterface; |
|
22
|
|
|
use Tardigrades\SectionField\Service\SectionManagerInterface; |
|
23
|
|
|
use Tardigrades\SectionField\ValueObject\FieldConfig; |
|
24
|
|
|
use Tardigrades\SectionField\ValueObject\FieldTypeGeneratorConfig; |
|
25
|
|
|
|
|
26
|
|
|
abstract class FieldType implements FieldTypeInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var FieldConfig */ |
|
29
|
|
|
private $fieldConfig; |
|
30
|
|
|
|
|
31
|
|
|
/** @var FieldTypeGeneratorConfig */ |
|
32
|
|
|
private $fieldTypeGeneratorConfig; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(array $fieldTypeGeneratorConfig = []) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->fieldTypeGeneratorConfig = FieldTypeGeneratorConfig::fromArray($fieldTypeGeneratorConfig); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function setConfig(FieldConfig $fieldConfig): FieldTypeInterface |
|
40
|
|
|
{ |
|
41
|
|
|
$this->fieldConfig = $fieldConfig; |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getFieldTypeGeneratorConfig(): FieldTypeGeneratorConfig |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->fieldTypeGeneratorConfig; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getConfig(): FieldConfig |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->fieldConfig; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function formOptions($sectionEntity): array |
|
57
|
|
|
{ |
|
58
|
|
|
$fieldConfig = $this->getConfig()->toArray(); |
|
59
|
|
|
$options = []; |
|
60
|
|
|
if (!empty($fieldConfig['field']['form'])) { |
|
61
|
|
|
$entryId = $sectionEntity->getId(); |
|
62
|
|
|
$options = $fieldConfig['field']['form']['all']; |
|
63
|
|
|
if (empty($entryId) && !empty($fieldConfig['field']['form']['create'])) { |
|
64
|
|
|
$options = array_merge($options, $fieldConfig['field']['form']['create']); |
|
65
|
|
|
} |
|
66
|
|
|
if (!empty($entryId) && !empty($fieldConfig['field']['form']['update'])) { |
|
67
|
|
|
$options = array_merge($options, $fieldConfig['field']['form']['update']); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (!empty($options['constraints'])) { |
|
72
|
|
|
$options = $this->getFormConstraints($options); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $options; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function getFormConstraints(array $options): array |
|
79
|
|
|
{ |
|
80
|
|
|
$buildConstraints = []; |
|
81
|
|
|
foreach ($options['constraints'] as $validator => $parameters) { |
|
82
|
|
|
$namespace = 'Symfony\\Component\\Validator\\Constraints\\' . $validator; |
|
83
|
|
|
if (class_exists($namespace)) { |
|
84
|
|
|
$buildConstraints[] = new $namespace($parameters); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$options['constraints'] = $buildConstraints; |
|
89
|
|
|
|
|
90
|
|
|
return $options; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function hasEntityEvent(string $event): bool |
|
94
|
|
|
{ |
|
95
|
|
|
try { |
|
96
|
|
|
$entityEvents = $this->getConfig()->getEntityEvents(); |
|
97
|
|
|
} catch (\Exception $exception) { |
|
98
|
|
|
$entityEvents = []; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return in_array($event, $entityEvents); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function directory(): string |
|
105
|
|
|
{ |
|
106
|
|
|
try { |
|
107
|
|
|
$fieldType = new ReflectionClass($this); |
|
108
|
|
|
return pathinfo($fieldType->getFilename(), PATHINFO_DIRNAME); |
|
109
|
|
|
} catch (\ReflectionException $exception) { |
|
110
|
|
|
// No need |
|
111
|
|
|
} |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public static function getCofields(string $handle): array |
|
115
|
|
|
{ |
|
116
|
|
|
return []; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
abstract public function addToForm( |
|
120
|
|
|
FormBuilderInterface $formBuilder, |
|
121
|
|
|
SectionInterface $section, |
|
122
|
|
|
CommonSectionInterface $sectionEntity, |
|
123
|
|
|
SectionManagerInterface $sectionManager, |
|
124
|
|
|
ReadSectionInterface $readSection, |
|
125
|
|
|
Request $request |
|
126
|
|
|
): FormBuilderInterface; |
|
127
|
|
|
} |
|
128
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: