Skip to content

Commit 6b5eecb

Browse files
committed
Inheritance: Allow reference to reference, more specific classname
1 parent e551877 commit 6b5eecb

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

src/Utils/BeanDescriptor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public function getBeanPropertyDescriptors(): array
201201
public function getConstructorProperties(): array
202202
{
203203
$constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
204-
return !$property instanceof ScalarReferencePropertyDescriptor && $property->isCompulsory();
204+
return !$property instanceof InheritanceReferencePropertyDescriptor && $property->isCompulsory();
205205
});
206206

207207
return $constructorProperties;
@@ -230,7 +230,7 @@ public function getPropertiesWithDefault(): array
230230
public function getExposedProperties(): array
231231
{
232232
$exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
233-
return !$property instanceof ScalarReferencePropertyDescriptor && $property->getTable()->getName() === $this->table->getName();
233+
return !$property instanceof InheritanceReferencePropertyDescriptor && $property->getTable()->getName() === $this->table->getName();
234234
});
235235

236236
return $exposedProperties;
@@ -256,7 +256,7 @@ private function getProperties(Table $table): array
256256
$localProperties = $this->getPropertiesForTable($table);
257257
foreach ($localProperties as $name => $property) {
258258
// We do not override properties if this is a primary key!
259-
if (!$property instanceof ScalarReferencePropertyDescriptor && $property->isPrimaryKey()) {
259+
if (!$property instanceof InheritanceReferencePropertyDescriptor && $property->isPrimaryKey()) {
260260
continue;
261261
}
262262
$properties[$name] = $property;
@@ -302,7 +302,7 @@ private function getPropertiesForTable(Table $table): array
302302
// Check that this property is not an inheritance relationship
303303
$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
304304
if ($parentRelationship !== null && $parentRelationship->getName() === $fk->getName()) {
305-
$beanPropertyDescriptors[] = new ScalarReferencePropertyDescriptor(
305+
$beanPropertyDescriptors[] = new InheritanceReferencePropertyDescriptor(
306306
$table,
307307
$column,
308308
$this->namingStrategy,

src/Utils/ScalarReferencePropertyDescriptor.php renamed to src/Utils/InheritanceReferencePropertyDescriptor.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser;
99

1010
/**
11-
* This class represents a reference to a AbstractBeanPropertyDescriptor
11+
* This class represents a reference to a AbstractBeanPropertyDescriptor used in an inheritance schema
1212
*/
13-
class ScalarReferencePropertyDescriptor extends ScalarBeanPropertyDescriptor
13+
class InheritanceReferencePropertyDescriptor extends ScalarBeanPropertyDescriptor
1414
{
1515
/** @var AbstractBeanPropertyDescriptor */
1616
private $referencedPropertyDescriptor;
@@ -27,15 +27,18 @@ public function __construct(
2727
}
2828

2929
/**
30-
* @return AbstractBeanPropertyDescriptor
30+
* @return ScalarBeanPropertyDescriptor|ObjectBeanPropertyDescriptor
3131
*/
32-
public function getReferencedPropertyDescriptor(): AbstractBeanPropertyDescriptor
32+
public function getNonScalarReferencedPropertyDescriptor(): AbstractBeanPropertyDescriptor
3333
{
34+
if ($this->referencedPropertyDescriptor instanceof InheritanceReferencePropertyDescriptor) {
35+
return $this->referencedPropertyDescriptor->getNonScalarReferencedPropertyDescriptor();
36+
}
3437
return $this->referencedPropertyDescriptor;
3538
}
3639

3740
public function getJsonSerializeCode(): string
3841
{
39-
return $this->referencedPropertyDescriptor->getJsonSerializeCode();
42+
return $this->getNonScalarReferencedPropertyDescriptor()->getJsonSerializeCode();
4043
}
4144
}

src/Utils/ObjectBeanPropertyDescriptor.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,20 +253,22 @@ public function getJsonSerializeCode(): string
253253
return $code;
254254
}
255255

256-
public function getLazySerializeCode(string $propertyAccess): string
256+
private function getLazySerializeCode(string $propertyAccess): string
257257
{
258258
$rows = [];
259259
foreach ($this->getForeignKey()->getUnquotedForeignColumns() as $column) {
260260
$descriptor = $this->getBeanPropertyDescriptor($column);
261-
if ($descriptor instanceof ScalarReferencePropertyDescriptor) {
262-
$descriptor = $descriptor->getReferencedPropertyDescriptor();
261+
if ($descriptor instanceof InheritanceReferencePropertyDescriptor) {
262+
$descriptor = $descriptor->getNonScalarReferencedPropertyDescriptor();
263263
}
264264
if ($descriptor instanceof ObjectBeanPropertyDescriptor) {
265265
$rows[] = trim($descriptor->getLazySerializeCode($propertyAccess), '[]');
266-
} else {
266+
} elseif ($descriptor instanceof ScalarBeanPropertyDescriptor) {
267267
$indexName = ltrim($descriptor->getVariableName(), '$');
268268
$columnGetterName = $descriptor->getGetterName();
269269
$rows[] = "'$indexName' => $propertyAccess->$columnGetterName()";
270+
} else {
271+
throw new TDBMException('PropertyDescriptor of class `' . get_class($descriptor) . '` cannot be serialized.');
270272
}
271273
}
272274
return '[' . implode(', ', $rows) . ']';

0 commit comments

Comments
 (0)