Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PhpCs_AnyFinder" xsi:noNamespaceSchemaLocation="./vendor/squizlabs/php_codesniffer/phpcs.xsd">
<rule ref="./vendor/jerodev/code-styles/phpcs.xml">
<exclude name="SlevomatCodingStandard.Classes.ModernClassNameReference" />
</rule>
<rule ref="./vendor/jerodev/code-styles/phpcs.xml" />
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall" />
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration" />

<file>src</file>
</ruleset>
</ruleset>
20 changes: 20 additions & 0 deletions src/Attributes/PostMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Jerodev\DataMapper\Attributes;

use Attribute;
use Closure;

/**
* Execute a function right after the class has been mapped.
* Can be either the name of a public function on the class, or a closure accepting the new object.
* The function is gets the data array as a first parameter and the new object as the second.
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class PostMapping
{
public function __construct(
public string|Closure $postMappingCallback,
) {
}
}
4 changes: 4 additions & 0 deletions src/Objects/ClassBluePrint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jerodev\DataMapper\Objects;

use Attribute;
use Jerodev\DataMapper\Types\DataTypeCollection;

class ClassBluePrint
Expand All @@ -11,4 +12,7 @@ class ClassBluePrint

/** @var array<string, array{type: DataTypeCollection, default?: mixed}> */
public array $properties = [];

/** @var array<Attribute> */
public array $classAttributes = [];
}
16 changes: 14 additions & 2 deletions src/Objects/ClassBluePrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

namespace Jerodev\DataMapper\Objects;

use Jerodev\DataMapper\Attributes\PostMapping;
use Jerodev\DataMapper\Types\DataType;
use Jerodev\DataMapper\Types\DataTypeCollection;
use Jerodev\DataMapper\Types\DataTypeFactory;
use ReflectionClass;

class ClassBluePrinter
{
private readonly ClassResolver $classResolver;
private readonly DocBlockParser $docBlockParser;
private readonly DataTypeFactory $dataTypeFactory;

public function __construct()
{
$this->classResolver = new ClassResolver();
$this->dataTypeFactory = new DataTypeFactory();
$this->docBlockParser = new DocBlockParser();
}
Expand All @@ -27,6 +26,7 @@ public function print(string $class): ClassBluePrint
$blueprint = new ClassBluePrint();
$this->printConstructor($reflection, $blueprint);
$this->printProperties($reflection, $blueprint);
$this->printAttributes($reflection, $blueprint);

return $blueprint;
}
Expand Down Expand Up @@ -93,6 +93,18 @@ private function printProperties(ReflectionClass $reflection, ClassBluePrint $bl
}
}

private function printAttributes(ReflectionClass $reflection, ClassBluePrint $blueprint): void
{
foreach ($reflection->getAttributes(PostMapping::class) as $attribute) {
$blueprint->classAttributes[] = $attribute->newInstance();
}

// Also check parent for relevant attributes
if ($reflection->getParentClass()) {
$this->printAttributes($reflection->getParentClass(), $blueprint);
}
}

private function resolveType(DataTypeCollection $type, string $className): DataTypeCollection
{
$baseClassName = \explode('\\', $className);
Expand Down
12 changes: 12 additions & 0 deletions src/Objects/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jerodev\DataMapper\Objects;

use Jerodev\DataMapper\Attributes\PostMapping;
use Jerodev\DataMapper\Exceptions\CouldNotResolveClassException;
use Jerodev\DataMapper\Mapper;
use Jerodev\DataMapper\Types\DataType;
Expand Down Expand Up @@ -74,6 +75,17 @@ private function createObjectMappingFunction(string $class, string $mapFunctionN
$content.= \PHP_EOL . ' $x->' . $name . ' = ' . $propertyMap . ';';
}

// Post mapping functions?
foreach ($blueprint->classAttributes as $attribute) {
if ($attribute instanceof PostMapping) {
if (\is_string($attribute->postMappingCallback)) {
$content.= \PHP_EOL . \PHP_EOL . " \$x->{$attribute->postMappingCallback}(\$data, \$x);";
} else {
$content.= \PHP_EOL . \PHP_EOL . " \call_user_func({$attribute->postMappingCallback}, \$data, \$x);";
}
}
}

// Render the function
$mapperClass = Mapper::class;
return <<<PHP
Expand Down
17 changes: 15 additions & 2 deletions tests/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Generator;
use Jerodev\DataMapper\Mapper;
use Jerodev\DataMapper\Tests\_Mocks\SuitEnum;
use Jerodev\DataMapper\Tests\_Mocks\SuperUserDto;
use Jerodev\DataMapper\Tests\_Mocks\UserDto;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -70,11 +71,23 @@ public static function objectValuesDataProvider(): Generator
'name' => 'Jeroen',
'favoriteSuit' => 'D',
'friends' => [
['name' => 'John'],
['name' => 'Jane'],
['name' => 'john'],
['name' => 'jane'],
],
],
$dto
];

$dto = new SuperUserDto('Superman'); // Uppercase because UserDto post mapping
$dto->canFly = true;
$dto->stars = 3; // Increased 3 times by post mapping function
yield [
SuperUserDto::class,
[
'name' => 'superman',
'canFly' => true,
],
$dto,
];
}
}
19 changes: 19 additions & 0 deletions tests/_Mocks/SuperUserDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Jerodev\DataMapper\Tests\_Mocks;

use Jerodev\DataMapper\Attributes\PostMapping;

#[PostMapping('increaseStars')]
#[PostMapping('increaseStars')]
#[PostMapping('increaseStars')]
class SuperUserDto extends UserDto
{
public bool $canFly = false;
public int $stars = 0;

public function increaseStars(): void
{
$this->stars++;
}
}
8 changes: 8 additions & 0 deletions tests/_Mocks/UserDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Jerodev\DataMapper\Tests\_Mocks;

use Jerodev\DataMapper\Attributes\PostMapping;

#[PostMapping('post')]
class UserDto
{
/** First name and last name */
Expand All @@ -15,4 +18,9 @@ public function __construct(string $name)
{
$this->name = $name;
}

public function post(): void
{
$this->name = \ucfirst($this->name);
}
}