koel/app/Values/SmartPlaylistRuleGroup.php

38 lines
921 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;
final class SmartPlaylistRuleGroup implements Arrayable
{
2022-07-27 08:49:33 +00:00
private function __construct(public ?int $id, public Collection $rules)
{
}
public static function tryCreate(array $jsonArray): ?self
{
2022-08-08 16:00:59 +00:00
return attempt(static fn () => self::create($jsonArray));
}
public static function create(array $jsonArray): self
{
2022-07-27 08:49:33 +00:00
$rules = collect(array_map(static function (array $rawRuleConfig) {
return SmartPlaylistRule::create($rawRuleConfig);
}, $jsonArray['rules']));
2022-07-29 06:47:10 +00:00
return new self(Arr::get($jsonArray, 'id'), $rules);
}
/** @return array<mixed> */
public function toArray(): array
{
return [
'id' => $this->id,
'rules' => $this->rules->toArray(),
];
}
}