Skip to content

Commit

Permalink
see: pluf/lm#5
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafabarmshory committed Feb 20, 2021
1 parent d614967 commit bfb22b6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/Process/Entity/ReadEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ class ReadEntity

private string $class;

private string $name = "itemId";

public function __construct(string $class, string $name = "itemId")
public function __construct(string $class)
{
$this->class = $class;
$this->name = $name;
}

/**
Expand Down
57 changes: 57 additions & 0 deletions src/Process/Entity/UpdateEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace Pluf\Core\Process\Entity;

use Pluf\Core\Exception;
use Pluf\Orm\AssertionTrait;
use Pluf\Orm\EntityManager;
use Throwable;
use Pluf\Orm\ModelDescriptionRepository;

class UpdateEntity
{
use AssertionTrait;

private string $class;


public function __construct(string $class)
{
$this->class = $class;
}

public function __invoke(EntityManager $entityManager, ModelDescriptionRepository $modelDescriptionRepository, $entity, $itemId)
{
$md = $modelDescriptionRepository->get($this->class);
$id = $md->properties[$md->primaryKey];

$oldentity = $entityManager->find($this->class, $itemId);
$this->assertTrue($oldentity, 'Entity type {{type}} with ID {{itemId} not found', [
'type' => $this->class,
'itemId' => $itemId
]);

$idValue = $id->getValue($oldentity);
$id->setValue($entity, $idValue);
$entity = $entityManager->merge​($entity);

return $entity;
}



/**
* Creates new exception
*
* @param string $message
* @param int $code
* @param Throwable $previous
* @param array $params
* @param array $solutions
* @return Throwable
*/
protected function generateException($message = '', ?int $code = null, ?Throwable $previous = null, ?array $params = [], ?array $solutions = []): Throwable
{
return new Exception($message, $code, $previous, 404, $params, $solutions);
}
}

21 changes: 21 additions & 0 deletions src/Process/HttpBodyToEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Pluf\Core\Process;

use Pluf\Orm\AssertionTrait;

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);
}
}

0 comments on commit bfb22b6

Please sign in to comment.