Skip to content

Commit

Permalink
Consider discriminator value on properties processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitro-N committed Dec 18, 2022
1 parent f000b9a commit 8c122c4
Showing 1 changed file with 48 additions and 19 deletions.
67 changes: 48 additions & 19 deletions DataSchema/DataSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class DataSchema
'hidden' => false,
'conditions' => [],
'roles' => [],
'hasSubclasses' => false,
'discriminatorColumnName' => null,
'discriminatorMap' => [],
'tableName' => null
Expand Down Expand Up @@ -215,6 +216,7 @@ protected function prepareConfiguration(array $configuration,

if ($classMetadata instanceof ClassMetadata) {
if ($classMetadata->subClasses) {
$configuration['hasSubclasses'] = true;
$configuration['discriminatorColumnName'] = $classMetadata->discriminatorColumn['name'];
$configuration['discriminatorMap'] = $classMetadata->discriminatorMap;
$discriminatorMap = $configuration['discriminatorMap'];
Expand Down Expand Up @@ -337,35 +339,36 @@ protected function decode($value, string $decodeString, TransformEvent $transfor
*/
private function fetchMissingPropertiesRecursive(array $data, array $config, array $scopeConfig = null): array
{
$id = $data['id'] ?? null;
$class = $this->getDataClassName($config, $data);
$metadata = $this->dataSchemaService->getClassMetadata($class);
$id = $data['id'] ?? null;
$class = $this->getDataClassName($config, $data);
$discriminator = $config['hasSubclasses'] ? $this->getDiscriminatorValue($config, $data) : null;
$metadata = $this->dataSchemaService->getClassMetadata($class);

$result = $data + [];
$fields = [];

foreach ($config['properties'] as $propertyName => $propertyConfig) {
$propertyScopeConfig = $scopeConfig[$propertyName] ?? [];
$isNested = $this->dataSchemaService->isNestedProperty($propertyConfig);
$isFromDb = $propertyConfig['from_db'] ?? false;
$propertyScopeConfig = $scopeConfig[$propertyName] ?? [];
$propertyDiscriminator = $propertyConfig['discriminator'] ?? null;
$isNested = $this->dataSchemaService->isNestedProperty($propertyConfig);
$isFromDb = $propertyConfig['from_db'] ?? false;

$value = null;
$source = $propertyConfig['source'] ?? null;

if ($discriminator && $propertyDiscriminator && $discriminator !== $propertyDiscriminator) {
continue;
}

if ($source) {
$querySelects = $this->getQuerySelects($config);
$select = $querySelects[$source] ?? null;

if (!array_key_exists($source, $data) && !$select) {
throw new \RuntimeException("Source \"$source\" must be defined in properties or selects.");
}

if ($select) {
$data[$source] = $this->persister->getSelectQueryResult($class, $select, $id);
$value = $data[$source];
}

$value = $data[$source];

} elseif (array_key_exists($propertyName, $data)) {
$value = $data[$propertyName];

Expand Down Expand Up @@ -433,15 +436,21 @@ private function modifyPropertiesRecursive(array $data,
string $parentClassName = null,
string $parentPropertyName = null): array
{
$class = $this->getDataClassName($config, $data);
$class = $this->getDataClassName($config, $data);
$discriminator = $config['hasSubclasses'] ? $this->getDiscriminatorValue($config, $data) : null;

$result = [];

foreach ($config['properties'] as $propertyName => $propertyConfig) {
$value = null;
$propertyScopeConfig = $scopeConfig[$propertyName] ?? [];
$isHidden = $propertyConfig['hidden'] ?? false;
$source = $propertyConfig['source'] ?? null;
$value = null;
$propertyScopeConfig = $scopeConfig[$propertyName] ?? [];
$propertyDiscriminator = $propertyConfig['discriminator'] ?? null;
$isHidden = $propertyConfig['hidden'] ?? false;
$source = $propertyConfig['source'] ?? null;

if ($discriminator && $propertyDiscriminator && $discriminator !== $propertyDiscriminator) {
continue;
}

if ($source) {
if (!array_key_exists($source, $data)) {
Expand Down Expand Up @@ -830,6 +839,26 @@ private function isOnlyNullInArray(array $array): bool
return true;
}

/**
* @param array $config
* @param array $data
* @return string
*/
private function getDiscriminatorValue(array $config, array $data): string
{
if (!$config['hasSubclasses']) {
throw new \InvalidArgumentException("Only class configurations with subclasses may have discriminator");
}

$discriminatorColumnName = $config['discriminatorColumnName'];

if (empty($data[$discriminatorColumnName])) {
throw new \InvalidArgumentException("Discriminator field \"$discriminatorColumnName\" must have value");
}

return $data[$discriminatorColumnName];
}

/**
* @param array $config
* @param array $data
Expand All @@ -839,8 +868,8 @@ private function getDataClassName(array $config, array $data): string
{
$class = $config['class'];

if ($config['discriminatorMap'] && isset($data[$config['discriminatorColumnName']])) {
$discriminator = $data[$config['discriminatorColumnName']];
if ($config['hasSubclasses']) {
$discriminator = $this->getDiscriminatorValue($config, $data);
$class = $config['discriminatorMap'][$discriminator];
}

Expand Down

0 comments on commit 8c122c4

Please sign in to comment.