80 lines
1.6 KiB
PHP
80 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace LaraBB\Thread\Tasks;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use LaraBB\Thread\Models\Thread;
|
|
|
|
/**
|
|
* Class GetThreadTask
|
|
* @package LaraBB\Thread\Tasks
|
|
*/
|
|
class FindTask
|
|
{
|
|
/**
|
|
* @var Builder|null $query
|
|
*/
|
|
private ?Builder $query;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private string $return = 'Collection';
|
|
|
|
/**
|
|
* GetThreadTask constructor.
|
|
* @param Thread $thread
|
|
*/
|
|
public function __construct(private readonly Thread $thread)
|
|
{
|
|
|
|
}
|
|
|
|
/**
|
|
* @param array $with
|
|
* @return Collection|Thread|null
|
|
*/
|
|
public function run(array $with = []): Collection|Thread|null
|
|
{
|
|
if (is_null($this->query)) {
|
|
$this->query = $this->thread->query();
|
|
}
|
|
|
|
/** @var Collection $data */
|
|
$data = $this->query->with($with)->get();
|
|
|
|
if(is_null($data)) {
|
|
return null;
|
|
}
|
|
|
|
return $this->return == 'Model' ? $data->first() : $data;
|
|
}
|
|
|
|
/**
|
|
* @param $uuid
|
|
* @param array $with
|
|
* @return Collection|Thread|null
|
|
*/
|
|
public function byUuid($uuid, array $with = []): Collection|Thread|null
|
|
{
|
|
$this->return = 'Model';
|
|
$this->query = $this->thread->query()->where('uuid', $uuid);
|
|
|
|
return $this->run($with);
|
|
}
|
|
|
|
/**
|
|
* @param $slug
|
|
* @param array $with
|
|
* @return Collection|Thread|null
|
|
*/
|
|
public function bySlug($slug, array $with = []): Collection|Thread|null
|
|
{
|
|
$this->return = 'Model';
|
|
$this->query = $this->thread->query()->where('slug', $slug);
|
|
|
|
return $this->run($with);
|
|
}
|
|
}
|