-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
75c54d9
commit db4168c
Showing
31 changed files
with
162 additions
and
122 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
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 |
---|---|---|
@@ -1,20 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd" | ||
colors="true" | ||
columns="max" | ||
stderr="true" | ||
bootstrap="./.build/vendor/autoload.php" | ||
cacheDirectory=".phpunit.cache" | ||
> | ||
<testsuites> | ||
<testsuite name="Integration Tests"> | ||
<directory>./test</directory> | ||
</testsuite> | ||
</testsuites> | ||
<source> | ||
<coverage> | ||
<include> | ||
<directory>./src</directory> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</source> | ||
</coverage> | ||
</phpunit> |
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 |
---|---|---|
|
@@ -11,11 +11,14 @@ | |
|
||
namespace MagicSunday; | ||
|
||
use function array_key_exists; | ||
use Closure; | ||
use Doctrine\Common\Annotations\Annotation; | ||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use DomainException; | ||
use function in_array; | ||
use InvalidArgumentException; | ||
use function is_array; | ||
use MagicSunday\JsonMapper\Annotation\ReplaceNullWithDefaultValue; | ||
use MagicSunday\JsonMapper\Annotation\ReplaceProperty; | ||
use MagicSunday\JsonMapper\Converter\PropertyNameConverterInterface; | ||
|
@@ -27,15 +30,12 @@ | |
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
use function array_key_exists; | ||
use function in_array; | ||
use function is_array; | ||
|
||
/** | ||
* JsonMapper. | ||
* | ||
* @author Rico Sonntag <[email protected]> | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
* @link https://github.com/magicsunday/jsonmapper/ | ||
* | ||
* @template TEntity | ||
|
@@ -46,41 +46,41 @@ class JsonMapper | |
/** | ||
* @var PropertyInfoExtractorInterface | ||
*/ | ||
private PropertyInfoExtractorInterface $extractor; | ||
private $extractor; | ||
|
||
/** | ||
* @var PropertyAccessorInterface | ||
*/ | ||
private PropertyAccessorInterface $accessor; | ||
private $accessor; | ||
|
||
/** | ||
* The property name converter instance. | ||
* | ||
* @var PropertyNameConverterInterface|null | ||
*/ | ||
protected ?PropertyNameConverterInterface $nameConverter; | ||
protected $nameConverter; | ||
|
||
/** | ||
* Override class names that JsonMapper uses to create objects. Useful when your | ||
* setter methods accept abstract classes or interfaces. | ||
* | ||
* @var string[]|Closure[] | ||
*/ | ||
private array $classMap; | ||
private $classMap; | ||
|
||
/** | ||
* The default value type instance. | ||
* | ||
* @var Type | ||
*/ | ||
private Type $defaultType; | ||
private $defaultType; | ||
|
||
/** | ||
* The custom types. | ||
* | ||
* @var Closure[] | ||
*/ | ||
private array $types = []; | ||
private $types = []; | ||
|
||
/** | ||
* JsonMapper constructor. | ||
|
@@ -308,7 +308,7 @@ private function getReflectionProperty(string $className, string $propertyName): | |
{ | ||
try { | ||
return new ReflectionProperty($className, $propertyName); | ||
} catch (ReflectionException) { | ||
} catch (ReflectionException $exception) { | ||
return null; | ||
} | ||
} | ||
|
@@ -419,15 +419,15 @@ private function hasClassAnnotation(string $className, string $annotationName): | |
* | ||
* @return mixed|null | ||
*/ | ||
private function getDefaultValue(string $className, string $propertyName): mixed | ||
private function getDefaultValue(string $className, string $propertyName) | ||
{ | ||
$reflectionProperty = $this->getReflectionProperty($className, $propertyName); | ||
|
||
if (!($reflectionProperty instanceof ReflectionProperty)) { | ||
return null; | ||
} | ||
|
||
return $reflectionProperty->getDefaultValue(); | ||
return $reflectionProperty->getDeclaringClass()->getDefaultProperties()[$propertyName] ?? null; | ||
} | ||
|
||
/** | ||
|
@@ -437,7 +437,7 @@ private function getDefaultValue(string $className, string $propertyName): mixed | |
* | ||
* @return bool | ||
*/ | ||
private function isNumericIndexArray(mixed $json): bool | ||
private function isNumericIndexArray($json): bool | ||
{ | ||
foreach ($json as $propertyName => $propertyValue) { | ||
if (is_int($propertyName)) { | ||
|
@@ -455,7 +455,7 @@ private function isNumericIndexArray(mixed $json): bool | |
* | ||
* @return bool | ||
*/ | ||
private function isIterableWithArraysOrObjects(mixed $json): bool | ||
private function isIterableWithArraysOrObjects($json): bool | ||
{ | ||
// Return false if JSON is not an array or object (is_iterable won't work here) | ||
if (!is_array($json) && !is_object($json)) { | ||
|
@@ -510,7 +510,7 @@ private function assertClassesExists(string $className, ?string $collectionClass | |
* @param string $name | ||
* @param mixed $value | ||
*/ | ||
private function setProperty(object $entity, string $name, mixed $value): void | ||
private function setProperty(object $entity, string $name, $value): void | ||
{ | ||
// Handle variadic setters | ||
if (is_array($value)) { | ||
|
@@ -570,7 +570,7 @@ private function getType(string $className, string $propertyName): Type | |
* | ||
* @throws DomainException | ||
*/ | ||
private function getValue(mixed $json, Type $type): mixed | ||
private function getValue($json, Type $type) | ||
{ | ||
if ((is_array($json) || is_object($json)) && $type->isCollection()) { | ||
$collectionType = $this->getCollectionValueType($type); | ||
|
@@ -652,7 +652,7 @@ private function getClassNameFromType(Type $type): string | |
* | ||
* @throws DomainException | ||
*/ | ||
private function getMappedClassName(string $className, mixed $json): string | ||
private function getMappedClassName(string $className, $json): string | ||
{ | ||
if (array_key_exists($className, $this->classMap)) { | ||
$classNameOrClosure = $this->classMap[$className]; | ||
|
@@ -680,7 +680,7 @@ private function getMappedClassName(string $className, mixed $json): string | |
* | ||
* @throws DomainException | ||
*/ | ||
private function getClassName(mixed $json, Type $type): string | ||
private function getClassName($json, Type $type): string | ||
{ | ||
return $this->getMappedClassName( | ||
$this->getClassNameFromType($type), | ||
|
@@ -698,7 +698,7 @@ private function getClassName(mixed $json, Type $type): string | |
* | ||
* @throws DomainException | ||
*/ | ||
private function asCollection(mixed $json, Type $type): ?array | ||
private function asCollection($json, Type $type): ?array | ||
{ | ||
if ($json === null) { | ||
return null; | ||
|
@@ -723,7 +723,7 @@ private function asCollection(mixed $json, Type $type): ?array | |
* | ||
* @throws DomainException | ||
*/ | ||
private function asObject(mixed $json, Type $type): mixed | ||
private function asObject($json, Type $type) | ||
{ | ||
/** @var class-string<TEntity> $className */ | ||
$className = $this->getClassName($json, $type); | ||
|
@@ -759,7 +759,7 @@ private function isCustomType(string $typeClassName): bool | |
* | ||
* @return mixed | ||
*/ | ||
private function callCustomClosure(mixed $json, string $typeClassName): mixed | ||
private function callCustomClosure($json, string $typeClassName) | ||
{ | ||
$callback = $this->types[$typeClassName]; | ||
|
||
|
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 |
---|---|---|
|
@@ -19,14 +19,15 @@ | |
* | ||
* @author Rico Sonntag <[email protected]> | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
* @link https://github.com/magicsunday/jsonmapper/ | ||
*/ | ||
class CamelCasePropertyNameConverter implements PropertyNameConverterInterface | ||
{ | ||
/** | ||
* @var Inflector | ||
*/ | ||
private Inflector $inflector; | ||
private $inflector; | ||
|
||
/** | ||
* CamelCasePropertyNameConverter constructor. | ||
|
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
* | ||
* @author Rico Sonntag <[email protected]> | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
* @link https://github.com/magicsunday/jsonmapper/ | ||
*/ | ||
interface PropertyNameConverterInterface | ||
|
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 |
---|---|---|
|
@@ -20,14 +20,16 @@ | |
* | ||
* @author Rico Sonntag <[email protected]> | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
* @link https://github.com/magicsunday/jsonmapper/ | ||
*/ | ||
class ReplacePropertyTest extends TestCase | ||
{ | ||
/** | ||
* Tests replacing a property. | ||
* | ||
* @test | ||
*/ | ||
#[Test] | ||
public function replaceProperty(): void | ||
{ | ||
$result = $this->getJsonMapper() | ||
|
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
* | ||
* @author Rico Sonntag <[email protected]> | ||
* @license https://opensource.org/licenses/MIT | ||
* | ||
* @link https://github.com/magicsunday/jsonmapper/ | ||
*/ | ||
class Base | ||
|
@@ -61,7 +62,7 @@ class Base | |
/** | ||
* @var string | ||
*/ | ||
private string $privateProperty = ''; | ||
private $privateProperty = ''; | ||
|
||
/** | ||
* @return string | ||
|
Oops, something went wrong.