fix unsaved
continuous-integration/drone/push Build is passing Details

issue_021_Auth
David D'ALMEIDA 1 year ago
parent 0e5e671423
commit 7c79d2656a

@ -1,4 +1,5 @@
<? <?
// should make the session start
class App { class App {
private $appName; private $appName;

@ -10,6 +10,7 @@ use Responce\{RedirectResponse, Response};
* Contôle l'intégrité des données et valid les action ( les deux doivent être vérifier) * Contôle l'intégrité des données et valid les action ( les deux doivent être vérifier)
* Ses responsabilités sont de garantir que les données de la demande sont valides et de choisir la vue à retourner. * Ses responsabilités sont de garantir que les données de la demande sont valides et de choisir la vue à retourner.
* Un Controller à la responsibilité de lire les information d'un Request(obj) et créer une {Response}. * Un Controller à la responsibilité de lire les information d'un Request(obj) et créer une {Response}.
* Un controller à toujour un default et un Null case
*/ */
abstract class BaseController{ abstract class BaseController{
@ -37,7 +38,30 @@ abstract class BaseController{
{ {
return $this->doRender($view, null, $parameters, $response, __FUNCTION__); return $this->doRender($view, null, $parameters, $response, __FUNCTION__);
} }
/**
* Renders a view.
*
* If an invalid form is found in the list of parameters, a 422 status code is returned.
* Forms found in parameters are auto-cast to form views.
*/
protected function render(string $view, array $parameters = [], Response $response = null): Response
{
$content = $this->renderView($view, $parameters);
$response ??= new Response();
if (200 === $response->getStatusCode()) {
foreach ($parameters as $v) {
if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
$response->setStatusCode(422);
break;
}
}
}
$response->setContent($content);
return $response;
}
private function doRenderView(string $view, ?string $block, array $parameters, string $method): string private function doRenderView(string $view, ?string $block, array $parameters, string $method): string
{ {
if (!$this->container->has('twig')) { if (!$this->container->has('twig')) {
@ -74,6 +98,23 @@ abstract class BaseController{
return $response; return $response;
} }
/**
* Creates and returns a Form instance from the type of the form.
*/
protected function createForm(string $type, mixed $data = null, array $options = []): FormInterface
{
return $this->container->get('form.factory')->create($type, $data, $options);
}
/**
* Creates and returns a form builder instance.
*/
protected function createFormBuilder(mixed $data = null, array $options = []): FormBuilderInterface
{
return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options);
}
abstract public function index(); abstract public function index();
public function __call($method, $parameters) public function __call($method, $parameters)
@ -88,9 +129,8 @@ abstract class BaseController{
* *
* @param string $method * @param string $method
* @param array $parameters * @param array $parameters
* @return \Symfony\Component\HttpFoundation\Response
*/ */
public function callAction($method, $parameters) public function callAction($method, $parameters): Response
{ {
return $this->{$method}(...array_values($parameters)); return $this->{$method}(...array_values($parameters));
} }

@ -0,0 +1,74 @@
<?php
abstract class AbstractField implements IField {
protected $name;
protected $value;
protected $validators = [];
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
// Autres méthodes...
}
// abstract class AbstractField {
// protected $name;
// protected $value;
// protected $attributes;
// protected $errors = [];
// protected $validators = [];
// public function __construct($name, $value = '', $attributes = []) {
// $this->name = $name;
// $this->value = $value;
// $this->attributes = $attributes;
// }
// public function getName() {
// return $this->name;
// }
// public function getValue() {
// return $this->value;
// }
// public function setValue($value) {
// $this->value = $value;
// }
// public function addValidator(callable $validator) {
// $this->validators[] = $validator;
// return $this;
// }
// public function isValid() {
// foreach ($this->validators as $validator) {
// if (!$validator($this->value)) {
// return false;
// }
// }
// return true;
// }
// public function getErrors() {
// return $this->errors;
// }
// public function addError($error) {
// $this->errors[] = $error;
// }
// public function getAttributesAsString() {
// $attributesStr = '';
// foreach ($this->attributes as $key => $value) {
// $attributesStr .= " $key=\"$value\"";
// }
// return $attributesStr;
// }
// abstract public function render();
// }

@ -0,0 +1,29 @@
<?php
abstract class AbstractType implements FormTypeInterface
{
protected $fields = [];
/**
* @return void
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
}
/**
* @return void
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
}
/**
* @return void
*/
public function finishView(FormView $view, FormInterface $form, array $options)
{
}
}

@ -0,0 +1,9 @@
<?php
class FieldFactory {
public static function createField($type, $name) {
switch($type) {
case 'text': return new TextField($name);
// Autres cas...
}
}
}

@ -0,0 +1,18 @@
<?php
class FormBuilder {
private $form;
public function __construct() {
$this->form = new BasicForm();
}
public function addText($name, $options) {
$this->form->add(FieldFactory::createField('text', $options));
return $this;
}
public function build() {
return $this->form;
}
}

@ -0,0 +1,8 @@
<?php
interface FormInterface {
public function add(FieldInterface $field);
public function remove($fieldName);
public function submit($requestData);
public function isValid();
}

@ -0,0 +1,9 @@
<!-- rendering HTML form fields, validating submitted data, mapping the form data into objects and a lot more -->
<?php
class Login extends AbstractForm {
// Implémentation spécifique de BasicForm
}
?>

@ -0,0 +1,11 @@
<?php
class TextField extends AbstractField {
public function render() {
return "<input type='text' name='{$this->name}' value='{$this->value}' />";
}
public function validate($value) {
// Logique de validation
}
}
Loading…
Cancel
Save