Skip to content

Instantly share code, notes, and snippets.

@szepeviktor
Last active July 11, 2020 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szepeviktor/62b75e3b43a4eb8680408e52fd2a1cba to your computer and use it in GitHub Desktop.
Save szepeviktor/62b75e3b43a4eb8680408e52fd2a1cba to your computer and use it in GitHub Desktop.
Csodálatos Input
<?php
// ezzel kéne működjön
// https://symfony.com/doc/current/components/http_foundation.html#accessing-request-data
// https://github.com/symfony/symfony/blob/5.1/src/Symfony/Component/HttpFoundation/ParameterBag.php
namespace Csodálatos
class Input
{
protected $post;
protected $variables;
public function __construct(array $post)
{
$this->post = $post;
$this->variables = [];
// Ezek kell érkezzenek
$this->szabályok = [
'mezőneve' => [
'type' => 'int',
'default' => 'min',
'min' => 0,
'max' => 1.5e6,
]
];
}
public function getStrict(string $varName)
{
if (!array_key_exists($varName, $this->szabályok)) {
throw new \Exception('Nincs ilyen :(');
}
// null lesz, ha nem érkezett ilyen a $_POST-ban
return $this->variables[$varName];
}
public function get(string $varName)
{
$this->getStrict($varName) ?? $this->szabályok[$varName]['default'];
}
}
@szepeviktor
Copy link
Author

@Jony-Shark A nemlétező szabadidődben beledolgozhatsz.

@szepeviktor
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment