dimaslanjaka /
universal-framework
| 1 | <?php |
||
| 2 | const POST_DEFAULT_FLAG = 0; |
||
| 3 | const POST_NOT_EMPTY = 1; |
||
| 4 | const POST_NOT_NULL = 2; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Post getter. |
||
| 8 | * Get post data with fallback value |
||
| 9 | * |
||
| 10 | * @param string $name |
||
| 11 | * @param string $fallback |
||
| 12 | * @param int $flag flag rule for post getter |
||
| 13 | * @return void |
||
| 14 | */ |
||
| 15 | function getPost(string $name, string $fallback = null, int $flag = POST_DEFAULT_FLAG) |
||
| 16 | { |
||
| 17 | if (isPost()) { |
||
| 18 | if (isset($_POST[$name])) { |
||
| 19 | $thePost = $_POST[$name]; |
||
| 20 | if ($flag != POST_DEFAULT_FLAG) { |
||
| 21 | if ($flag == POST_NOT_NULL) { |
||
| 22 | if ($thePost != null) return $thePost; |
||
| 23 | } else if ($flag == POST_NOT_EMPTY) { |
||
| 24 | if (!empty($thePost)) return $thePost; |
||
| 25 | } |
||
| 26 | } else { |
||
| 27 | return $thePost; |
||
| 28 | } |
||
| 29 | } |
||
| 30 | } |
||
| 31 | return $fallback; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Check if request method is post |
||
| 36 | * |
||
| 37 | * @return boolean |
||
| 38 | */ |
||
| 39 | function isPost() |
||
| 40 | { |
||
| 41 | return isRequest('post'); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Check request method |
||
| 46 | * |
||
| 47 | * @param string $methodName |
||
| 48 | * @return boolean |
||
| 49 | */ |
||
| 50 | function isRequest(string $methodName) |
||
| 51 | { |
||
| 52 | return $_SERVER['REQUEST_METHOD'] == strtoupper($methodName); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get request value by name ($_REQUEST) |
||
| 57 | */ |
||
| 58 | function getRequest(string $requestName) |
||
| 59 | { |
||
| 60 | if (isset($_REQUEST[$requestName])) { |
||
| 61 | return $_REQUEST[$requestName]; |
||
| 62 | } |
||
| 63 | return null; |
||
| 64 | } |
||
| 65 |