Skip to content

Commit

Permalink
Merge pull request #984 from einorler/singular-fields
Browse files Browse the repository at this point in the history
Added a possibility to have singular embedded fields
  • Loading branch information
einorler authored Feb 15, 2023
2 parents fec2527 + 1b2907a commit 5f9da0d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Annotation/Embedded.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ final class Embedded extends AbstractAnnotation implements PropertiesAwareInterf
* @Doctrine\Common\Annotations\Annotation\Required
*/
public $class;

public $singular = false;
}
15 changes: 10 additions & 5 deletions Mapping/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,18 @@ protected function denormalize(array $raw, string $namespace)
$setter = $fieldMeta['setter'];

if ($fieldMeta['embeded']) {
$this->addClassMetadata($fieldMeta['class'], $fieldMeta['sub_properties']);
$iterator = new ObjectIterator($fieldMeta['class'], $value, $this);
$this->addClassMetadata($fieldMeta['class'], $fieldMeta['sub_properties']);

if ($fieldMeta['public']) {
$object->{$fieldMeta['name']} = $iterator;
if ($fieldMeta['singular']) {
$object->$setter($this->denormalize($value, $fieldMeta['class']));
} else {
$object->$setter($iterator);
$iterator = new ObjectIterator($fieldMeta['class'], $value, $this);

if ($fieldMeta['public']) {
$object->{$fieldMeta['name']} = $iterator;
} else {
$object->$setter($iterator);
}
}
} else {
if ($fieldMeta['type'] == 'date') {
Expand Down
2 changes: 2 additions & 0 deletions Mapping/DocumentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private function getClassMetadata(\ReflectionClass $class): array
$fieldMapping['type'] = $this->getObjectMappingType($embeddedClass);
$fieldMapping['properties'] = $this->getClassMetadata($embeddedClass);
$embeddedFields[$name] = $annotation->class;
$embeddedFields['singular'] = $annotation->singular;
}

$mapping[$annotation->getName() ?? Caser::snake($name)] = array_filter($fieldMapping);
Expand Down Expand Up @@ -207,6 +208,7 @@ public function getPropertyMetadata(\ReflectionClass $class, bool $subClass = fa
if ($annotation instanceof Embedded) {
$propertyMetadata['embeded'] = true;
$propertyMetadata['class'] = $annotation->class;
$propertyMetadata['singular'] = $annotation->singular;
$propertyMetadata['sub_properties'] = $this->getPropertyMetadata(
new \ReflectionClass($annotation->class),
true
Expand Down
15 changes: 10 additions & 5 deletions Resources/doc/mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,26 +209,31 @@ class Product
/**
* @var ContentMetaObject
*
* @ES\Embedded(class="App\Document\CategoryObject")
* @ES\Embedded(class="App\Document\CategoryObject", singular=true)
*/
private $category;
//...
public function __construct()
public funtion setCategory($category)
{
$this->category = new ArrayCollection();
$this->category = $category;
}
public funtion addCategory($category)
public function getCategory($category)
{
$this->category->add($category)
return $this->category;
}
//...
}
```

Please note that if you want the category to be embedded as a singular
object (not an array of objects), you need to use the `singular=true` in the
annotation, otherwise it will be interpreted as a collection. Read more on
embedding collections bellow.

And the `Category` object will look like (it's a separate class):

```php
Expand Down

0 comments on commit 5f9da0d

Please sign in to comment.