2024-07-12 00:16:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Lib\Editor;
|
|
|
|
|
|
|
|
use Spatie\LaravelData\Data;
|
|
|
|
use Spatie\LaravelData\DataCollection;
|
|
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
|
|
|
use Spatie\LaravelData\Attributes\DataCollectionOf;
|
|
|
|
|
|
|
|
class Condition extends Data
|
|
|
|
{
|
|
|
|
|
|
|
|
/** @param DataCollection<int, Statement> $ifs */
|
|
|
|
public function __construct(
|
|
|
|
public ConditionMode $mode,
|
|
|
|
#[DataCollectionOf(Statement::class)]
|
|
|
|
public DataCollection $ifs,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromMedia(Media $media): self
|
|
|
|
{
|
2024-07-12 18:05:11 +02:00
|
|
|
return $media->getCustomProperty('conditions') ? static::withoutMagicalCreationFrom($media->getCustomProperty('conditions')) : static::default();
|
2024-07-12 00:16:50 +02:00
|
|
|
}
|
|
|
|
|
2024-07-12 18:05:11 +02:00
|
|
|
public static function defaults(): self
|
2024-07-12 00:16:50 +02:00
|
|
|
{
|
2024-07-12 18:05:11 +02:00
|
|
|
return static::withoutMagicalCreationFrom(['mode' => 'any', 'ifs' => []]);
|
2024-07-12 00:16:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hasStatements(): bool
|
|
|
|
{
|
|
|
|
return count($this->ifs) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isAny(): bool
|
|
|
|
{
|
|
|
|
return $this->mode === ConditionMode::ANY;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isAll(): bool
|
|
|
|
{
|
|
|
|
return $this->mode === ConditionMode::ALL;
|
|
|
|
}
|
|
|
|
}
|