44 lines
900 B
PHP
44 lines
900 B
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Lucent\Field;
|
||
|
|
|
||
|
|
use Illuminate\Support\Collection;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @extends \Illuminate\Support\Collection<int|string, Field>
|
||
|
|
*/
|
||
|
|
final class FieldCollection extends Collection
|
||
|
|
{
|
||
|
|
|
||
|
|
public function __construct(
|
||
|
|
Field ...$array
|
||
|
|
) {
|
||
|
|
parent::__construct($array);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return Schema[]
|
||
|
|
**/
|
||
|
|
public function toArray(): array
|
||
|
|
{
|
||
|
|
return collect($this)->values()->toArray();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function findByName(string $name): ?Field
|
||
|
|
{
|
||
|
|
return $this->firstWhere("name", $name);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function fromArray(array $data): FieldCollection
|
||
|
|
{
|
||
|
|
$items = array_map([Field::class, 'fromArray'], $data);
|
||
|
|
return new FieldCollection(...$items);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function fromDB(string $data): FieldCollection
|
||
|
|
{
|
||
|
|
return FieldCollection::fromArray(\json_decode($data,true));
|
||
|
|
}
|
||
|
|
}
|