2929use Orisai \ObjectMapper \Types \MappedObjectType ;
3030use Orisai \ObjectMapper \Types \MessageType ;
3131use Orisai \ObjectMapper \Types \Type ;
32- use ReflectionProperty ;
3332use function array_key_exists ;
3433use function array_keys ;
3534use function array_map ;
@@ -51,7 +50,10 @@ final class DefaultProcessor implements Processor
5150 private ServicesContext $ services ;
5251
5352 /** @var Closure(MappedObject, string, mixed): void */
54- private ?Closure $ setterFunction ;
53+ private Closure $ setFunction ;
54+
55+ /** @var Closure(MappedObject, string): void */
56+ private Closure $ unsetFunction ;
5557
5658 /** @var array<class-string<MappedObject>, RuntimeMeta> */
5759 private array $ metaCache = [];
@@ -67,9 +69,12 @@ public function __construct(MetaLoader $metaLoader, RuleManager $ruleManager, Ob
6769 $ this ->rawValuesMap = new RawValuesMap ();
6870 $ this ->services = new ServicesContext ($ metaLoader , $ ruleManager , $ this );
6971 // phpcs:disable SlevomatCodingStandard.Functions.StaticClosure
70- $ this ->setterFunction = function (MappedObject $ object , string $ name , $ value ): void {
72+ $ this ->setFunction = function (MappedObject $ object , string $ name , $ value ): void {
7173 $ object ->$ name = $ value ;
7274 };
75+ $ this ->unsetFunction = function (MappedObject $ object , string $ name ): void {
76+ unset($ object ->$ name );
77+ };
7378 // phpcs:enable
7479 }
7580
@@ -557,13 +562,29 @@ private function fillObject(
557562 $ fieldsMeta = $ meta ->fields ;
558563
559564 if ($ dynamic ->getOptions ()->getRequiredFields () === RequiredFields::none ()) {
565+ $ unsetter = $ this ->unsetFunction ;
566+
560567 // Reset mapped properties state
561568 foreach ($ fieldsMeta as $ fieldMeta ) {
562- $ this ->objectUnset ($ object , $ fieldMeta ->property );
569+ $ property = $ fieldMeta ->property ;
570+ $ declaringClass = $ property ->getDeclaringClass ();
571+ $ name = $ property ->getName ();
572+
573+ if (
574+ $ property ->isInitialized ($ object )
575+ && $ property ->isPublic ()
576+ && (PHP_VERSION_ID < 8_01_00 || !$ property ->isReadOnly ())
577+ ) {
578+ unset($ object ->$ name );
579+ } else {
580+ // phpcs:disable SlevomatCodingStandard.Functions.StaticClosure
581+ $ unsetter ->bindTo ($ object , $ declaringClass ->getName ())($ object , $ name );
582+ // phpcs:enable
583+ }
563584 }
564585 }
565586
566- $ setter = $ this ->setterFunction ;
587+ $ setter = $ this ->setFunction ;
567588
568589 // Set processed data
569590 foreach ($ data as $ fieldName => $ value ) {
@@ -586,24 +607,4 @@ public function getRawValues(MappedObject $object)
586607 return $ this ->rawValuesMap ->getRawValues ($ object );
587608 }
588609
589- private function objectUnset (MappedObject $ object , ReflectionProperty $ property ): void
590- {
591- $ declaringClass = $ property ->getDeclaringClass ();
592- $ name = $ property ->getName ();
593-
594- if (
595- $ property ->isInitialized ($ object )
596- && $ property ->isPublic ()
597- && (PHP_VERSION_ID < 8_01_00 || !$ property ->isReadOnly ())
598- ) {
599- unset($ object ->$ name );
600- } else {
601- // phpcs:disable SlevomatCodingStandard.Functions.StaticClosure
602- (function () use ($ object , $ name ): void {
603- unset($ object ->$ name );
604- })->bindTo ($ object , $ declaringClass ->getName ())();
605- // phpcs:enable
606- }
607- }
608-
609610}
0 commit comments