Skip to content

Commit 3537fdc

Browse files
committed
Raw values work independently of MappedObject
1 parent c162cd6 commit 3537fdc

File tree

6 files changed

+67
-42
lines changed

6 files changed

+67
-42
lines changed

src/MappedObject.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,15 @@
33
namespace Orisai\ObjectMapper;
44

55
use Nette\Utils\ObjectHelpers;
6-
use Orisai\Exceptions\Logic\InvalidState;
7-
use Orisai\ObjectMapper\Processing\Options;
86
use ReflectionException;
97
use ReflectionProperty;
10-
use function sprintf;
118

129
/**
1310
* Base class required for mapped objects
1411
*/
1512
abstract class MappedObject
1613
{
1714

18-
private bool $hasRawValues = false;
19-
20-
/** @var mixed */
21-
private $rawValues;
22-
23-
/**
24-
* @param mixed $values
25-
*
26-
* @internal
27-
*/
28-
public function setRawValues($values): void
29-
{
30-
$this->hasRawValues = true;
31-
$this->rawValues = $values;
32-
}
33-
34-
/**
35-
* @return mixed
36-
*/
37-
public function getRawValues()
38-
{
39-
if (!$this->hasRawValues) {
40-
throw InvalidState::create()
41-
->withMessage(sprintf(
42-
'Cannot get raw values as they were never set. You may achieve it by setting %s::setFillRawValues(true)',
43-
Options::class,
44-
));
45-
}
46-
47-
return $this->rawValues;
48-
}
49-
5015
public function isInitialized(string $property): bool
5116
{
5217
return (new ReflectionProperty($this, $property))->isInitialized($this);

src/Processing/DefaultProcessor.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,15 @@ final class DefaultProcessor implements Processor
5555

5656
private SkippedPropertiesContextMap $skippedMap;
5757

58+
private RawValuesMap $rawValuesMap;
59+
5860
public function __construct(MetaLoader $metaLoader, RuleManager $ruleManager, ObjectCreator $objectCreator)
5961
{
6062
$this->metaLoader = $metaLoader;
6163
$this->ruleManager = $ruleManager;
6264
$this->objectCreator = $objectCreator;
6365
$this->skippedMap = new SkippedPropertiesContextMap();
66+
$this->rawValuesMap = new RawValuesMap();
6467
}
6568

6669
/**
@@ -643,7 +646,7 @@ private function fillObject(
643646

644647
// Set raw data
645648
if ($options->isFillRawValues()) {
646-
$object->setRawValues($rawData);
649+
$this->rawValuesMap->setRawValues($object, $rawData);
647650
}
648651

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

687+
/**
688+
* @return mixed
689+
*/
690+
public function getRawValues(MappedObject $object)
691+
{
692+
return $this->rawValuesMap->getRawValues($object);
693+
}
694+
684695
// ////////////// //
685696
// Late processing //
686697
// ////////////// //

src/Processing/RawValuesMap.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Orisai\ObjectMapper\Processing;
4+
5+
use Orisai\Exceptions\Logic\InvalidState;
6+
use Orisai\ObjectMapper\MappedObject;
7+
use WeakMap;
8+
use function sprintf;
9+
10+
final class RawValuesMap
11+
{
12+
13+
/** @var WeakMap<MappedObject, mixed> */
14+
private WeakMap $map;
15+
16+
public function __construct()
17+
{
18+
$this->map = new WeakMap();
19+
}
20+
21+
/**
22+
* @param mixed $values
23+
*/
24+
public function setRawValues(MappedObject $object, $values): void
25+
{
26+
$this->map->offsetSet($object, $values);
27+
}
28+
29+
/**
30+
* @return mixed
31+
*/
32+
public function getRawValues(MappedObject $object)
33+
{
34+
if (!$this->map->offsetExists($object)) {
35+
throw InvalidState::create()
36+
->withMessage(sprintf(
37+
'Cannot get raw values as they were never set. You may achieve it by setting %s::setFillRawValues(true)',
38+
Options::class,
39+
));
40+
}
41+
42+
return $this->map->offsetGet($object);
43+
}
44+
45+
}

tests/Toolkit/ProcessingTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Orisai\ObjectMapper\Context\TypeContext;
88
use Orisai\ObjectMapper\Meta\DefaultValueMeta;
99
use Orisai\ObjectMapper\Meta\MetaLoader;
10+
use Orisai\ObjectMapper\Processing\DefaultProcessor;
1011
use Orisai\ObjectMapper\Processing\Options;
11-
use Orisai\ObjectMapper\Processing\Processor;
1212
use Orisai\ObjectMapper\Rules\DefaultRuleManager;
1313
use Orisai\ObjectMapper\Tester\ObjectMapperTester;
1414
use Orisai\ObjectMapper\Tester\TesterDependencies;
@@ -26,7 +26,7 @@ abstract class ProcessingTestCase extends TestCase
2626

2727
protected DefaultRuleManager $ruleManager;
2828

29-
protected Processor $processor;
29+
protected DefaultProcessor $processor;
3030

3131
protected function setUp(): void
3232
{

tests/Unit/Processing/DefaultProcessorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ public function testTransformation(): void
409409
self::assertSame(123.456, $vo->float);
410410
self::assertNull($vo->stdClassOrNull);
411411

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

415415
public function testDontUseConstructor(): void
@@ -655,7 +655,7 @@ public function testBeforeClassCallbackMixedValue(): void
655655
$this->formatter->printError($exception),
656656
);
657657
$vo = $this->processor->process(true, BeforeClassCallbackMixedValueVO::class, $options);
658-
self::assertTrue($vo->getRawValues());
658+
self::assertTrue($this->processor->getRawValues($vo));
659659
}
660660

661661
public function testBeforeClassCallbackRuleException(): void

tools/phpstan.tests.neon

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ parameters:
1010

1111
ignoreErrors:
1212
# Tests are expected to throw exceptions
13-
- message: "#^Method (.+) throws checked exception (.+) but it's missing from the PHPDoc @throws tag\\.$#"
14-
path: %currentWorkingDirectory%/tests/Unit
13+
- message: '#^Method (.+) throws checked exception (.+) but it''s missing from the PHPDoc @throws tag\.$#'
14+
path: ../tests/Unit
15+
16+
# Temporary workaround
17+
- message: '#^Property (.+)\:\:\$processor \((.+)DefaultProcessor\) does not accept (.+)\\Processor\.$#'
18+
path: ../tests/Toolkit/ProcessingTestCase.php

0 commit comments

Comments
 (0)