File: D:/HostingSpaces/SBogers10/stielman.komma.nl/vendor/komma/kms/src/Core/AbstractModelHandler.php
<?php declare(strict_types=1);
namespace Komma\KMS\Core;
use Illuminate\Support\Facades\Log;
use 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);
/**
* Log a message to the regular logging system (usually laravel.log).
* You will be able to see it too in the Laravel debug bar if you have it.
*
* @param string|null $message
* @param $payload
*/
protected function debug(string $message = null, ...$payload) {
if(!config('app.debug_services')) return;
//Log to the regular logging system
Log::debug(class_basename(static::class) . ': ' . $message);
foreach($payload as $payloadItem) Log::debug($payloadItem);
}
}