File: D:/HostingSpaces/SBogers10/bomacon.komma.pro/app/Components/ComponentType/ComponentTypeFactory.php
<?php
namespace App\Components\ComponentType;
use App\Components\ComponentType\Types\AbstractComponentType;
use App\Components\ComponentType\Types\AccordionFixedImage;
use App\Components\ComponentType\Types\Capterra;
use App\Components\ComponentType\Types\Cards;
use App\Components\ComponentType\Types\ContentPersonal;
use App\Components\ComponentType\Types\ContentSlider;
use App\Components\ComponentType\Types\DataSecurity;
use App\Components\ComponentType\Types\DoubleText;
use App\Components\ComponentType\Types\Downloads;
use App\Components\ComponentType\Types\Heading;
use App\Components\ComponentType\Types\Image;
use App\Components\ComponentType\Types\DoubleImage;
use App\Components\ComponentType\Types\ProductTable;
use App\Components\ComponentType\Types\Quote;
use App\Components\ComponentType\Types\Text;
use App\Components\ComponentType\Types\TextImage;
use App\Components\ComponentType\Types\USP;
use App\Components\ComponentType\Types\Video;
use Illuminate\Support\Collection;
/**
* Class ComponentTypeFactory
*
* Knows how to create component types by passing it an ComponentTypes enum.
*
* WARNING! The Factory only can create one instance of a componentType per request.
* If you make componentTypes more then once in a request, you will get the same componentType.
* This is because they must have unique attribute keys.
*
* @package App\Components\ComponentType
*/
class ComponentTypeFactory
{
private static $maked = [];
/**
* Make componentTypes
*
* @param int $componentTypeId
* @return AbstractComponentType|Collection
* @throws \ReflectionException
*/
static public function make(int $componentTypeId = null) {
if($componentTypeId === null) {
$componentTypes = new Collection();
foreach(ComponentTypes::getAsArray() as $componentTypeId) {
if($componentTypeId === 0) continue;
$componentTypes->push(self::makeSpecificType($componentTypeId));
}
$componentTypes = $componentTypes->filter(function($type) {
return !is_null($type);
});
$componentTypes = $componentTypes->keyBy(function(AbstractComponentType $componentType) {
return $componentType->getId();
});
return $componentTypes;
} else {
return self::makeSpecificType($componentTypeId);
}
}
/**
* @param $componentTypeId
* @return AbstractComponentType
*/
private static function makeSpecificType($componentTypeId) {
if(in_array($componentTypeId, array_keys(self::$maked), true)) return self::$maked[$componentTypeId];
switch ($componentTypeId) {
case ComponentTypes::TEXT_IMAGE:
self::$maked[$componentTypeId] = new TextImage();
break;
case ComponentTypes::DOUBLE_TEXT:
self::$maked[$componentTypeId] = new DoubleText();
break;
case ComponentTypes::VIDEO:
self::$maked[$componentTypeId] = new Video();
break;
case ComponentTypes::TEXT:
self::$maked[$componentTypeId] = new Text();
break;
case ComponentTypes::IMAGE:
self::$maked[$componentTypeId] = new Image();
break;
case ComponentTypes::DOUBLE_IMAGE:
self::$maked[$componentTypeId] = new DoubleImage();
break;
case ComponentTypes::USP:
self::$maked[$componentTypeId] = new USP();
break;
case ComponentTypes::CONTENT_PERSONAL:
self::$maked[$componentTypeId] = new ContentPersonal();
break;
case ComponentTypes::CONTENT_SLIDER:
self::$maked[$componentTypeId] = new ContentSlider();
break;
case ComponentTypes::QUOTE:
self::$maked[$componentTypeId] = new Quote();
break;
case ComponentTypes::DOWNLOADS:
self::$maked[$componentTypeId] = new Downloads();
break;
default:
throw new \RuntimeException('Type "'.$componentTypeId.'" not makable since it is unknown');
}
return self::$maked[$componentTypeId];
}
}