-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
d614967
commit bfb22b6
Showing
3 changed files
with
79 additions
and
4 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
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,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); | ||
} | ||
} | ||
|
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,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); | ||
} | ||
} | ||
|