Last active
July 11, 2020 08:50
-
-
Save szepeviktor/62b75e3b43a4eb8680408e52fd2a1cba to your computer and use it in GitHub Desktop.
Csodálatos Input
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
<?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']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Jony-Shark A nemlétező szabadidődben beledolgozhatsz.