-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace MathieuTu\JsonImport\Contracts; | ||
|
||
interface JsonImportable | ||
{ | ||
public static function importFromJson($objectsToCreate); | ||
|
||
public static function getJsonImportableRelations(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace MathieuTu\JsonImport\Exceptions; | ||
|
||
class JsonDecodingException extends \RuntimeException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace MathieuTu\JsonImport\Traits; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\HasOneOrMany; | ||
use MathieuTu\JsonImport\Contracts\JsonImportable; | ||
use MathieuTu\JsonImport\Exceptions\JsonDecodingException; | ||
|
||
trait JsonImporter | ||
{ | ||
protected $jsonImportableRelations = []; | ||
|
||
public function importFromJson($objects) | ||
{ | ||
$objects = $this->convertObjectsToArray($objects); | ||
|
||
foreach ($objects as $attributes) { | ||
$object = $this->importAttributes($attributes); | ||
|
||
$this->importRelations($object, $attributes); | ||
} | ||
} | ||
|
||
protected function convertObjectsToArray($objects): mixed | ||
{ | ||
if (is_string($objects)) { | ||
$objects = json_decode($objects, true); | ||
} | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
throw new JsonDecodingException('Invalid json format.'); | ||
} | ||
return $objects; | ||
} | ||
|
||
protected function importAttributes($attributes): Model | ||
{ | ||
return $this instanceof Model ? $object = $this->create($attributes) : $this; | ||
} | ||
|
||
protected function importRelations($object, $attributes) | ||
{ | ||
foreach ($this->getJsonImportableRelations($object) as $relationName) { | ||
$this->importChildrenIfImportable($object->$relationName, $attributes[$relationName]); | ||
} | ||
} | ||
|
||
public function getJsonImportableRelations($object): array | ||
{ | ||
return $this->jsonImportableRelations | ||
?? $this->jsonExportableRelations | ||
?? array_diff(array_keys($object), $this->getFillable()); | ||
} | ||
|
||
abstract public function getFillable(): array; | ||
|
||
protected function importChildrenIfImportable(HasOneOrMany $relation, array $children) | ||
{ | ||
if (!$relation instanceof HasOneOrMany) { | ||
return; | ||
} | ||
|
||
$childClass = $relation->getRelated(); | ||
|
||
if ($childClass instanceof JsonImportable) { | ||
$children = $this->addParentKeyToChildren($children, $relation); | ||
|
||
$childClass->importFromJson($children); | ||
} | ||
} | ||
|
||
protected function addParentKeyToChildren(array $children, HasOneOrMany $relation): array | ||
{ | ||
foreach ($children as $object) { | ||
$object[$relation->getForeignKeyName()] = $relation->getParentKey(); | ||
} | ||
|
||
return $children; | ||
} | ||
} |