Skip to content

Commit a65f6d2

Browse files
author
n.gnato
committed
BaseKeyValueStructure extention
1 parent 6458c54 commit a65f6d2

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## Added
10+
- BaseKeyValueStructure::assignFieldValue() method for inject custom field instantiation logic.
11+
912
## [0.0.7] - 2025-06-26
1013

1114
### Fixed

src/FreeElephants/JsonApi/DTO/BaseKeyValueStructure.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,29 @@ class BaseKeyValueStructure
66
{
77
public function __construct(array $attributes)
88
{
9-
$concreteClass = new \ReflectionClass($this);
109
foreach ($attributes as $name => $value) {
11-
$property = $concreteClass->getProperty($name);
12-
if ($property->hasType()) {
13-
$propertyType = $property->getType();
14-
if ($propertyType instanceof \ReflectionNamedType && !$propertyType->isBuiltin()) {
15-
if($propertyType->allowsNull() && is_null($value)) {
16-
$value = null;
17-
} else {
18-
$propertyClassName = $propertyType->getName();
19-
$value = new $propertyClassName($value);
20-
}
10+
$this->assignFieldValue($name, $value);
11+
}
12+
}
13+
14+
protected function assignFieldValue(string $name, $value): self
15+
{
16+
$concreteClass = new \ReflectionClass($this);
17+
$property = $concreteClass->getProperty($name);
18+
if ($property->hasType()) {
19+
$propertyType = $property->getType();
20+
if ($propertyType instanceof \ReflectionNamedType && !$propertyType->isBuiltin()) {
21+
if($propertyType->allowsNull() && is_null($value)) {
22+
$value = null;
23+
} else {
24+
$propertyClassName = $propertyType->getName();
25+
$value = new $propertyClassName($value);
2126
}
2227
}
23-
$this->{$name} = $value;
2428
}
29+
30+
$this->$name = $value;
31+
32+
return $this;
2533
}
2634
}

0 commit comments

Comments
 (0)