The request helper can now also fetch JSON-payload attributes

This commit is contained in:
Alex Tselegidis 2021-10-29 12:54:49 +02:00
parent dd546164fb
commit 12ec5c1ac3
5 changed files with 80 additions and 46 deletions

View File

@ -26,7 +26,7 @@
* @property CI_Encryption $encryption
* @property CI_Exceptions $exceptions
* @property CI_Hooks $hooks
* @property CI_Input $input
* @property EA_Input $input
* @property CI_Lang $lang
* @property CI_Loader $load
* @property CI_Log $log

View File

@ -0,0 +1,76 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.4.0
* ---------------------------------------------------------------------------- */
/**
* Easy!Appointments input.
*
* @property CI_Benchmark $benchmark
* @property CI_Cache $cache
* @property CI_Calendar $calendar
* @property CI_Config $config
* @property CI_DB_forge $dbforge
* @property CI_DB_query_builder $db
* @property CI_DB_utility $dbutil
* @property CI_Email $email
* @property CI_Encrypt $encrypt
* @property CI_Encryption $encryption
* @property CI_Exceptions $exceptions
* @property CI_Hooks $hooks
* @property CI_Input $input
* @property CI_Lang $lang
* @property CI_Loader $load
* @property CI_Log $log
* @property CI_Migration $migration
* @property CI_Output $output
* @property CI_Profiler $profiler
* @property CI_Router $router
* @property CI_Security $security
* @property CI_Session $session
* @property CI_URI $uri
* @property CI_Upload $upload
*
* @property string $raw_input_stream
*/
class EA_Input extends CI_Input {
/**
* Fetch an item from JSON data.
*
* @param string $index Index for item to be fetched from the JSON payload.
* @param bool|false $xss_clean Whether to apply XSS filtering
*
* @return mixed
*/
public function json(string $index, bool $xss_clean = FALSE)
{
/** @var EA_Controller $CI */
$CI = &get_instance();
if ($CI->input->get_request_header('Content-Type') !== 'application/json')
{
throw new RuntimeException('Cannot get JSON attribute from non-JSON content.');
}
$input_stream = $CI->input->raw_input_stream;
if (empty($input_stream))
{
throw new RuntimeException('Cannot get JSON attribute from an empty input stream.');
}
$payload = json_decode($input_stream, TRUE);
$value = $payload[$index] ?? NULL;
return $value && $xss_clean ? $CI->security->xss_clean($value) : $value;
}
}

View File

@ -26,7 +26,7 @@
* @property CI_Encryption $encryption
* @property CI_Exceptions $exceptions
* @property CI_Hooks $hooks
* @property CI_Input $input
* @property EA_Input $input
* @property CI_Lang $lang
* @property CI_Loader $load
* @property CI_Log $log
@ -38,27 +38,6 @@
* @property CI_Session $session
* @property CI_URI $uri
* @property CI_Upload $upload
*
* @property Admins_model $admins_model
* @property Appointments_model $appointments_model
* @property Consents_model $consents_model
* @property Customers_model $customers_model
* @property Providers_model $providers_model
* @property Roles_model $roles_model
* @property Secretaries_model $secretaries_model
* @property Service_categories_model $service_categories_model
* @property Services_model $services_model
* @property Settings_model $settings_model
* @property Users_model $users_model
*
* @property Accounts $accounts
* @property Availability $availability
* @property Google_Sync $google_sync
* @property Ics_file $ics_file
* @property Instance $instance
* @property Notifications $notifications
* @property Synchronization $synchronization
* @property Timezones $timezones
*/
class EA_Migration extends CI_Migration {
//

View File

@ -26,7 +26,7 @@
* @property CI_Encryption $encryption
* @property CI_Exceptions $exceptions
* @property CI_Hooks $hooks
* @property CI_Input $input
* @property EA_Input $input
* @property CI_Lang $lang
* @property CI_Loader $load
* @property CI_Log $log
@ -38,27 +38,6 @@
* @property CI_Session $session
* @property CI_URI $uri
* @property CI_Upload $upload
*
* @property Admins_model $admins_model
* @property Appointments_model $appointments_model
* @property Consents_model $consents_model
* @property Customers_model $customers_model
* @property Providers_model $providers_model
* @property Roles_model $roles_model
* @property Secretaries_model $secretaries_model
* @property Service_categories_model $service_categories_model
* @property Services_model $services_model
* @property Settings_model $settings_model
* @property Users_model $users_model
*
* @property Accounts $accounts
* @property Availability $availability
* @property Google_Sync $google_sync
* @property Ics_file $ics_file
* @property Instance $instance
* @property Notifications $notifications
* @property Synchronization $synchronization
* @property Timezones $timezones
*/
class EA_Model extends CI_Model {
/**

View File

@ -37,7 +37,7 @@ if ( ! function_exists('request'))
throw new InvalidArgumentException('The $key argument cannot be empty.');
}
return $CI->input->post_get($key) ?? $default;
return $CI->input->post_get($key) ?? $CI->input->json($key) ?? $default;
}
}