Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] refactor http input to trigger each specific action, from GET, POST, PUT and FILES #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controller/indexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function home()
*/
function changeLang()
{
Session::put('lang', Input::get("lang"));
Session::put('lang', Input::post("lang"));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this example could be on another commit of example

Redirect::to("/");
}
}
7 changes: 4 additions & 3 deletions middleware/Validation/exampleValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function changeLang()
$rules = [
"token" => $this->schema->string("token")
->min(5)
->max(30)
->max(50)
->required(),
"lang" => $this->schema->string("lang")
->min(1)
Expand All @@ -21,8 +21,9 @@ function changeLang()
];

$this->validate->check($_POST, $rules);
if (!$this->validate->passed()) {
Application::dumper($this->validate->errors());
if (! $this->validate->passed()) {
print_r($this->validate->errors());
exit();
}
}
}
63 changes: 42 additions & 21 deletions src/Core/Http/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
class Input
{
/**
* check if the submission form is a port action
* @param $type
* @return bool
*/
public static function exists($type = "POST")
public static function exists($type = "POST"): bool
{
switch ($type) {
case "POST":
Expand Down Expand Up @@ -41,51 +42,46 @@ private static function put(): array
}

/**
* extract data from submit form as form data
* @param $file_input
* @return array|string[]
* @return array
*/
private static function extractFromFormData($file_input)
private static function extractFromFormData($file_input): array
{
$fragma = [];
$form_data_fragmentation = [];
$explode = explode("\r", implode("\r", explode("\n", $file_input)));
$len_Arr = count($explode);
for ($i = 1; $i < $len_Arr; $i++) {
if (!strchr($explode[$i], "----------------------------")) {
if (strlen($explode[$i]) > 0) {
$replaced = str_replace("Content-Disposition: form-data; name=", "", $explode[$i]);
array_push($fragma, $replaced);
$form_data_fragmentation[] = $replaced;
}
}
}
$len_object = count($fragma);
$len_object = count($form_data_fragmentation);
Comment on lines +51 to +62
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Load data from form-data should be revised, refactoring and validate while load form-data submission.
The process is not clean and testable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right

$object = [];
for ($j = 0; $j < $len_object; $j++) {
if ($j == 0 || ($j + 1) % 2 != 0) {
$key = str_replace("\"", "", $fragma[$j]);
$object = array_merge($object, [$key => trim($fragma[($j + 1)])]);
$key = str_replace("\"", "", $form_data_fragmentation[$j]);
$object = array_merge($object, [$key => trim($form_data_fragmentation[($j + 1)])]);
}
}
return $object;
}

/**
* Access data information from $_GET action of url request
* @param $item
* @return mixed|null
*/
static function get($item)
{
$object_data = self::put();
if (isset($_POST[$item])) {
return $_POST[$item];
} else if (isset($_GET[$item])) {
return $_GET[$item];
} else if (isset($object_data[$item])) {
return $object_data[$item];
}
return null;
bim-g marked this conversation as resolved.
Show resolved Hide resolved
return $_GET[$item] ?? null;
}

/**
* Access HEADER data information from header
* Extract header data information
* @param $item
* @return mixed|null
Expand All @@ -100,10 +96,35 @@ public static function header($item)
}

/**
* @return array|null
* Lists params submitted via POST, PATCH or PUT action event
* @return array
*/
public static function body()
public static function body(): ?array
{
return isset($_POST) && !empty($_POST) ? $_POST : !empty(self::put() ? self::put() : null);
return !empty($_POST) ? $_POST : (!empty(self::put()) ? self::put() : null);
}

/**
* Access data information from POST or PUT action event
* @param $item
* @return mixed|string|null
*/
public static function post($item){
$self_put = self::put();
if (isset($_POST[$item])) {
return $_POST[$item];
} else if (isset($self_put[$item])) {
return $self_put[$item];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is better to have a form-data method to avoid confusion.

}
return null;
}

/**
* Access data information from file upload event
* @param string $item
* @return mixed|null
*/
public static function file(string $item) {
return $_FILES[$item] ?? null;
}
}
}
Loading