Mongolid\DataMapper\EntityAssembler::assemble PHP Method

assemble() public method

Builds an object from the provided data.
public assemble ( array | object $document, Schema $schema ) : mixed
$document array | object The attributes that will be used to compose the entity.
$schema Mongolid\Schema\Schema Schema that will be used to map each field.
return mixed
    public function assemble($document, Schema $schema)
    {
        $entityClass = $schema->entityClass;
        $model = Ioc::make($entityClass);
        foreach ($document as $field => $value) {
            $fieldType = $schema->fields[$field] ?? null;
            if ($fieldType && substr($fieldType, 0, 7) == 'schema.') {
                $value = $this->assembleDocumentsRecursively($value, substr($fieldType, 7));
            }
            $model->{$field} = $value;
        }
        $entity = $this->morphingTime($model);
        return $this->prepareOriginalAttributes($entity);
    }

Usage Example

 /**
  * @dataProvider EntityAssemblerFixture
  */
 public function testShouldAssembleEntityForTheGivenSchema($inputValue, $availableSchemas, $inputSchema, $expectedOutput)
 {
     // Arrange
     $entityAssembler = new EntityAssembler();
     $schemas = [];
     foreach ($availableSchemas as $key => $value) {
         $schemas[$key] = m::mock(Schema::class . '[]');
         $schemas[$key]->entityClass = $value['entityClass'];
         $schemas[$key]->fields = $value['fields'];
     }
     // Act
     foreach ($schemas as $className => $instance) {
         Ioc::instance($className, $instance);
     }
     // Assert
     $result = $entityAssembler->assemble($inputValue, $schemas[$inputSchema]);
     $this->assertEquals($expectedOutput, $result);
 }