Skip to content

Commit

Permalink
Raw values work independently of MappedObject
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Jan 1, 2023
1 parent b013c9d commit 1a13743
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 42 deletions.
35 changes: 0 additions & 35 deletions src/MappedObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,15 @@
namespace Orisai\ObjectMapper;

use Nette\Utils\ObjectHelpers;
use Orisai\Exceptions\Logic\InvalidState;
use Orisai\ObjectMapper\Processing\Options;
use ReflectionException;
use ReflectionProperty;
use function sprintf;

/**
* Base class required for mapped objects
*/
abstract class MappedObject
{

private bool $hasRawValues = false;

/** @var mixed */
private $rawValues;

/**
* @param mixed $values
*
* @internal
*/
public function setRawValues($values): void
{
$this->hasRawValues = true;
$this->rawValues = $values;
}

/**
* @return mixed
*/
public function getRawValues()
{
if (!$this->hasRawValues) {
throw InvalidState::create()
->withMessage(sprintf(
'Cannot get raw values as they were never set. You may achieve it by setting %s::setFillRawValues(true)',
Options::class,
));
}

return $this->rawValues;
}

public function isInitialized(string $property): bool
{
return (new ReflectionProperty($this, $property))->isInitialized($this);
Expand Down
13 changes: 12 additions & 1 deletion src/Processing/DefaultProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ final class DefaultProcessor implements Processor

private SkippedPropertiesContextMap $skippedMap;

private RawValuesMap $rawValuesMap;

public function __construct(MetaLoader $metaLoader, RuleManager $ruleManager, ObjectCreator $objectCreator)
{
$this->metaLoader = $metaLoader;
$this->ruleManager = $ruleManager;
$this->objectCreator = $objectCreator;
$this->skippedMap = new SkippedPropertiesContextMap();
$this->rawValuesMap = new RawValuesMap();
}

/**
Expand Down Expand Up @@ -643,7 +646,7 @@ private function fillObject(

// Set raw data
if ($options->isFillRawValues()) {
$object->setRawValues($rawData);
$this->rawValuesMap->setRawValues($object, $rawData);
}

// Reset mapped properties state
Expand Down Expand Up @@ -681,6 +684,14 @@ private function createHolder(string $class, ClassRuntimeMeta $meta, ?MappedObje
return new ObjectHolder($this->objectCreator, $meta, $class, $object);
}

/**
* @return mixed
*/
public function getRawValues(MappedObject $object)
{
return $this->rawValuesMap->getRawValues($object);
}

// ////////////// //
// Late processing //
// ////////////// //
Expand Down
45 changes: 45 additions & 0 deletions src/Processing/RawValuesMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace Orisai\ObjectMapper\Processing;

use Orisai\Exceptions\Logic\InvalidState;
use Orisai\ObjectMapper\MappedObject;
use WeakMap;
use function sprintf;

final class RawValuesMap
{

/** @var WeakMap<MappedObject, mixed> */
private WeakMap $map;

public function __construct()
{
$this->map = new WeakMap();
}

/**
* @param mixed $values
*/
public function setRawValues(MappedObject $object, $values): void
{
$this->map->offsetSet($object, $values);
}

/**
* @return mixed
*/
public function getRawValues(MappedObject $object)
{
if (!$this->map->offsetExists($object)) {
throw InvalidState::create()
->withMessage(sprintf(
'Cannot get raw values as they were never set. You may achieve it by setting %s::setFillRawValues(true)',
Options::class,
));
}

return $this->map->offsetGet($object);
}

}
4 changes: 2 additions & 2 deletions tests/Toolkit/ProcessingTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Orisai\ObjectMapper\Context\TypeContext;
use Orisai\ObjectMapper\Meta\DefaultValueMeta;
use Orisai\ObjectMapper\Meta\MetaLoader;
use Orisai\ObjectMapper\Processing\DefaultProcessor;
use Orisai\ObjectMapper\Processing\Options;
use Orisai\ObjectMapper\Processing\Processor;
use Orisai\ObjectMapper\Rules\DefaultRuleManager;
use Orisai\ObjectMapper\Tester\ObjectMapperTester;
use Orisai\ObjectMapper\Tester\TesterDependencies;
Expand All @@ -26,7 +26,7 @@ abstract class ProcessingTestCase extends TestCase

protected DefaultRuleManager $ruleManager;

protected Processor $processor;
protected DefaultProcessor $processor;

protected function setUp(): void
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Processing/DefaultProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public function testTransformation(): void
self::assertSame(123.456, $vo->float);
self::assertNull($vo->stdClassOrNull);

self::assertSame($data, $vo->getRawValues());
self::assertSame($data, $this->processor->getRawValues($vo));
}

public function testDontUseConstructor(): void
Expand Down Expand Up @@ -655,7 +655,7 @@ public function testBeforeClassCallbackMixedValue(): void
$this->formatter->printError($exception),
);
$vo = $this->processor->process(true, BeforeClassCallbackMixedValueVO::class, $options);
self::assertTrue($vo->getRawValues());
self::assertTrue($this->processor->getRawValues($vo));
}

public function testBeforeClassCallbackRuleException(): void
Expand Down
8 changes: 6 additions & 2 deletions tools/phpstan.tests.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ parameters:

ignoreErrors:
# Tests are expected to throw exceptions
- message: "#^Method (.+) throws checked exception (.+) but it's missing from the PHPDoc @throws tag\\.$#"
path: %currentWorkingDirectory%/tests/Unit
- message: '#^Method (.+) throws checked exception (.+) but it''s missing from the PHPDoc @throws tag\.$#'
path: ../tests/Unit

# Temporary workaround
- message: '#^Property (.+)\:\:\$processor \((.+)DefaultProcessor\) does not accept (.+)\\Processor\.$#'
path: ../tests/Toolkit/ProcessingTestCase.php

0 comments on commit 1a13743

Please sign in to comment.