Skip to content

Commit

Permalink
bug fixed: map by objectMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafabarmshory committed Mar 8, 2021
1 parent bfb22b6 commit 7ba242e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Process/Entity/CreateEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function __invoke(EntityManager $entityManager, $entities): array
];
}
$resultList = [];
foreach ($entities as $customer) {
$item = $entityManager->persist​($customer);
foreach ($entities as $entity) {
$item = $entityManager->persist​($entity);
$resultList[] = $item;
}
return $resultList;
Expand Down
38 changes: 26 additions & 12 deletions src/Process/HttpBodyToEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ class HttpBodyToEntities
{
use AssertionTrait;

private string $class;
private bool $multi;
private string $name = "entities";
protected string $class;

protected bool $multi;

protected string $name = "entities";

public function __construct(string $class, bool $multi = true, string $name = "entities")
{
Expand All @@ -22,12 +24,11 @@ public function __construct(string $class, bool $multi = true, string $name = "e
$this->name = $name;
}

public function __invoke(
ModelDescriptionRepository $modelDescriptionRepository,
ServerRequestInterface $request,
UnitTrackerInterface $unitTracker)
public function __invoke(ModelDescriptionRepository $modelDescriptionRepository, ServerRequestInterface $request, UnitTrackerInterface $unitTracker)
{
$this->assertEquals("POST", $request->getMethod(), "Unsupported method {{method}}", ["method" => $request->getMethod()]);
$this->assertEquals("POST", $request->getMethod(), "Unsupported method {{method}}", [
"method" => $request->getMethod()
]);

$type = $this->getContentType($request);
$this->assertNotEmpty($type, "Content type is not specified.");
Expand All @@ -41,15 +42,28 @@ public function __invoke(
break;
}

$res = [];
$res[$this->name] = $this->generateEntities($type, $modelDescriptionRepository, $data);
return $unitTracker->next($res);
}

/**
* Converts data based on type and model description
*
* @param unknown $type
* @param unknown $modelDescriptionRepository
* @param unknown $data
* @return unknown
*/
public function generateEntities($type, $modelDescriptionRepository, $data)
{
$builder = new ObjectMapperBuilder();
$objectMapper = $builder->addType($type)
->setModelDescriptionRepository($modelDescriptionRepository)
->supportList($this->multi)
->build();

$res = [];
$res[$this->name] = $objectMapper->readValue($data, $this->class, $this->multi);
return $unitTracker->next($res);

return $objectMapper->readValue($data, $this->class, $this->multi);
}

public function getContentType(ServerRequestInterface $request): string
Expand Down
6 changes: 0 additions & 6 deletions src/Process/HttpBodyToEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ class HttpBodyToEntity extends HttpBodyToEntities
{
use AssertionTrait;

private string $class;

private bool $multi;

private string $name = "entities";

public function __construct(string $class, string $name = "entity")
{
parent::__construct($class, false, $name);
Expand Down

0 comments on commit 7ba242e

Please sign in to comment.