2024-03-07 00:58:14 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Form\Data;
|
|
|
|
|
2024-12-12 00:30:59 +01:00
|
|
|
use App\Form\Contracts\Filterable;
|
2024-03-15 00:28:57 +01:00
|
|
|
use App\Form\Enums\SpecialType;
|
2024-03-07 00:58:14 +01:00
|
|
|
use App\Form\Fields\Field;
|
|
|
|
use App\Form\Fields\NamiField;
|
2024-03-14 23:54:41 +01:00
|
|
|
use App\Form\Models\Form;
|
2024-03-07 00:58:14 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2024-03-15 00:28:57 +01:00
|
|
|
use stdClass;
|
2024-03-07 00:58:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @extends Collection<int, Field>
|
|
|
|
*/
|
|
|
|
class FieldCollection extends Collection
|
|
|
|
{
|
|
|
|
|
|
|
|
public function forMembers(): self
|
|
|
|
{
|
|
|
|
return $this->filter(fn ($field) => $field->forMembers === true);
|
|
|
|
}
|
|
|
|
|
2024-03-07 22:38:46 +01:00
|
|
|
public function withNamiType(): self
|
|
|
|
{
|
|
|
|
return $this->filter(fn ($field) => $field->namiType !== null);
|
|
|
|
}
|
|
|
|
|
2024-03-07 00:58:14 +01:00
|
|
|
public function noNamiType(): self
|
|
|
|
{
|
|
|
|
return $this->filter(fn ($field) => $field->namiType === null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function noNamiField(): self
|
|
|
|
{
|
|
|
|
return $this->filter(fn ($field) => !is_a($field, NamiField::class));
|
|
|
|
}
|
2024-03-14 23:54:41 +01:00
|
|
|
|
2024-06-20 23:29:23 +02:00
|
|
|
public function hasNamiField(): bool
|
|
|
|
{
|
|
|
|
return $this->first(fn ($field) => is_a($field, NamiField::class)) !== null;
|
|
|
|
}
|
|
|
|
|
2024-03-15 00:28:57 +01:00
|
|
|
/**
|
|
|
|
* @return stdClass
|
|
|
|
*/
|
|
|
|
public function getMailRecipient(): ?stdClass
|
|
|
|
{
|
|
|
|
$email = $this->findBySpecialType(SpecialType::EMAIL)?->value;
|
|
|
|
|
2024-03-15 01:52:27 +01:00
|
|
|
return $this->getFullname() && $email
|
2024-03-15 00:28:57 +01:00
|
|
|
? (object) [
|
2024-03-15 01:52:27 +01:00
|
|
|
'name' => $this->getFullname(),
|
2024-03-15 00:28:57 +01:00
|
|
|
"email" => $email,
|
|
|
|
] : null;
|
|
|
|
}
|
|
|
|
|
2024-03-15 01:52:27 +01:00
|
|
|
public function getFullname(): ?string
|
|
|
|
{
|
|
|
|
$firstname = $this->findBySpecialType(SpecialType::FIRSTNAME)?->value;
|
|
|
|
$lastname = $this->findBySpecialType(SpecialType::LASTNAME)?->value;
|
|
|
|
|
|
|
|
return $firstname && $lastname ? "$firstname $lastname" : null;
|
|
|
|
}
|
|
|
|
|
2024-03-14 23:54:41 +01:00
|
|
|
/**
|
|
|
|
* @param array<string, mixed> $input
|
|
|
|
*/
|
|
|
|
public static function fromRequest(Form $form, array $input): self
|
|
|
|
{
|
2024-04-12 15:42:43 +02:00
|
|
|
return $form->getFields()->map(function ($field) use ($input) {
|
|
|
|
$field->value = array_key_exists($field->key, $input) ? $input[$field->key] : $field->default();
|
|
|
|
return $field;
|
|
|
|
});
|
2024-03-14 23:54:41 +01:00
|
|
|
}
|
|
|
|
|
2024-03-15 01:52:27 +01:00
|
|
|
public function find(Field $givenField): ?Field
|
|
|
|
{
|
2024-04-20 00:00:04 +02:00
|
|
|
return $this->findByKey($givenField->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findByKey(string $key): ?Field
|
|
|
|
{
|
|
|
|
return $this->first(fn ($field) => $field->key === $key);
|
2024-03-15 01:52:27 +01:00
|
|
|
}
|
|
|
|
|
2024-03-14 23:54:41 +01:00
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
|
|
|
public function present(): array
|
|
|
|
{
|
|
|
|
$attributes = collect([]);
|
|
|
|
|
|
|
|
foreach ($this as $field) {
|
|
|
|
$attributes = $attributes->merge($field->present());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $attributes->toArray();
|
|
|
|
}
|
2024-03-15 00:28:57 +01:00
|
|
|
|
2024-05-27 21:08:09 +02:00
|
|
|
/**
|
|
|
|
* @return array<int, string>
|
|
|
|
*/
|
|
|
|
public function names(): array
|
|
|
|
{
|
|
|
|
return $this->map(fn ($field) => $field->name)->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<int, string>
|
|
|
|
*/
|
|
|
|
public function presentValues(): array
|
|
|
|
{
|
|
|
|
return $this->map(fn ($field) => $field->presentRaw())->toArray();
|
|
|
|
}
|
|
|
|
|
2024-03-15 00:28:57 +01:00
|
|
|
private function findBySpecialType(SpecialType $specialType): ?Field
|
|
|
|
{
|
|
|
|
return $this->first(fn ($field) => $field->specialType === $specialType);
|
|
|
|
}
|
2024-12-11 22:32:23 +01:00
|
|
|
|
2024-12-11 22:36:25 +01:00
|
|
|
public function searchables(): self
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function filterables(): self
|
|
|
|
{
|
2024-12-12 00:30:59 +01:00
|
|
|
return $this->filter(fn ($field) => $field instanceof Filterable);
|
2024-12-11 22:36:25 +01:00
|
|
|
}
|
|
|
|
|
2024-12-11 22:32:23 +01:00
|
|
|
/**
|
|
|
|
* @return array<int, string>
|
|
|
|
*/
|
|
|
|
public function getKeys(): array
|
|
|
|
{
|
|
|
|
return $this->map(fn ($field) => $field->key)->toArray();
|
|
|
|
}
|
2024-03-07 00:58:14 +01:00
|
|
|
}
|