BannerFilter.php 1.72 KB
<?php

namespace App\ModelFilters;

use App\Support\ModelFilter;
use Arr;

class BannerFilter extends ModelFilter
{
    /**
     * @param string $name
     * @return void
     */
    public function name(string $name): void
    {
        $this->whereLike('name', $name);
    }

    /**
     * @param string|array $role
     * @return void
     */
    public function role(string|array $role): void
    {
        $this->whereJsonContains('role', Arr::wrap($role));
    }

    /**
     * @param array|int|string $scope
     * @return void
     */
    public function scope(array|int|string $scope): void
    {
        $this->whereIn('scope', Arr::wrap($scope));
    }

    /**
     * @param string|array $permission
     * @return void
     */
    public function permission(string|array $permission): void
    {
        $permission = Arr::wrap($permission);
        $count      = count($permission);

        if ($count <= 1) {
            $this->whereJsonContains('permission', $permission);
        } else {
            $this->where(fn($query) => Arr::map($permission, static fn($item) => $query->orWhereJsonContains('permission', $item)));
//            $this->whereJsonContains('permission')
//            $this->whereRaw('JSON_EXTRACT(permission, \'$[*]\') IN (' . implode(',', array_fill(0, $count, '?')) . ')', $permission);
//            $this->whereIn('permission', Arr::wrap($permission));
//            $this->whereRaw('JSON_CONTAINS(JSON_ARRAY(' . implode(',', array_fill(0, $count, '?')) . '),JSON_PRETTY(permission))', $permission);
        }
    }

    /**
     * @param array|int|string $status
     * @return void
     */
    public function status(array|int|string $status): void
    {
        $this->whereIn('status', Arr::wrap($status));
    }
}