From 1a137432ff2426f874f878071614280fa77ad98f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Barto=C5=A1?= Date: Sun, 1 Jan 2023 00:09:55 +0100 Subject: [PATCH] Raw values work independently of MappedObject --- src/MappedObject.php | 35 --------------- src/Processing/DefaultProcessor.php | 13 +++++- src/Processing/RawValuesMap.php | 45 +++++++++++++++++++ tests/Toolkit/ProcessingTestCase.php | 4 +- .../Unit/Processing/DefaultProcessorTest.php | 4 +- tools/phpstan.tests.neon | 8 +++- 6 files changed, 67 insertions(+), 42 deletions(-) create mode 100644 src/Processing/RawValuesMap.php diff --git a/src/MappedObject.php b/src/MappedObject.php index c1dd0245..46870c93 100644 --- a/src/MappedObject.php +++ b/src/MappedObject.php @@ -3,11 +3,8 @@ 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 @@ -15,38 +12,6 @@ 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); diff --git a/src/Processing/DefaultProcessor.php b/src/Processing/DefaultProcessor.php index 5cc4ec2a..81f0143c 100644 --- a/src/Processing/DefaultProcessor.php +++ b/src/Processing/DefaultProcessor.php @@ -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(); } /** @@ -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 @@ -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 // // ////////////// // diff --git a/src/Processing/RawValuesMap.php b/src/Processing/RawValuesMap.php new file mode 100644 index 00000000..733e8ad8 --- /dev/null +++ b/src/Processing/RawValuesMap.php @@ -0,0 +1,45 @@ + */ + 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); + } + +} diff --git a/tests/Toolkit/ProcessingTestCase.php b/tests/Toolkit/ProcessingTestCase.php index 827132ab..0b068253 100644 --- a/tests/Toolkit/ProcessingTestCase.php +++ b/tests/Toolkit/ProcessingTestCase.php @@ -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; @@ -26,7 +26,7 @@ abstract class ProcessingTestCase extends TestCase protected DefaultRuleManager $ruleManager; - protected Processor $processor; + protected DefaultProcessor $processor; protected function setUp(): void { diff --git a/tests/Unit/Processing/DefaultProcessorTest.php b/tests/Unit/Processing/DefaultProcessorTest.php index e7dadc9d..48365819 100644 --- a/tests/Unit/Processing/DefaultProcessorTest.php +++ b/tests/Unit/Processing/DefaultProcessorTest.php @@ -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 @@ -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 diff --git a/tools/phpstan.tests.neon b/tools/phpstan.tests.neon index 8ffff17b..e9af12d4 100644 --- a/tools/phpstan.tests.neon +++ b/tools/phpstan.tests.neon @@ -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