111 lines
2.2 KiB
PHP
111 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\Contact;
|
|
use App\Traits\RepositoryRelationshipsTraits;
|
|
|
|
class ContactRepository implements ContactRepositoryInterface
|
|
{
|
|
use RepositoryRelationshipsTraits;
|
|
|
|
private string $Model;
|
|
|
|
private array $defaultRelations = [];
|
|
|
|
public function __construct()
|
|
{
|
|
$this->Model = Contact::class;
|
|
$this->actualRelations = $this->defaultRelations;
|
|
}
|
|
|
|
/**
|
|
* Get's a record by it's ID
|
|
*
|
|
* @param int
|
|
*/
|
|
public function get($id)
|
|
{
|
|
return Contact::find($id);
|
|
}
|
|
|
|
/**
|
|
* Get's all records.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function all()
|
|
{
|
|
return Contact::all();
|
|
}
|
|
|
|
/**
|
|
* Deletes a record.
|
|
*
|
|
* @param int
|
|
*/
|
|
public function delete($id)
|
|
{
|
|
Contact::destroy($id);
|
|
}
|
|
|
|
/**
|
|
* Updates a post.
|
|
*
|
|
* @param int
|
|
* @param array
|
|
*/
|
|
public function update($id, array $data)
|
|
{
|
|
Contact::find($id)->update($data);
|
|
}
|
|
|
|
public function add($data): ?int
|
|
{
|
|
$obj = new $this->Model;
|
|
$obj->canSee = true;
|
|
$obj->status == \App\Enums\DbStatusFieldEnum::draft;
|
|
$obj->fill($data);
|
|
$obj->save();
|
|
|
|
return $obj->id;
|
|
}
|
|
|
|
public function setContactRelation($id, $relationId, $relationType): bool
|
|
{
|
|
if (! $item = $this->Model::find($id)) {
|
|
return false;
|
|
}
|
|
if (! $attachedItem = $relationType::find($relationId)) {
|
|
return false;
|
|
}
|
|
|
|
$ret = false;
|
|
if ($item->contactable()->associate($attachedItem)) {
|
|
if ($item->save()) {
|
|
$ret = true;
|
|
}
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public function unsetContactRelation($id, $relationId, $relationType): bool
|
|
{
|
|
if (! $item = $this->Model::find($id)) {
|
|
return false;
|
|
}
|
|
if (! $attachedItem = $relationType::find($relationId)) {
|
|
|
|
}
|
|
$ret = false;
|
|
if ($item->contactable()->dissociate($attachedItem)) {
|
|
if ($item->save()) {
|
|
$ret = true;
|
|
}
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
}
|