Skip to content

Commit

Permalink
Restructure and cache contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Jan 21, 2025
1 parent 89b57e3 commit db008a0
Show file tree
Hide file tree
Showing 69 changed files with 1,506 additions and 920 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- `DefaultProcessor` - automatic meta cache reset after main `process()` call (performance optimization)
- `Skipped` modifier and the related code - it was an undocumented feature that is no longer necessary
- `FieldContext` - `getType()` - unused and no longer necessary

## [0.2.0](https://github.com/orisai/object-mapper/compare/0.1.0...0.2.0) - 2024-06-22

Expand Down
20 changes: 9 additions & 11 deletions src/Callbacks/BaseCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Orisai\ObjectMapper\Args\Args;
use Orisai\ObjectMapper\Args\ArgsChecker;
use Orisai\ObjectMapper\Context\ArgsContext;
use Orisai\ObjectMapper\Context\BaseFieldContext;
use Orisai\ObjectMapper\Context\FieldContext;
use Orisai\ObjectMapper\Context\MappedObjectContext;
use Orisai\ObjectMapper\Context\CallbackBaseContext;
use Orisai\ObjectMapper\Context\CallbackObjectContext;
use Orisai\ObjectMapper\Context\CallbackPropertyContext;
use Orisai\ObjectMapper\MappedObject;
use Orisai\ObjectMapper\Processing\ObjectHolder;
use ReflectionClass;
Expand Down Expand Up @@ -225,14 +225,14 @@ private static function validateClassMethodContextParam(
{
if (
($type = self::getTypeName($paramContext->getType())) === null
|| !is_a($type, MappedObjectContext::class, true)
|| !is_a($type, CallbackObjectContext::class, true)
) {
throw InvalidArgument::create()
->withMessage(sprintf(
'Second parameter of class callback method %s::%s should have "%s" type instead of %s',
$class->getName(),
$method->getName(),
MappedObjectContext::class,
CallbackObjectContext::class,
$type ?? 'none',
));
}
Expand Down Expand Up @@ -280,7 +280,7 @@ private static function validatePropertyMethodContextParam(
{
$type = self::getTypeName($paramContext->getType());

if ($type !== null && is_a($type, FieldContext::class, true)) {
if ($type !== null && is_a($type, CallbackPropertyContext::class, true)) {
return;
}

Expand All @@ -289,7 +289,7 @@ private static function validatePropertyMethodContextParam(
'Second parameter of field callback method %s::%s should have "%s" type instead of %s',
$class->getName(),
$method->getName(),
FieldContext::class,
CallbackPropertyContext::class,
$type ?? 'none',
));
}
Expand Down Expand Up @@ -317,16 +317,14 @@ public static function getArgsType(): string
}

/**
* @param mixed $data
* @param BaseCallbackArgs $args
* @param FieldContext|MappedObjectContext $context
* @param BaseCallbackArgs $args
* @return mixed
*/
public static function invoke(
$data,
Args $args,
ObjectHolder $holder,
BaseFieldContext $context,
CallbackBaseContext $context,
ReflectionClass $declaringClass
)
{
Expand Down
20 changes: 10 additions & 10 deletions src/Callbacks/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Orisai\ObjectMapper\Args\Args;
use Orisai\ObjectMapper\Context\ArgsContext;
use Orisai\ObjectMapper\Context\BaseFieldContext;
use Orisai\ObjectMapper\Context\FieldContext;
use Orisai\ObjectMapper\Context\MappedObjectContext;
use Orisai\ObjectMapper\Context\CallbackBaseContext;
use Orisai\ObjectMapper\Context\CallbackObjectContext;
use Orisai\ObjectMapper\Context\CallbackPropertyContext;
use Orisai\ObjectMapper\Exception\InvalidData;
use Orisai\ObjectMapper\Exception\ValueDoesNotMatch;
use Orisai\ObjectMapper\MappedObject;
Expand All @@ -22,7 +22,7 @@ interface Callback
{

/**
* @param array<int|string, mixed> $args
* @param array<int|string, mixed> $args
* @param ReflectionClass<MappedObject>|ReflectionProperty $reflector
* @return T_ARGS
*/
Expand All @@ -34,11 +34,11 @@ public static function resolveArgs(array $args, ArgsContext $context, Reflector
public static function getArgsType(): string;

/**
* @param mixed $data
* @param T_ARGS $args
* @param FieldContext|MappedObjectContext $context
* @param ObjectHolder<MappedObject> $holder
* @param ReflectionClass<MappedObject> $declaringClass
* @param mixed $data
* @param T_ARGS $args
* @param CallbackObjectContext|CallbackPropertyContext $context
* @param ObjectHolder<MappedObject> $holder
* @param ReflectionClass<MappedObject> $declaringClass
* @return mixed
* @throws ValueDoesNotMatch
* @throws InvalidData
Expand All @@ -47,7 +47,7 @@ public static function invoke(
$data,
Args $args,
ObjectHolder $holder,
BaseFieldContext $context,
CallbackBaseContext $context,
ReflectionClass $declaringClass
);

Expand Down
40 changes: 0 additions & 40 deletions src/Context/BaseFieldContext.php

This file was deleted.

42 changes: 42 additions & 0 deletions src/Context/CallbackBaseContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Orisai\ObjectMapper\Context;

use Orisai\ObjectMapper\Processing\Options;
use Orisai\ObjectMapper\Processing\Processor;
use Orisai\ObjectMapper\Types\Type;

abstract class CallbackBaseContext
{

private ServicesContext $services;

private DynamicContext $dynamic;

public function __construct(
ServicesContext $services,
DynamicContext $dynamic
)
{
$this->services = $services;
$this->dynamic = $dynamic;
}

abstract public function getType(): Type;

public function getProcessor(): Processor
{
return $this->services->getProcessor();
}

public function getOptions(): Options
{
return $this->dynamic->getOptions();
}

public function shouldInitializeObjects(): bool
{
return $this->dynamic->shouldInitializeObjects();
}

}
32 changes: 32 additions & 0 deletions src/Context/CallbackObjectContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace Orisai\ObjectMapper\Context;

use Orisai\ObjectMapper\MappedObject;
use Orisai\ObjectMapper\Types\MappedObjectType;

final class CallbackObjectContext extends CallbackBaseContext
{

/** @var ProcessorCallContext<MappedObject> */
private ProcessorCallContext $call;

/**
* @param ProcessorCallContext<MappedObject> $call
*/
public function __construct(
ServicesContext $services,
DynamicContext $dynamic,
ProcessorCallContext $call
)
{
parent::__construct($services, $dynamic);
$this->call = $call;
}

public function getType(): MappedObjectType
{
return $this->call->getType();
}

}
58 changes: 58 additions & 0 deletions src/Context/CallbackPropertyContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types = 1);

namespace Orisai\ObjectMapper\Context;

use Closure;
use Orisai\ObjectMapper\Types\Type;

final class CallbackPropertyContext extends CallbackBaseContext
{

private PropertyContext $property;

/** @var Closure(): Type */
private Closure $typeCreator;

private ?Type $type = null;

/**
* @param Closure(): Type $typeCreator
*/
public function __construct(
ServicesContext $services,
DynamicContext $dynamic,
PropertyContext $property,
Closure $typeCreator
)
{
parent::__construct($services, $dynamic);
$this->property = $property;
$this->typeCreator = $typeCreator;
}

public function getType(): Type
{
if ($this->type !== null) {
return $this->type;
}

$type = ($this->typeCreator)();
unset($this->typeCreator);

return $this->type = $type;
}

public function getPropertyName(): string
{
return $this->property->getPropertyName();
}

/**
* @return int|string
*/
public function getFieldName()
{
return $this->property->getFieldName();
}

}
54 changes: 17 additions & 37 deletions src/Context/TypeContext.php → src/Context/DynamicContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,44 @@
namespace Orisai\ObjectMapper\Context;

use Orisai\ObjectMapper\MappedObject;
use Orisai\ObjectMapper\Meta\MetaLoader;
use Orisai\ObjectMapper\Meta\Runtime\RuntimeMeta;
use Orisai\ObjectMapper\Processing\Options;
use Orisai\ObjectMapper\Rules\Rule;
use Orisai\ObjectMapper\Rules\RuleManager;
use function array_keys;

class TypeContext
final class DynamicContext
{

private MetaLoader $metaLoader;

private RuleManager $ruleManager;

private Options $options;

private bool $initializeObjects;

/** @var array<class-string<MappedObject>, true> */
private array $processedClasses = [];

public function __construct(MetaLoader $metaLoader, RuleManager $ruleManager, Options $options)
public function __construct(Options $options, bool $initializeObjects)
{
$this->metaLoader = $metaLoader;
$this->ruleManager = $ruleManager;
$this->options = $options;
$this->initializeObjects = $initializeObjects;
}

public function getOptions(): Options
/**
* @return static
*/
public function createClone(): self
{
return $this->options;
$clone = clone $this;
$clone->options = $this->options->createClone();

return $clone;
}

/**
* @param class-string<MappedObject> $class
*/
public function getMeta(string $class): RuntimeMeta
public function getOptions(): Options
{
return $this->metaLoader->load($class);
return $this->options;
}

/**
* @template T of Rule
* @param class-string<T> $rule
* @return T
*/
public function getRule(string $rule): Rule
public function shouldInitializeObjects(): bool
{
return $this->ruleManager->getRule($rule);
return $this->initializeObjects;
}

/**
Expand All @@ -72,15 +63,4 @@ public function getProcessedClasses(): array
return array_keys($this->processedClasses);
}

/**
* @return static
*/
public function createClone(): self
{
$clone = clone $this;
$clone->options = $this->options->createClone();

return $clone;
}

}
Loading

0 comments on commit db008a0

Please sign in to comment.