DenyTrashed.php 738 Bytes
Newer Older
jiangbowen's avatar
jiangbowen committed
1 2 3 4 5
<?php

namespace Adldap\Laravel\Validation\Rules;

use Adldap\Laravel\Events\AuthenticatedModelTrashed;
jiangbowen's avatar
jiangbowen committed
6
use Illuminate\Support\Facades\Event;
jiangbowen's avatar
jiangbowen committed
7 8 9 10 11 12 13 14 15

class DenyTrashed extends Rule
{
    /**
     * {@inheritdoc}
     */
    public function isValid()
    {
        if ($this->isTrashed()) {
jiangbowen's avatar
jiangbowen committed
16
            Event::dispatch(
jiangbowen's avatar
jiangbowen committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
                new AuthenticatedModelTrashed($this->user, $this->model)
            );

            return false;
        }

        return true;
    }

    /**
     * Determines if the current model is trashed.
     *
     * @return bool
     */
    protected function isTrashed()
    {
jiangbowen's avatar
jiangbowen committed
33 34 35
        return $this->model
            ? method_exists($this->model, 'trashed') && $this->model->trashed()
            : false;
jiangbowen's avatar
jiangbowen committed
36 37
    }
}