- Merged getValue override
This commit is contained in:
1 parent
b0b3aca6d9
commit
fd5d31fa17
11 files changed
+183
-106
No files matched your search
+46
-7
@@ -4,7 +4,8 @@ namespace Ulmus;
|
||||
|
||||
use Notes\Attribute\Ignore;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Ulmus\{Attribute\Property\Join,
|
||||
use Ulmus\{Attribute\Obj\JsonSerialize,
|
||||
Attribute\Property\Join,
|
||||
Attribute\Property\Relation,
|
||||
Attribute\Property\ResettablePropertyInterface,
|
||||
Attribute\Property\Virtual,
|
||||
@@ -19,6 +20,7 @@ use Ulmus\SearchRequest\{Attribute\SearchParameter,
|
||||
SearchRequestFromRequestTrait,
|
||||
SearchRequestPaginationTrait};
|
||||
|
||||
#[JsonSerialize(includeRelations: true)]
|
||||
trait EntityTrait {
|
||||
use EventTrait;
|
||||
|
||||
@@ -86,21 +88,25 @@ trait EntityTrait {
|
||||
return $this->entityLoadedDataset;
|
||||
}
|
||||
|
||||
$dataset = [];
|
||||
return iterator_to_array($this->entityYieldDataset($includeRelations, $rewriteValue));
|
||||
}
|
||||
|
||||
|
||||
#[Ignore]
|
||||
protected function entityYieldDataset(bool $includeRelations = false, bool $rewriteValue = true) : \Generator
|
||||
{
|
||||
foreach($this->datasetHandler->pull($this) as $field => $value) {
|
||||
$dataset[$field] = $rewriteValue ? static::repository()->adapter->adapter()->writableValue($value) : $value;
|
||||
yield $field => $rewriteValue ? static::repository()->adapter->adapter()->writableValue($value) : $value;
|
||||
}
|
||||
|
||||
if ($includeRelations) {
|
||||
foreach($this->datasetHandler->pullRelation($this) as $field => $object) {
|
||||
$dataset[$field] = $object;
|
||||
yield $field => $object;
|
||||
}
|
||||
}
|
||||
|
||||
return $dataset;
|
||||
}
|
||||
|
||||
|
||||
#[Ignore]
|
||||
public function resetVirtualProperties() : self
|
||||
{
|
||||
@@ -219,7 +225,40 @@ trait EntityTrait {
|
||||
#[Ignore]
|
||||
public function jsonSerialize() : mixed
|
||||
{
|
||||
return $this->entityGetDataset(true, false, false);
|
||||
# Allows overridding of this method
|
||||
return $this->_jsonSerialize();
|
||||
}
|
||||
|
||||
#[Ignore]
|
||||
public function _jsonSerialize() : mixed
|
||||
{
|
||||
$resolver = static::resolveEntity();
|
||||
$objectAttribute = $resolver->getAttributeImplementing(JsonSerialize::class);
|
||||
|
||||
$dataset = [];
|
||||
|
||||
foreach($this->entityYieldDataset($objectAttribute->includeRelations ?? true, false, false) as $key => $value) {
|
||||
$field = $resolver->searchField($key, EntityResolver::KEY_COLUMN_NAME);
|
||||
|
||||
if ($field) {
|
||||
$jsonSerialize = $field->getAttribute(\Ulmus\Attribute\Property\JsonSerialize::class);
|
||||
if ($jsonSerialize) {
|
||||
# Should we ignore the field ?
|
||||
if ($jsonSerialize->object->ignoreField) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_object($value) && ($value instanceof JsonSerializable) ) {
|
||||
$dataset[$key] = $value->jsonSerialize();
|
||||
}
|
||||
else {
|
||||
$dataset[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $dataset;
|
||||
}
|
||||
|
||||
#[Ignore]
|
||||
|
||||
Reference in new issue
Block a user