| 1 |
<?php |
| 2 |
/** |
| 3 |
* Request class. |
| 4 |
* |
| 5 |
* @package Calotes\Component |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Calotes\Component; |
| 9 |
|
| 10 |
use Calotes\Base\Model; |
| 11 |
|
| 12 |
/** |
| 13 |
* Parse and validate request data. |
| 14 |
*/ |
| 15 |
class Request { |
| 16 |
|
| 17 |
/** |
| 18 |
* Store the data from request. |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
protected $data = array(); |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor to initialize the object with data from the POST or GET request. |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
if ( 'POST' === defender_get_data_from_request( 'REQUEST_METHOD', 's' ) ) { |
| 29 |
$this->data = defender_get_data_from_request( 'data', 'p' ); |
| 30 |
} else { |
| 31 |
$this->data = defender_get_data_from_request( 'data', 'g' ); |
| 32 |
} |
| 33 |
// defender_get_data_from_request( 'data', … ) JSON-decodes; empty/malformed yields null. Central assigns get_data() to $_POST — must stay array (PHP 8+). |
| 34 |
if ( ! is_array( $this->data ) ) { |
| 35 |
$this->data = array(); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Retrieve the data that will be in use, it's recommended that $filters should be provided for data validation and |
| 41 |
* cast. |
| 42 |
* |
| 43 |
* @param array $filters The filters to apply. |
| 44 |
* |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
public function get_data( $filters = array() ) { |
| 48 |
if ( ! is_array( $filters ) || array() === $filters ) { |
| 49 |
return $this->data; |
| 50 |
} |
| 51 |
$data = array(); |
| 52 |
foreach ( $filters as $key => $rule ) { |
| 53 |
if ( ! isset( $this->data[ $key ] ) ) { |
| 54 |
continue; // Moving on. |
| 55 |
} |
| 56 |
// Mandatory. |
| 57 |
$type = $rule['type']; |
| 58 |
$sanitize = $rule['sanitize'] ?? null; |
| 59 |
|
| 60 |
$value = $this->data[ $key ]; |
| 61 |
// Cast. |
| 62 |
settype( $value, $type ); |
| 63 |
if ( ! is_array( $sanitize ) ) { |
| 64 |
$sanitize = array( $sanitize ); |
| 65 |
} |
| 66 |
foreach ( $sanitize as $function ) { |
| 67 |
if ( null !== $function && function_exists( $function ) ) { |
| 68 |
if ( is_array( $value ) ) { |
| 69 |
$value = $this->sanitize_array( $value, $function ); |
| 70 |
} else { |
| 71 |
$value = $function( $value ); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
$data[ $key ] = $value; |
| 76 |
} |
| 77 |
|
| 78 |
return $data; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Sanitize an array recursively. |
| 83 |
* |
| 84 |
* @param array $arr The array to sanitize. |
| 85 |
* @param mixed $sanitize The sanitization function to apply. |
| 86 |
* |
| 87 |
* @return array The sanitized array. |
| 88 |
*/ |
| 89 |
protected function sanitize_array( $arr, $sanitize ) { |
| 90 |
foreach ( $arr as &$value ) { |
| 91 |
if ( is_array( $value ) ) { |
| 92 |
$value = $this->sanitize_array( $value, $sanitize ); |
| 93 |
} else { |
| 94 |
$value = $sanitize( $value ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return $arr; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get the data from _REQUEST. |
| 103 |
* |
| 104 |
* @param Model $model The model to get data from. |
| 105 |
* |
| 106 |
* @return array |
| 107 |
*/ |
| 108 |
public function get_data_by_model( Model $model ) { |
| 109 |
return $this->get_data( $model->annotations ); |
| 110 |
} |
| 111 |
} |
| 112 |
|