-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
541 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Exception; | ||
|
||
class ClassNotInstantiableException extends NotMappableValueException | ||
{ | ||
/** | ||
* @param class-string $class | ||
*/ | ||
public function __construct(string $class) | ||
{ | ||
parent::__construct(sprintf('Trying to instantiate class "%s", but this class is not instantiable.', $class)); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Exception; | ||
|
||
class IncompleteConstructorArgument extends NotMappableValueException | ||
{ | ||
/** | ||
* @param class-string $targetClass | ||
*/ | ||
public function __construct( | ||
object $source, | ||
string $targetClass, | ||
string $property, | ||
\Throwable $previous = null | ||
) { | ||
parent::__construct(sprintf('Trying to instantiate target class "%s", but its constructor requires the property "%s", which is missing from the source "%s".', $targetClass, $property, \get_debug_type($source)), 0, $previous); | ||
} | ||
} |
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Exception; | ||
|
||
class InstantiationFailureException extends NotMappableValueException | ||
{ | ||
/** | ||
* @param array<string,mixed> $constructorArguments | ||
*/ | ||
public function __construct( | ||
object $source, | ||
string $targetClass, | ||
array $constructorArguments, | ||
\Throwable $previous | ||
) { | ||
if (count($constructorArguments) === 0) { | ||
parent::__construct(sprintf( | ||
'Trying to map the source object of type "%s", but failed to instantiate the target object "%s" with no constructor argument.', | ||
\get_debug_type($source), | ||
$targetClass, | ||
), 0, $previous); | ||
} else { | ||
parent::__construct(sprintf( | ||
'Trying to map the source object of type "%s", but failed to instantiate the target object "%s" using constructor arguments: %s.', | ||
\get_debug_type($source), | ||
$targetClass, | ||
self::formatConstructorArguments($constructorArguments) | ||
), 0, $previous); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* @param array<string,mixed> $constructorArguments | ||
*/ | ||
private static function formatConstructorArguments(array $constructorArguments): string | ||
{ | ||
$formattedArguments = []; | ||
/** @var mixed $argumentValue */ | ||
foreach ($constructorArguments as $argumentName => $argumentValue) { | ||
$formattedArguments[] = sprintf('%s: %s', $argumentName, \get_debug_type($argumentValue)); | ||
} | ||
|
||
return implode(', ', $formattedArguments); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Exception; | ||
|
||
use Rekalogika\Mapper\Util\TypeUtil; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
class InvalidClassException extends UnexpectedValueException | ||
{ | ||
public function __construct(Type $type) | ||
{ | ||
parent::__construct(sprintf('Trying to map to class "%s", but this is not a valid class, interface, or enum.', TypeUtil::getDebugType($type))); | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
tests/Fixtures/Constructor/ObjectWithConstructorAndMoreArgumentDto.php
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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\Fixtures\Constructor; | ||
|
||
class ObjectWithConstructorAndMoreArgumentDto | ||
{ | ||
public function __construct( | ||
private int $a, | ||
private string $b, | ||
private bool $c, | ||
private float $d, | ||
// uses object to prevent casting | ||
private \Stringable $e, | ||
) { | ||
} | ||
|
||
public function getA(): int | ||
{ | ||
return $this->a; | ||
} | ||
|
||
public function getB(): string | ||
{ | ||
return $this->b; | ||
} | ||
|
||
public function isC(): bool | ||
{ | ||
return $this->c; | ||
} | ||
|
||
public function getD(): float | ||
{ | ||
return $this->d; | ||
} | ||
|
||
// work around mapping null to object, now property info sees it as a string | ||
public function getE(): string | ||
{ | ||
return (string) $this->e; | ||
} | ||
} |
Oops, something went wrong.