diff --git a/core/Entity/Site/Page/FileBlock.php b/core/Entity/Site/Page/FileBlock.php index 7f4d772..4d1ed0c 100644 --- a/core/Entity/Site/Page/FileBlock.php +++ b/core/Entity/Site/Page/FileBlock.php @@ -2,6 +2,7 @@ namespace App\Core\Entity\Site\Page; +use App\Core\File\FileAttribute; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\HttpFoundation\File\File; @@ -12,17 +13,7 @@ class FileBlock extends Block { public function getValue() { - $value = parent::getValue(); - - if (is_string($value)) { - if (file_exists($value)) { - return new File($value); - } - - return null; - } - - return $value; + return FileAttribute::handleFile(parent::getValue()); } public function setValue($value): self diff --git a/core/Entity/Site/Page/Page.php b/core/Entity/Site/Page/Page.php index fd6608a..f48602d 100644 --- a/core/Entity/Site/Page/Page.php +++ b/core/Entity/Site/Page/Page.php @@ -11,6 +11,7 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\HttpFoundation\File\File; +use App\Core\File\FileAttribute; /** * @ORM\Entity(repositoryClass=PageRepository::class) @@ -229,15 +230,7 @@ class Page implements EntityInterface public function getOgImage() { - if (is_string($this->ogImage)) { - if (file_exists($this->ogImage)) { - return new File($this->ogImage); - } - - return null; - } - - return $this->ogImage; + return FileAttribute::handleFile($this->ogImage); } public function setOgImage($ogImage): self diff --git a/core/File/FileAttribute.php b/core/File/FileAttribute.php new file mode 100644 index 0000000..b643187 --- /dev/null +++ b/core/File/FileAttribute.php @@ -0,0 +1,30 @@ + + */ +class FileAttribute +{ + public static function handleFile($attribute, string $class = null) + { + if (null === $class) { + $class = File::class; + } + + if (is_string($attribute)) { + if (file_exists($attribute)) { + return new $class($attribute); + } + + return null; + } + + return $attribute; + } +}