/** * Convert an instance to an eloquent model object. * * @param string $lastObjectId * @param \ProAI\Datamapper\Eloquent\Model $lastEloquentModel * @return \ProAI\Datamapper\Eloquent\Model */ public function toEloquentObject($lastObjectId, $lastEloquentModel) { $class = get_mapped_model(static::class); $eloquentModel = new $class(); // get model data $dict = ['mapping' => $eloquentModel->getMapping(), 'attributes' => $eloquentModel->getAttributes(), 'relations' => $eloquentModel->getRelations()]; // attributes foreach ($dict['mapping']['attributes'] as $attribute => $column) { if (!$eloquentModel->isAutomaticallyUpdatedDate($column)) { $eloquentModel->setAttribute($column, $this->{$attribute}); } } // embeddeds foreach ($dict['mapping']['embeddeds'] as $name => $embedded) { $embeddedObject = $this->{$name}; if (!empty($embeddedObject)) { $embeddedObject->toEloquentObject($eloquentModel, $name); } } // relations foreach ($dict['mapping']['relations'] as $name => $relation) { $relationObject = $this->{$name}; if (!empty($relationObject) && !$relationObject instanceof \ProAI\Datamapper\Contracts\Proxy) { // set relation if ($relationObject instanceof \ProAI\Datamapper\Support\Collection) { $value = EloquentCollection::newFromDatamapperObject($relationObject, $this, $eloquentModel); } elseif (spl_object_hash($relationObject) == $lastObjectId) { $value = $lastEloquentModel; } else { $value = EloquentModel::newFromDatamapperObject($relationObject, spl_object_hash($this), $eloquentModel); } $eloquentModel->setRelation($name, $value); } } return $eloquentModel; }
/** * Convert model to plain old php object. * * @param \ProAI\Datamapper\Contracts\Entity $entity * @param string $lastObjectId * @param \ProAI\Datamapper\Eloquent\Model $lastEloquentModel * @return \ProAI\Datamapper\Eloquent\Model */ public static function newFromDatamapperObject(EntityContract $entity, $lastObjectId = null, $lastEloquentModel = null) { // directly get private properties if entity extends the datamapper entity class (fast!) if ($entity instanceof \ProAI\Datamapper\Support\Entity) { return $entity->toEloquentObject($lastObjectId, $lastEloquentModel); } // get private properties via reflection (slow!) $class = get_mapped_model(get_class($entity)); $eloquentModel = new $class(); $reflectionObject = new ReflectionObject($entity); $mapping = $eloquentModel->getMapping(); // attributes foreach ($mapping['attributes'] as $attribute => $column) { if (!$eloquentModel->isAutomaticallyUpdatedDate($column)) { // get property $property = $eloquentModel->getProperty($reflectionObject, $entity, $attribute); // set attribute $eloquentModel->setAttribute($column, $property); } } // embeddeds foreach ($mapping['embeddeds'] as $name => $embedded) { $embeddedObject = $eloquentModel->getProperty($reflectionObject, $entity, $name); if (!empty($embeddedObject)) { $embeddedReflectionObject = new ReflectionObject($embeddedObject); foreach ($embedded['attributes'] as $attribute => $column) { // get property $property = $eloquentModel->getProperty($embeddedReflectionObject, $embeddedObject, $attribute); // set attribute $eloquentModel->setAttribute($column, $property); } } } // relations foreach ($mapping['relations'] as $name => $relation) { $relationObject = $eloquentModel->getProperty($reflectionObject, $entity, $name); if (!empty($relationObject) && !$relationObject instanceof \ProAI\Datamapper\Contracts\Proxy) { // set relation if ($relationObject instanceof \ProAI\Datamapper\Support\Collection) { $value = EloquentCollection::newFromDatamapperObject($relationObject, $this, $eloquentModel); } elseif (spl_object_hash($relationObject) == $lastObjectId) { $value = $lastEloquentModel; } else { $value = EloquentModel::newFromDatamapperObject($relationObject, spl_object_hash($this), $eloquentModel); } $eloquentModel->setRelation($name, $value); } } return $eloquentModel; }
/** * Get a new schema query instance. * * @param string $class * @return \ProAI\Datamapper\Eloquent\GraphBuilder */ public function newGraphQuery($class) { $class = get_real_entity($class); $eloquentModel = get_mapped_model($class); return (new $eloquentModel())->newGraphQuery(); }
/** * Replace relations. * * @param array $relations * @param string $stub * @return void */ protected function replaceRelations($relations, &$stub) { $textRelations = []; foreach ($relations as $key => $relation) { $relationStub = $this->stubs['relation']; // generate options array $options = []; if ($relation['type'] != 'morphTo') { $options[] = "'" . get_mapped_model($relation['relatedEntity'], false) . "'"; } foreach ($relation['options'] as $name => $option) { if ($option === null) { $options[] = 'null'; } elseif ($option === true) { $options[] = 'true'; } elseif ($option === false) { $options[] = 'false'; } else { if ($name == 'throughEntity') { $options[] = "'" . get_mapped_model($option, false) . "'"; } elseif ($name != 'morphableClasses') { $options[] = "'" . $option . "'"; } } } $options = implode(", ", $options); $relationStub = str_replace('{{name}}', $relation['name'], $relationStub); $relationStub = str_replace('{{options}}', $options, $relationStub); $relationStub = str_replace('{{ucfirst_type}}', ucfirst($relation['type']), $relationStub); $relationStub = str_replace('{{type}}', $relation['type'], $relationStub); $textRelations[] = $relationStub; if ($relation['type'] == 'morphTo' || $relation['type'] == 'morphToMany' && !$relation['options']['inverse']) { $morphStub = $this->stubs['morph_extension']; $morphableClasses = []; foreach ($relation['options']['morphableClasses'] as $key => $name) { $morphableClasses[$key] = get_mapped_model($name, false); } $morphStub = str_replace('{{name}}', $relation['name'], $morphStub); $morphStub = str_replace('{{morphName}}', ucfirst($relation['options']['morphName']), $morphStub); $morphStub = str_replace('{{types}}', $this->getArrayAsText($morphableClasses, 2), $morphStub); $textRelations[] = $morphStub; } } $stub = str_replace('{{relations}}', implode(PHP_EOL . PHP_EOL, $textRelations), $stub); }
/** * Convert model to plain old php object. * * @param \ProAI\Datamapper\Contracts\Entity $entity * @return \ProAI\Datamapper\Eloquent\Model */ public static function newFromEntity(EntityContract $entity) { // directly get private properties if entity extends the datamapper entity class (fast!) if ($entity instanceof \ProAI\Datamapper\Support\Entity) { return $entity->toEloquentModel(); } // get private properties via reflection (slow!) $class = get_mapped_model(get_class($entity)); $eloquentModel = new $class(); $reflectionObject = new ReflectionObject($entity); $mapping = $eloquentModel->getMapping(); // attributes foreach ($mapping['attributes'] as $attribute => $column) { if (!$eloquentModel->isGeneratedDate($column)) { $eloquentModel->setAttribute($column, $eloquentModel->getProperty($reflectionObject, $entity, $attribute)); } } // embeddeds foreach ($mapping['embeddeds'] as $name => $embedded) { $embeddedObject = $eloquentModel->getProperty($reflectionObject, $entity, $name); if (!empty($embeddedObject)) { $embeddedReflectionObject = new ReflectionObject($embeddedObject); foreach ($embedded['attributes'] as $attribute => $column) { $eloquentModel->setAttribute($column, $eloquentModel->getProperty($embeddedReflectionObject, $embeddedObject, $attribute)); } } } // relations foreach ($mapping['relations'] as $name => $relation) { $relationObject = $eloquentModel->getProperty($reflectionObject, $entity, $name); if (!empty($relationObject) && !$relationObject instanceof \ProAI\Datamapper\Contracts\Proxy) { $value = $relationObject instanceof \ProAI\Datamapper\Support\Collection ? Collection::newFromEntity($relationObject) : self::newFromEntity($relationObject); $eloquentModel->setRelation($name, $value); } } return $eloquentModel; }