Request.php (1946B)
1 <?php 2 3 /** 4 * This is under development. Expect changes! 5 * Class Request 6 * Abstracts the access to $_GET, $_POST and $_COOKIE, preventing direct access to these super-globals. 7 * This makes PHP code quality analyzer tools very happy. 8 * @see http://php.net/manual/en/reserved.variables.request.php 9 */ 10 class Request 11 { 12 /** 13 * Gets/returns the value of a specific key of the POST super-global. 14 * When using just Request::post('x') it will return the raw and untouched $_POST['x'], when using it like 15 * Request::post('x', true) then it will return a trimmed and stripped $_POST['x'] ! 16 * 17 * @param mixed $key key 18 * @param bool $clean marker for optional cleaning of the var 19 * @return mixed the key's value or nothing 20 */ 21 public static function post($key, $clean = false) 22 { 23 if (isset($_POST[$key])) { 24 // we use the Ternary Operator here which saves the if/else block 25 // @see http://davidwalsh.name/php-shorthand-if-else-ternary-operators 26 return ($clean) ? trim(strip_tags($_POST[$key])) : $_POST[$key]; 27 } 28 } 29 30 /** 31 * Returns the state of a checkbox. 32 * 33 * @param mixed $key key 34 * @return mixed state of the checkbox 35 */ 36 public static function postCheckbox($key) 37 { 38 return isset($_POST[$key]) ? 1 : NULL; 39 } 40 41 /** 42 * gets/returns the value of a specific key of the GET super-global 43 * @param mixed $key key 44 * @return mixed the key's value or nothing 45 */ 46 public static function get($key) 47 { 48 if (isset($_GET[$key])) { 49 return $_GET[$key]; 50 } 51 } 52 53 /** 54 * gets/returns the value of a specific key of the COOKIE super-global 55 * @param mixed $key key 56 * @return mixed the key's value or nothing 57 */ 58 public static function cookie($key) 59 { 60 if (isset($_COOKIE[$key])) { 61 return $_COOKIE[$key]; 62 } 63 } 64 }