Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykbaszak committed Oct 16, 2023
1 parent 2c637f0 commit 245d02a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 22 deletions.
57 changes: 57 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace PBaszak\DedicatedMapper;

use PBaszak\DedicatedMapper\Reflection\ClassReflection;
use PBaszak\DedicatedMapper\Reflection\ReflectionFactory;

class Config
{
/** @var array<string> */
protected array $usedFiles = [];
protected ClassReflection $classReflection;

/**
* @param class-string $className
*/
public function __construct(private string $className)
{
if (!class_exists($this->className, false)) {
throw new \InvalidArgumentException(
sprintf('Class %s does not exist', $this->className)
);
}
}

public function reflect(): self
{
$this->classReflection = (new ReflectionFactory())->createClassReflection($this->className, null);

return $this;
}

public function getClassReflection(): ClassReflection
{
return $this->classReflection;
}

public function export(): array
{
return [
'className' => $this->className,
'classReflection' => $this->classReflection,
'usedFiles' => $this->usedFiles,
];
}

public static function import(array $data): self
{
$config = new self($data['className']);
$config->classReflection = $data['classReflection'];
$config->usedFiles = $data['usedFiles'];

return $config;
}
}
1 change: 0 additions & 1 deletion src/Contract/MapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface MapperInterface
* map() function creates mapper (function or class) and use it to map data based on blueprint.
*
* @param class-string $blueprint
* @param boolean $isCollection if `true` then $data is treated as collection of blueprint representations
*/
public function map(
array|object $data,
Expand Down
10 changes: 8 additions & 2 deletions src/Expression/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@

namespace PBaszak\DedicatedMapper\Expression;

use LogicException;

abstract class AbstractBuilder
{
/**
* @param null|class-string $blueprint if You need You can change input or output type.
* For example: if You want to map dto to entity.
*/
public function __construct(protected ?string $blueprint = null)
{}
{
if (null !== $blueprint && !class_exists($blueprint, false)) {
throw new LogicException("Given (`$blueprint`) blueprint class does not exists.");
}
}

public function getBlueprint(): ?string
{
return $this->blueprint;
}
}
}
17 changes: 0 additions & 17 deletions src/Expression/Builder/ClassObjectExpressionBuilder.php

This file was deleted.

4 changes: 2 additions & 2 deletions tests/assets/DummyByFormats.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PBaszak\DedicatedMapper\Expression\Builder\AnonymousObjectExpressionBuilder;
use PBaszak\DedicatedMapper\Expression\Builder\ArrayExpressionBuilder;
use PBaszak\DedicatedMapper\Expression\Builder\ReflectionClassExpressionBuilder;
use PBaszak\DedicatedMapper\Expression\Builder\ReflectionClassObjectExpressionBuilder;
use PBaszak\DedicatedMapper\Tests\assets\Dummy;
use PBaszak\DedicatedMapper\Tests\assets\EmbeddedDTO;
use PBaszak\DedicatedMapper\Tests\assets\ItemDTO;
Expand Down Expand Up @@ -119,7 +119,7 @@
],
],
],
ReflectionClassExpressionBuilder::class => new Dummy(...[
ReflectionClassObjectExpressionBuilder::class => new Dummy(...[
'id' => 'e2a85ae5-490b-4747-abc1-6efa3352a587',
'name' => 'test',
'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla nec velit vitae arcu aliquam tincidunt.',
Expand Down

0 comments on commit 245d02a

Please sign in to comment.