<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Behavior\Impl;

use CpCreation\VitiCore\Media\Model\Image as ModelImage;
use Doctrine\ORM\Mapping as ORM;

trait Image
{
    #[ORM\OneToOne(
        targetEntity: ModelImage::class,
        cascade: ['persist', 'remove']
    )]
    #[ORM\JoinColumn(
        name: 'image_id',
        referencedColumnName: 'id',
        nullable: true,
        onDelete: 'SET NULL'
    )]
    private ?ModelImage $image = null;

    public function getImage(): ?ModelImage
    {
        return $this->image;
    }

    public function setImage(?ModelImage $image): void
    {
        if (null !== $image && ($image->getPath() || $image->getFile())) {
            $this->image = $image;
        } else {
            $this->image = null;
        }
    }
}
