diff options
Diffstat (limited to 'src/SvgCollectionState.php')
| -rw-r--r-- | src/SvgCollectionState.php | 132 | 
1 files changed, 132 insertions, 0 deletions
| diff --git a/src/SvgCollectionState.php b/src/SvgCollectionState.php new file mode 100644 index 0000000..1bf8013 --- /dev/null +++ b/src/SvgCollectionState.php @@ -0,0 +1,132 @@ +<?php + +namespace Lightscale\LaralightSvg; + +use Illuminate\Support\Facades\Cache; +use Illuminate\Cache\Repository; + +class SvgCollectionState +{ +    protected array $existing; +    protected array $pending; +    protected string $svg; + +    public function __construct( +        protected readonly string $name, +    ) {} + +    protected function arrayToMap(array $arr): array +    { +        $result = []; +        foreach($arr as $val) { +            $result[$val] = $val; +        } +        return $result; +    } + +    public function pull(): void +    { +        $data = $this->cache()->get($this->cacheKey()); +        $this->svg = $data['svg'] ?? ''; + +        $existing = $data['existing'] ?? []; +        $pending = $data['pending'] ?? []; + +        $this->existing = static::arrayToMap($existing); +        $this->pending = static::arrayToMap($pending); +    } + +    public function push(): void +    { +        $data = [ +            'existing' => array_values($this->existing), +            'pending' => array_values($this->pending), +            'svg' => $this->svg, +        ]; +        $this->cache()->put($this->cacheKey(), $data); +    } + +    public function getPending(): array +    { +        if (!isset($this->pending)) $this->pull(); +        return $this->pending; +    } + +    public function getExisting(): array +    { +        if (!isset($this->existing)) $this->pull(); +        return $this->existing; +    } + +    public function getFullSvg(): string +    { +        if (!isset($this->svg)) $this->pull(); +        return $this->svg; +    } + +    public function setFullSvg(string $svg, bool $push = true): void +    { +        $this->svg = $svg; +        if ($push) $this->push(); +    } + +    public function isExisting(string $svg): bool +    { +        return !empty($this->getExisting()[$svg] ?? null); +    } + +    public function concatExisting(array $svgs, bool $push = true): void +    { +        $this->existing = [ +           ...$this->existing, +           ...$svgs, +        ]; +        if ($push) $this->push(); +    } + +    public function clearExisting(bool $push = true): void +    { +        $this->existing = []; +        if($push) $this->push(); +    } + +    public function isPending(string $svg): bool +    { +        return !empty($this->getPending()[$svg] ?? null); +    } + +    public function hasPending(): bool +    { +        return !empty($this->getPending()); +    } + +    public function addPending(string $svg): void +    { +        $this->pending[$svg] = $svg; +        $this->push(); +    } + +    public function clearPending(bool $push = true): void +    { +        $this->pending = []; +        if($push) $this->push(); +    } + +    public function addSvg(string $svg): void +    { +        if (!$this->isExisting($svg) && !$this->isPending($svg)) { +            $this->addPending($svg); +        } +    } + +    protected function cache(): Repository +    { +        return Cache::store(config('svg.cache_store')); +    } + +    protected function cacheKey(): string +    { +        return SvgServiceProvider::NAMESPACE . '::' . $this->name; +    } + +} | 
