| @@ 10-70 (lines=61) @@ | ||
| 7 | /** |
|
| 8 | * Check/assign from superglobal $_GET |
|
| 9 | */ |
|
| 10 | trait Get |
|
| 11 | { |
|
| 12 | /** |
|
| 13 | * Check $_GET |
|
| 14 | * |
|
| 15 | * Example: |
|
| 16 | * ```php |
|
| 17 | * if(chkGet("s") == "a") instead of if(isset($_GET["s"]) && $_GET["s"] == "a") |
|
| 18 | * ``` |
|
| 19 | * |
|
| 20 | * @param string $getKey Get-key. |
|
| 21 | * @return bool|string |
|
| 22 | */ |
|
| 23 | public static function chkGet($getKey) |
|
| 24 | { |
|
| 25 | return filter_input(INPUT_GET, $getKey); |
|
| 26 | } |
|
| 27 | ||
| 28 | /** |
|
| 29 | * Assign value from $_GET |
|
| 30 | * |
|
| 31 | * Example: |
|
| 32 | * ```php |
|
| 33 | * $var = assignFromGet("a") instead of $var = ((!empty($_GET["s"])) ? $_GET["s"] : ""); |
|
| 34 | * ``` |
|
| 35 | * |
|
| 36 | * @param string $getKey Get-key. |
|
| 37 | * @return string |
|
| 38 | */ |
|
| 39 | public static function assignFromGet($getKey) |
|
| 40 | { |
|
| 41 | return (string)filter_input(INPUT_GET, $getKey); |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * Check if multiple $_GET-keys are not empty |
|
| 46 | * |
|
| 47 | * Example: |
|
| 48 | * ```php |
|
| 49 | * if(chkGetAll(array("a","b"))) instead of if(!empty($_GET["a"]) && !empty($_GET["b"])) |
|
| 50 | * ``` |
|
| 51 | * |
|
| 52 | * @param array $keys Get-keys. |
|
| 53 | * @return bool |
|
| 54 | */ |
|
| 55 | public static function chkGetAll(...$keys) |
|
| 56 | { |
|
| 57 | // If first value is array, then create array from first value |
|
| 58 | if ((array)$keys[0] === $keys[0]) { |
|
| 59 | $keys = $keys[0]; |
|
| 60 | } |
|
| 61 | ||
| 62 | foreach ($keys as $key) { |
|
| 63 | $val = filter_input(INPUT_GET, $key); |
|
| 64 | if (empty($val)) { |
|
| 65 | return false; |
|
| 66 | } |
|
| 67 | } |
|
| 68 | return true; |
|
| 69 | } |
|
| 70 | } |
|
| 71 | ||
| @@ 10-70 (lines=61) @@ | ||
| 7 | /** |
|
| 8 | * Check/assign from superglobal $_POST |
|
| 9 | */ |
|
| 10 | trait Post |
|
| 11 | { |
|
| 12 | /** |
|
| 13 | * Check $_POST |
|
| 14 | * |
|
| 15 | * Example: |
|
| 16 | * ```php |
|
| 17 | * if(chkPost("s") == "a") instead of if(isset($_POST["s"]) && $_POST["s"] == "a") |
|
| 18 | * ``` |
|
| 19 | * |
|
| 20 | * @param string $postKey Post-key. |
|
| 21 | * @return bool|string |
|
| 22 | */ |
|
| 23 | public static function chkPost($postKey) |
|
| 24 | { |
|
| 25 | return filter_input(INPUT_POST, $postKey); |
|
| 26 | } |
|
| 27 | ||
| 28 | /** |
|
| 29 | * Assign value from $_POST |
|
| 30 | * |
|
| 31 | * Example: |
|
| 32 | * ```php |
|
| 33 | * $var = assignFromPost("a") instead of $var = ((!empty($_POST["s"])) ? $_POST["s"] : ""); |
|
| 34 | * ``` |
|
| 35 | * |
|
| 36 | * @param string $postKey Post-key. |
|
| 37 | * @return string |
|
| 38 | */ |
|
| 39 | public static function assignFromPost($postKey) |
|
| 40 | { |
|
| 41 | return (string)filter_input(INPUT_POST, $postKey); |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * Check if multiple $_POST-keys are not empty |
|
| 46 | * |
|
| 47 | * Example: |
|
| 48 | * ```php |
|
| 49 | * if(chkPostAll(array("a","b"))) instead of if(!empty($_POST["a"]) && !empty($_POST["b"])) |
|
| 50 | * ``` |
|
| 51 | * |
|
| 52 | * @param array $keys Post-keys. |
|
| 53 | * @return bool |
|
| 54 | */ |
|
| 55 | public static function chkPostAll(...$keys) |
|
| 56 | { |
|
| 57 | // If first value is array, then create array from first value |
|
| 58 | if ((array)$keys[0] === $keys[0]) { |
|
| 59 | $keys = $keys[0]; |
|
| 60 | } |
|
| 61 | ||
| 62 | foreach ($keys as $key) { |
|
| 63 | $val = filter_input(INPUT_POST, $key); |
|
| 64 | if (empty($val)) { |
|
| 65 | return false; |
|
| 66 | } |
|
| 67 | } |
|
| 68 | return true; |
|
| 69 | } |
|
| 70 | } |
|
| 71 | ||