1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
fieldtemplate.php
См. документацию.
1<?php
2
4
18use ReflectionClass;
19
20final class FieldTemplate implements Template
21{
22 private const TYPE_TO_CLASS_MAP = [
23 'integer' => IntegerField::class,
24 'float' => FloatField::class,
25 'boolean' => BooleanField::class,
26 'string' => StringField::class,
27 'text' => TextField::class,
28 'enum' => EnumField::class,
29 'date' => DateField::class,
30 'datetime' => DatetimeField::class,
31 ];
32
33 private const TYPE_SUPPORT_LENGTH_VALIDATOR = [
34 'string',
35 'text',
36 ];
37
38 private array $usedClasses;
39
51 public function __construct(
52 private readonly string $columnName,
53 private readonly string $typeOrm,
54 private readonly bool $isPrimary,
55 private readonly bool $isUnique,
56 private readonly bool $isIncrement,
57 private readonly bool $isNullable,
58 private readonly mixed $defaultValue,
59 private readonly ?int $size,
60 private readonly ?int $maxLength,
61 )
62 {}
63
68 public function getUsedClasses(): array
69 {
70 return $this->usedClasses;
71 }
72
73 public function getContent(): string
74 {
75 [$className, $fullClassName] = $this->getFieldClassName();
76 $this->usedClasses[] = $fullClassName;
77
78 $configureCode = [];
79
80 if ($this->isPrimary)
81 {
82 $configureCode[] = "->configurePrimary()";
83 }
84 elseif (!$this->isNullable)
85 {
86 $configureCode[] = "->configureRequired()";
87 }
88 else
89 {
90 $configureCode[] = "->configureNullable()";
91 }
92
93 if ($this->isIncrement)
94 {
95 $configureCode[] = "->configureAutocomplete()";
96 }
97
98 if ($this->isUnique)
99 {
100 $configureCode[] = "->configureUnique()";
101 }
102
103 if (isset($this->defaultValue))
104 {
105 $configureCode[] = "->configureDefaultValue({$this->getDefaultValueCode()})";
106 }
107
108 if (isset($this->size))
109 {
110 $configureCode[] = "->configureSize({$this->size})";
111 }
112
113 if (isset($this->maxLength) && in_array($this->typeOrm, self::TYPE_SUPPORT_LENGTH_VALIDATOR))
114 {
115 $min = $this->isNullable ? 0 : 1;
116 $max = $this->maxLength;
117
118 $configureCode[] = "->addValidator(new LengthValidator(min:{$min}, max:{$max}))";
119 $this->usedClasses[] = LengthValidator::class;
120 }
121
122 if ($this->typeOrm === 'boolean')
123 {
124 if (in_array($this->defaultValue, ['Y', 'N']))
125 {
126 $configureCode[] = "->configureValues('N', 'Y')";
127 }
128 }
129
130 $resultCode = [
131 "\t\t\t" . "(new {$className}('{$this->columnName}'))",
132 ...array_map(
133 static fn($line) => "\t\t\t\t" . $line,
134 $configureCode,
135 ),
136 "\t\t\t" . ",\n",
137 ];
138
139 return join("\n", $resultCode);
140 }
141
142 private function getFieldClassName(): array
143 {
144 $className = self::TYPE_TO_CLASS_MAP[$this->typeOrm] ?? null;
145 if (empty($className))
146 {
147 throw new ArgumentException("Invalid type '{$this->typeOrm}' for field '{$this->columnName}'");
148 }
149
150 $sortClassName = (new ReflectionClass($className))->getShortName();
151
152 return [$sortClassName, $className];
153 }
154
155 private function getDefaultValueCode(): string
156 {
157 if ($this->typeOrm === 'datetime' || $this->typeOrm === 'date')
158 {
159 $dateFunctions = [
160 'curdate' => true,
161 'current_date' => true,
162 'current_time' => true,
163 'current_timestamp' => true,
164 'curtime' => true,
165 'localtime' => true,
166 'localtimestamp' => true,
167 'now' => true
168 ];
169
170 if (!is_numeric($this->defaultValue))
171 {
172 $prepareValue = mb_strtolower($this->defaultValue);
173 if (
174 mb_strlen($prepareValue) > 2
175 && substr_compare($prepareValue, '()', -2, 2, true) === 0
176 )
177 {
178 $prepareValue = mb_substr($prepareValue, 0, -2);
179 }
180
181 if (isset($dateFunctions[$prepareValue]))
182 {
183 if ($this->typeOrm == 'date')
184 {
185 $this->usedClasses[] = Date::class;
186
187 return "static fn() => Date()";
188 }
189 else
190 {
191 $this->usedClasses[] = DateTime::class;
192
193 return "static fn() => DateTime()";
194 }
195 }
196 }
197 }
198
199 return "'" . addslashes($this->defaultValue) . "'";
200 }
201}
if($_SERVER $defaultValue['REQUEST_METHOD']==="GET" &&!empty($RestoreDefaults) && $bizprocPerms==="W" &&check_bitrix_sessid())
Определения options.php:32
__construct(private readonly string $columnName, private readonly string $typeOrm, private readonly bool $isPrimary, private readonly bool $isUnique, private readonly bool $isIncrement, private readonly bool $isNullable, private readonly mixed $defaultValue, private readonly ?int $size, private readonly ?int $maxLength,)
Определения fieldtemplate.php:51
Определения date.php:9
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
font size
Определения invoice.php:442
$max
Определения template_copy.php:262