File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Kms/Core/AbstractModelHandler.php
<?php
declare(strict_types=1);
namespace App\Komma\Kms\Core;
use App\Komma\Kms\Core\Attributes\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
abstract class AbstractModelHandler implements AbstractModelHandlerInterface
{
/**
* @param Collection $attributes
* @param bool $mayTrowException
* @return bool
*/
protected function checkContainsAttributes(Collection $attributes, bool $mayTrowException = true)
{
$containsAttributes = true;
$attributes->each(function ($attribute) use (&$containsAttributes, $mayTrowException) {
if (! is_a($attribute, Attribute::class)) {
if ($mayTrowException) {
throw new \InvalidArgumentException('The collection did not exclusively contain attributes.');
}
$containsAttributes = false;
return false;
}
});
return $containsAttributes;
}
/**
* Puts the values of attributes in an Eloquent model. And then saves that model.
*
* @param Model $model
* @param Collection $attributes
* @return Model
*/
abstract public function save(Model $model, Collection $attributes = null): Model;
/**
* Gets the values of an Eloquent model and passes them to a collection of attributes
*
* @param Model $model
* @param Collection $attributes
* @return Collection
*/
abstract public function load(Model $model, Collection $attributes = null): Collection;
/**
* Destroys the appropriate related models for a given model.
* Those related models must be the responsibility of this service
*
* @param Model $model
*/
abstract public function destroyForModel(Model $model);
}