You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.6 KiB
107 lines
2.6 KiB
<?php
|
|
|
|
namespace App\UI\Support\Schemas;
|
|
|
|
abstract class AbstractGroupSchema extends AbstractSchema
|
|
{
|
|
/**
|
|
* @var array<string, InputSchema>
|
|
*/
|
|
protected array $fields = [];
|
|
|
|
/**
|
|
* Add one or multiple fields to the container.
|
|
*/
|
|
public function addInput(InputSchema $input): static
|
|
{
|
|
$inputSchema = $input->withContext($this->context)->getSchema();
|
|
$this->fields[$inputSchema->key] = $input;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Resolve input values along with option label snapshots.
|
|
*
|
|
* @param array<string, mixed> $inputValues
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function resolveInputSnapshots(array $inputValues): array
|
|
{
|
|
$resolved = $inputValues;
|
|
|
|
foreach ($this->fields as $key => $schema) {
|
|
$fieldSchema = $schema->getSchema();
|
|
$value = $inputValues[$key] ?? null;
|
|
|
|
if ($fieldSchema->hasLabelSnapshot && ! blank($value)) {
|
|
$labelKey = $fieldSchema->labelKey;
|
|
|
|
// Calling resolveOptionLabel from InputSchema
|
|
$resolved[$labelKey] = $schema->resolveOptionLabel($value, $inputValues);
|
|
}
|
|
}
|
|
|
|
return $resolved;
|
|
}
|
|
|
|
/**
|
|
* Pass dynamic context down to all child fields.
|
|
*/
|
|
public function withContext(array $context): static
|
|
{
|
|
parent::withContext($context);
|
|
|
|
foreach ($this->fields as $field) {
|
|
$field->withContext($this->context);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Resolve all field schemas, keyed by their input keys.
|
|
*
|
|
* @return array<string, InputSchemaField>
|
|
*/
|
|
public function getSchema(): array
|
|
{
|
|
return array_map(function ($schema) {
|
|
return $schema->getSchema();
|
|
}, $this->fields);
|
|
}
|
|
|
|
/**
|
|
* Extract validation rules for FormRequest or Livewire.
|
|
*
|
|
* @return array<string, array>
|
|
*/
|
|
public function getValidationRules(string $prefix = ''): array
|
|
{
|
|
$rules = [];
|
|
|
|
foreach ($this->getSchema() as $key => $schema) {
|
|
$fieldKey = $prefix ? "{$prefix}.{$key}" : $key;
|
|
$rules[$fieldKey] = $schema->rules;
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
/**
|
|
* Extract validation rules for FormRequest or Livewire.
|
|
*
|
|
* @return array<string, array>
|
|
*/
|
|
public function getValidationAttributes(string $prefix = ''): array
|
|
{
|
|
$rules = [];
|
|
|
|
foreach ($this->getSchema() as $key => $schema) {
|
|
$fieldKey = $prefix ? "{$prefix}.{$key}" : $key;
|
|
$rules[$fieldKey] = $schema->label;
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
}
|
|
|