1
0
cms11/app/Classes/MetadataManager.php

69 lines
1.4 KiB
PHP

<?php
namespace App\Classes;
use Illuminate\Filesystem\FilesystemAdapter;
class MetadataManager
{
/**
* Original metadata content
*/
private array $originalContent = [];
/**
* Current metadata content
*/
private array $content = [];
public function __construct(protected string $filename, protected FilesystemAdapter $disk)
{
}
/**
* Return a boolean value indicating if the file actually exists on disk
*/
public function exists()
{
return $this->disk->exists($this->filename);
}
/**
* Set a new array of values
*/
public function set(array $content): void
{
$this->content = $content;
}
/**
* Return a boolean indicating if the file has changed since it was loaded
*/
public function isDirty(): bool
{
$original = collect($this->originalContent);
$current = collect($this->content);
return $original->diffAssoc($current)->isEmpty() && $current->diffAssoc($original)->isEmpty();
}
/**
* Store file on disk
*/
public function save(): bool
{
if (!$this->isDirty()) {
return false;
}
$json = json_encode($this->content, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$this->disk->put($this->filename, $json);
$this->originalContent = $this->content;
return true;
}
}