-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
GenericTargetFactory
and related configuration (#33)
Co-authored-by: Jacob Dreesen <j.dreesen@neusta.de> Co-authored-by: Michael Albrecht <m.albrecht@neusta.de>
- Loading branch information
Showing
6 changed files
with
181 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neusta\ConverterBundle\Target; | ||
|
||
use Neusta\ConverterBundle\TargetFactory; | ||
|
||
/** | ||
* @template T of object | ||
* | ||
* @implements TargetFactory<T, object|null> | ||
*/ | ||
final class GenericTargetFactory implements TargetFactory | ||
{ | ||
/** @var \ReflectionClass<T> */ | ||
private \ReflectionClass $type; | ||
|
||
/** | ||
* @param class-string<T> $type | ||
* | ||
* @throws \ReflectionException | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __construct(string $type) | ||
{ | ||
$this->type = new \ReflectionClass($type); | ||
|
||
if (!$this->type->isInstantiable()) { | ||
throw new \InvalidArgumentException(sprintf('Target class "%s" is not instantiable.', $type)); | ||
} | ||
|
||
if ($this->type->getConstructor()?->getNumberOfRequiredParameters()) { | ||
throw new \InvalidArgumentException(sprintf('Target class "%s" has required constructor parameters.', $type)); | ||
} | ||
} | ||
|
||
/** | ||
* @throws \LogicException | ||
*/ | ||
public function create(?object $ctx = null): object | ||
{ | ||
try { | ||
return $this->type->newInstance(); | ||
} catch (\ReflectionException $e) { | ||
throw new \LogicException(sprintf('Cannot create new instance of "%s" because: %s', $this->type->getName(), $e->getMessage()), 0, $e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters