<?php

namespace App\Classes\General\Eloquent\Filters;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use Illuminate\Database\Eloquent\Builder;

class IsMappedWithMultiple implements Filter
{

    /**
     * @param Builder $builder
     * @param $value
     * @return Builder|mixed
     */
    public static function apply(Builder $builder, $value)
    {
        $query = $builder->withCount(['owners' => function ($query){
            $query->where('status', ApprovalStatus::PENDING_VERIFICATION);
        }]);
        return $value ? $query->having('owners_count', '>', 1) : $query->having('owners_count', '=', 1);
    }

}
