koel/app/Values/SmartPlaylistRuleGroup.php

38 lines
966 B
PHP
Raw Normal View History

<?php
namespace App\Values;
use Illuminate\Contracts\Support\Arrayable;
2022-07-29 06:47:10 +00:00
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Webmozart\Assert\Assert;
final class SmartPlaylistRuleGroup implements Arrayable
{
private function __construct(public string $id, public Collection $rules)
2022-07-27 08:49:33 +00:00
{
Assert::uuid($id);
2022-07-27 08:49:33 +00:00
}
public static function make(array $array): self
{
return new self(
id: Arr::get($array, 'id'),
rules: collect(Arr::get($array, 'rules', []))->transform(
static function (array|SmartPlaylistRule $rule): SmartPlaylistRule {
return $rule instanceof SmartPlaylistRule ? $rule : SmartPlaylistRule::make($rule);
}
),
);
}
/** @return array<mixed> */
public function toArray(): array
{
return [
'id' => $this->id,
'rules' => $this->rules->toArray(),
];
}
}