lean-api/src/Form/Form.php
2026-05-21 14:58:21 +00:00

49 lines
1.4 KiB
PHP

<?php
namespace Lean\Api\Form;
use Lean\Api\Factory\MessageFactoryInterface;
use Lean\LanguageHandler;
use Picea\Ui\Method\FormContextInterface;
use Ulmus\Entity\EntityInterface;
use Ulmus\EntityCollection;
abstract class Form
{
protected string $contextClass;
public function __construct(
protected LanguageHandler $languageHandler,
protected MessageFactoryInterface $message,
# public EntityInterface $entity,
) {}
public function validate(FormContextInterface $context) : bool
{
return $context->valid($this->getEntity()->isLoaded() ? $this->getEntity() : null);
}
public function initialize(FormContextInterface $context) : void
{
if(isset($this->contextClass) && ! $context instanceof $this->contextClass ) {
throw new \LogicException(
sprintf("Your context type should be a %s, received %s", $this->contextClass, $context::class)
);
}
}
public function lang(string $key, array $variables = [])
{
return $this->languageHandler->languageFromKey($key, $variables);
}
public function getEntity() : EntityInterface|EntityCollection
{
if (! isset($this->entity)) {
throw new \InvalidArgumentException($this->lang("lean.api.form.error.entity"));
}
return $this->entity;
}
}