/home/brandsfa/public_html/app/Repositories/VendorRepository.php
<?php
namespace App\Repositories;
use App\Contracts\Repositories\VendorRepositoryInterface;
use App\Models\DeliveryZipCode;
use App\Models\Seller;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
class VendorRepository implements VendorRepositoryInterface
{
public function __construct(
private readonly Seller $vendor,
)
{
}
public function getByStatusExcept(string $status, array $relations = [], int $paginateBy = DEFAULT_DATA_LIMIT): Collection|array|LengthAwarePaginator
{
return $this->vendor->with($relations)->whereNotIn('status', [$status])->paginate($paginateBy);
}
public function add(array $data): string|object
{
return $this->vendor->create($data);
}
public function getFirstWhere(array $params, array $relations = []): ?Model
{
return $this->vendor->with($relations)
->when(isset($params['identity']),function ($query) use ($params){
return $query->where(['email' => $params['identity']])
->orWhere(['phone' => $params['identity']]);
})
->when(isset($params['id']),function ($query) use ($params){
return $query->where(['id' => $params['id']]);
})
->when(isset($params['withCount']),function ($query)use($params){
return $query->withCount($params['withCount']);
})
->first();
}
public function getList(array $orderBy = [], array $relations = [], int|string $dataLimit = DEFAULT_DATA_LIMIT, int $offset = null): Collection|LengthAwarePaginator
{
$query = $this->vendor->with($relations)->when(!empty($orderBy), function ($query) use ($orderBy) {
$query->orderBy(array_key_first($orderBy),array_values($orderBy)[0]);
});
return $dataLimit == 'all' ? $query->get() : $query->paginate($dataLimit);
}
public function getListWhere(array $orderBy=[], string $searchValue = null, array $filters = [], array $relations = [], int|string $dataLimit = DEFAULT_DATA_LIMIT, int $offset = null): Collection|LengthAwarePaginator
{
$query = $this->vendor->where($filters)->with($relations)
->when($searchValue, function ($query) use ($searchValue) {
$searchTerms = explode(' ', $searchValue);
$query->where(function ($query) use ($searchTerms) {
foreach ($searchTerms as $term) {
$query->orWhere('f_name', 'like', "%$term%")
->orWhere('l_name', 'like', "%$term%")
->orWhere('phone', 'like', "%$term%")
->orWhere('email', 'like', "%$term%")
->orWhereHas('shop', function ($query) use ($term) {
$query->where('name', 'like', "%$term%");
});
}
});
})
->when(!empty($relations) && in_array('product', $relations), function ($query) {
$query->withCount('product');
})
->when(!empty($relations) && in_array('orders', $relations), function ($query) {
$query->withCount('orders');
})
->when(!empty($orderBy), function ($query) use ($orderBy) {
$query->orderBy(array_key_first($orderBy),array_values($orderBy)[0]);
});
$filters += ['searchValue' =>$searchValue];
return $dataLimit == 'all' ? $query->get() : $query->paginate($dataLimit)->appends($filters);
}
public function update(string $id, array $data): bool
{
return $this->vendor->where(['id'=>$id])->update($data);
}
public function delete(array $params): bool
{
$this->vendor->where($params)->delete();
return true;
}
}