lean-api/src/Form/Delete.php
2025-04-25 20:08:52 +00:00

40 lines
1.2 KiB
PHP

<?php
namespace Lean\Api\Form;
use CSSLSJ\ExamenFga\Api\{Lib};
use Picea\Ui\Method\{FormContextInterface};
use Ulmus\Entity\EntityInterface;
abstract class Delete implements \Picea\Ui\Method\FormInterface {
use FormTrait;
public function validate(FormContextInterface $context) : bool
{
if ( ! $this->entity->isLoaded() ) {
$context->pushMessage($this->message::generateError(
$this->lang('lean.api.form.delete.error.entity')
));
}
return $context->valid($this->getEntity()->isLoaded() ? $this->getEntity() : null);
}
public function execute(FormContextInterface $context) : void
{
try {
if ( $this->getEntity()::repository()->destroy($this->getEntity()) ) {
$context->pushMessage($this->message::generateSuccess(
$this->lang('lean.api.form.delete.success.save')
));
}
else {
throw new \InvalidArgumentException($this->lang('lean.api.form.delete.error.save'));
}
}
catch(\Throwable $ex) {
throw new \ErrorException($this->lang('lean.api.form.delete.error.pdo', [ 'error' => $ex->getMessage() ]));
}
}
}