File: D:/HostingSpaces/SBogers10/ste.komma.pro/vendor/komma/feedback-company/src/Base/ResourceFactory.php
<?php
namespace Komma\FeedbackCompany\Base;
use Komma\FeedbackCompany\FeedbackCompanyApi;
use Komma\FeedbackCompany\Resources\RenderableInterface;
use Carbon\Carbon;
final class ResourceFactory
{
/**
* Create an array of resource objects from API results
*
* @param array $apiResults
* @param string $resourceClass
* @return array
*/
public static function createFromApiResultArray(array $apiResults, string $resourceClass): array
{
$collection = [];
foreach ($apiResults as $result) {
$collection[] = ResourceFactory::createFromApiResult($result, new $resourceClass);
}
return $collection;
}
/**
* Create resource object from API result
*
* @param object $apiResult
* @param ApiResource $resource
*
* @return ApiResource
*/
public static function createFromApiResult($apiResult, ApiResource $resource)
{
foreach ($apiResult as $property => $value) {
// Check if the attribute can be ignored
if($resource->isIgnoredAttribute($property)) continue;
// Loop up if the attribute is remapped.
if($resource->isRemappedAttribute($property)) $property = $resource->getRemappedAttributeKey($property);
// Check if property exists on the resource
if (!property_exists($resource, $property)) {
// Possible log the undefined property
if(FeedbackCompanyApi::$debug) {
$debugMessage = self::class.': Undefined property "' . $property . '" on Resource "' . get_class($resource) . '"';
// If laravel is installed and the debug function exists, use it else log by echoing the message
if(function_exists('debug')) debug($debugMessage);
else echo $debugMessage. ' <br/>';
}
continue;
}
// Check if property is a date attribute, then convert it into Carbon
if($resource->isDateAttribute($property)) $value = Carbon::parse($value);
$resource->{$property} = $value;
}
if(is_a($resource, RenderableInterface::class)) $resource->render();
return $resource;
}
}