forked from miguel456/rbrecruiter
Revert "Apply fixes from StyleCI (pull request #6)"
This reverts pull request #6. > This pull request applies code style fixes from an analysis carried out by [StyleCI](https://bitbucket.styleci.io). > > For more information, click [here](https://bitbucket.styleci.io/analyses/a2Jl7D).
This commit is contained in:
@@ -1,141 +1,138 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright © 2020 Miguel Nogueira
|
||||
*
|
||||
* This file is part of Raspberry Staff Manager.
|
||||
*
|
||||
* Raspberry Staff Manager is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Raspberry Staff Manager is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Raspberry Staff Manager. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class ContextAwareValidator
|
||||
{
|
||||
/**
|
||||
* The excludedNames array will make the validator ignore any of these names when including names into the rules.
|
||||
* @var array
|
||||
*/
|
||||
private $excludedNames = [
|
||||
'_token',
|
||||
'_method',
|
||||
'formName',
|
||||
];
|
||||
|
||||
/**
|
||||
* Utility wrapper for json_encode.
|
||||
*
|
||||
* @param array $value The array to be converted.
|
||||
* @return string The JSON representation of $value
|
||||
*/
|
||||
private function encode(array $value): string
|
||||
* The excludedNames array will make the validator ignore any of these names when including names into the rules.
|
||||
* @var array
|
||||
*/
|
||||
private $excludedNames = [
|
||||
'_token',
|
||||
'_method',
|
||||
'formName'
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* Utility wrapper for json_encode.
|
||||
*
|
||||
* @param array $value The array to be converted.
|
||||
* @return string The JSON representation of $value
|
||||
*/
|
||||
private function encode(array $value) : string
|
||||
{
|
||||
return json_encode($value);
|
||||
return json_encode($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The getValidator() method will take an array of fields from the request body, iterates through them,
|
||||
* and dynamically adds validation rules for them. Depending on parameters, it may or may not generate
|
||||
* a form structure for rendering purposes.
|
||||
*
|
||||
* This method is mostly meant by internal use by means of static proxies (Facades), in order to reduce code repetition;
|
||||
* Using it outside it's directed scope may cause unexpected results; For instance, the method expects inputs to be in array format, e.g. myFieldNameID1[],
|
||||
* myFieldNameID2[], and so on and so forth.
|
||||
*
|
||||
* This isn't checked by the code yet, but if you're implementing it this way in the HTML markup, make sure it's consistent (e.g. use a loop).
|
||||
*
|
||||
* P.S This method automatically ignores the CSRF token for validation.
|
||||
*
|
||||
* @param array $fields The request form fields
|
||||
* @param bool $generateStructure Whether to incldue a JSON-ready form structure for rendering
|
||||
* @param bool $includeFormName Whether to include formName in the list of validation rules
|
||||
* @return Validator|Collection A validator instance you can use to check for validity, or a Collection with a validator and structure (validator, structure)
|
||||
*/
|
||||
* The getValidator() method will take an array of fields from the request body, iterates through them,
|
||||
* and dynamically adds validation rules for them. Depending on parameters, it may or may not generate
|
||||
* a form structure for rendering purposes.
|
||||
*
|
||||
* This method is mostly meant by internal use by means of static proxies (Facades), in order to reduce code repetition;
|
||||
* Using it outside it's directed scope may cause unexpected results; For instance, the method expects inputs to be in array format, e.g. myFieldNameID1[],
|
||||
* myFieldNameID2[], and so on and so forth.
|
||||
*
|
||||
* This isn't checked by the code yet, but if you're implementing it this way in the HTML markup, make sure it's consistent (e.g. use a loop).
|
||||
*
|
||||
* P.S This method automatically ignores the CSRF token for validation.
|
||||
*
|
||||
* @param array $fields The request form fields
|
||||
* @param bool $generateStructure Whether to incldue a JSON-ready form structure for rendering
|
||||
* @param bool $includeFormName Whether to include formName in the list of validation rules
|
||||
* @return Validator|Collection A validator instance you can use to check for validity, or a Collection with a validator and structure (validator, structure)
|
||||
*/
|
||||
public function getValidator(array $fields, bool $generateStructure = false, bool $includeFormName = false)
|
||||
{
|
||||
$formStructure = [];
|
||||
$validator = [];
|
||||
|
||||
if ($includeFormName) {
|
||||
if ($includeFormName)
|
||||
{
|
||||
$validator['formName'] = 'required|string|max:100';
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($fields as $fieldName => $field) {
|
||||
if (! in_array($fieldName, $this->excludedNames)) {
|
||||
$validator[$fieldName.'.0'] = 'required|string';
|
||||
$validator[$fieldName.'.1'] = 'required|string';
|
||||
foreach ($fields as $fieldName => $field)
|
||||
{
|
||||
if(!in_array($fieldName, $this->excludedNames))
|
||||
{
|
||||
$validator[$fieldName . ".0"] = 'required|string';
|
||||
$validator[$fieldName . ".1"] = 'required|string';
|
||||
|
||||
if ($generateStructure) {
|
||||
if ($generateStructure)
|
||||
{
|
||||
$formStructure['fields'][$fieldName]['title'] = $field[0];
|
||||
$formStructure['fields'][$fieldName]['type'] = $field[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$validatorInstance = Validator::make($fields, $validator);
|
||||
}
|
||||
}
|
||||
|
||||
return ($generateStructure) ?
|
||||
$validatorInstance = Validator::make($fields, $validator);
|
||||
|
||||
return ($generateStructure) ?
|
||||
collect([
|
||||
'validator' => $validatorInstance,
|
||||
'structure' => $this->encode($formStructure),
|
||||
'validator' => $validatorInstance,
|
||||
'structure' => $this->encode($formStructure)
|
||||
])
|
||||
: $validatorInstance;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The getResponseValidator method is similar to the getValidator method; It basically takes
|
||||
* an array of fields from a previous form (that probably went through the other method) and adds validation
|
||||
* to the field names.
|
||||
*
|
||||
* Also generates the storable response structure if you tell it to.
|
||||
*
|
||||
* @param array $fields The received fields
|
||||
* @param array $formStructure The form structure - You must supply this if you want the response structure
|
||||
* @param bool $generateResponseStructure Whether to generate the response structure
|
||||
* @return Validator|Collection A collection or a validator, depending on the args. Will return validatior if only fields are supplied.
|
||||
*/
|
||||
* The getResponseValidator method is similar to the getValidator method; It basically takes
|
||||
* an array of fields from a previous form (that probably went through the other method) and adds validation
|
||||
* to the field names.
|
||||
*
|
||||
* Also generates the storable response structure if you tell it to.
|
||||
*
|
||||
* @param array $fields The received fields
|
||||
* @param array $formStructure The form structure - You must supply this if you want the response structure
|
||||
* @param bool $generateResponseStructure Whether to generate the response structure
|
||||
* @return Validator|Collection A collection or a validator, depending on the args. Will return validatior if only fields are supplied.
|
||||
*/
|
||||
public function getResponseValidator(array $fields, array $formStructure = [], bool $generateResponseStructure = true)
|
||||
{
|
||||
$responseStructure = [];
|
||||
$validator = [];
|
||||
|
||||
if (empty($formStructure) && $generateResponseStructure) {
|
||||
throw new \InvalidArgumentException('Illegal combination of arguments supplied! Please check the method\'s documentation.');
|
||||
}
|
||||
$responseStructure = [];
|
||||
$validator = [];
|
||||
|
||||
foreach ($fields as $fieldName => $value) {
|
||||
if (! in_array($fieldName, $this->excludedNames)) {
|
||||
$validator[$fieldName] = 'required|string';
|
||||
if (empty($formStructure) && $generateResponseStructure)
|
||||
{
|
||||
throw new \InvalidArgumentException('Illegal combination of arguments supplied! Please check the method\'s documentation.');
|
||||
}
|
||||
|
||||
if ($generateResponseStructure) {
|
||||
$responseStructure['responses'][$fieldName]['type'] = $formStructure['fields'][$fieldName]['type'] ?? 'Unavailable';
|
||||
$responseStructure['responses'][$fieldName]['title'] = $formStructure['fields'][$fieldName]['title'];
|
||||
$responseStructure['responses'][$fieldName]['response'] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach($fields as $fieldName => $value)
|
||||
{
|
||||
if(!in_array($fieldName, $this->excludedNames))
|
||||
{
|
||||
$validator[$fieldName] = 'required|string';
|
||||
|
||||
$validatorInstance = Validator::make($fields, $validator);
|
||||
if ($generateResponseStructure)
|
||||
{
|
||||
$responseStructure['responses'][$fieldName]['type'] = $formStructure['fields'][$fieldName]['type'] ?? 'Unavailable';
|
||||
$responseStructure['responses'][$fieldName]['title'] = $formStructure['fields'][$fieldName]['title'];
|
||||
$responseStructure['responses'][$fieldName]['response'] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ($generateResponseStructure) ?
|
||||
$validatorInstance = Validator::make($fields, $validator);
|
||||
|
||||
return ($generateResponseStructure) ?
|
||||
collect([
|
||||
'validator' => $validatorInstance,
|
||||
'responseStructure' => $this->encode($responseStructure),
|
||||
'validator' => $validatorInstance,
|
||||
'responseStructure' => $this->encode($responseStructure)
|
||||
])
|
||||
: $validatorInstance;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,23 +1,5 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright © 2020 Miguel Nogueira
|
||||
*
|
||||
* This file is part of Raspberry Staff Manager.
|
||||
*
|
||||
* Raspberry Staff Manager is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Raspberry Staff Manager is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Raspberry Staff Manager. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
@@ -27,18 +9,20 @@ use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Options
|
||||
{
|
||||
|
||||
public function getOption(string $option): string
|
||||
{
|
||||
$value = Cache::get($option);
|
||||
|
||||
if (is_null($value)) {
|
||||
Log::debug('Option '.$option.'not found in cache, refreshing from database');
|
||||
if (is_null($value))
|
||||
{
|
||||
Log::debug('Option ' . $option . 'not found in cache, refreshing from database');
|
||||
$value = Option::where('option_name', $option)->first();
|
||||
if (is_null($value)) {
|
||||
if (is_null($value))
|
||||
throw new \Exception('This option does not exist.');
|
||||
}
|
||||
|
||||
Cache::put($option, $value);
|
||||
Cache::put($option.'_desc', 'Undefined description');
|
||||
Cache::put($option . '_desc', 'Undefined description');
|
||||
}
|
||||
|
||||
return $value->option_value;
|
||||
@@ -46,14 +30,14 @@ class Options
|
||||
|
||||
public function setOption(string $option, string $value, string $description)
|
||||
{
|
||||
Option::create([
|
||||
'option_name' => $option,
|
||||
'option_value' => $value,
|
||||
'friendly_name' => $description,
|
||||
]);
|
||||
Option::create([
|
||||
'option_name' => $option,
|
||||
'option_value' => $value,
|
||||
'friendly_name' => $description
|
||||
]);
|
||||
|
||||
Cache::put($option, $value, now()->addDay());
|
||||
Cache::put($option.'_desc', $description, now()->addDay());
|
||||
Cache::put($option, $value, now()->addDay());
|
||||
Cache::put($option . '_desc', $description, now()->addDay());
|
||||
}
|
||||
|
||||
public function pullOption($option): array
|
||||
@@ -64,7 +48,7 @@ class Options
|
||||
// putMany is overkill here
|
||||
return [
|
||||
Cache::pull($option),
|
||||
Cache::pull($option.'_desc'),
|
||||
Cache::pull($option . '_desc')
|
||||
];
|
||||
}
|
||||
|
||||
@@ -72,13 +56,14 @@ class Options
|
||||
{
|
||||
$dbOption = Option::where('option_name', $option);
|
||||
|
||||
if ($dbOption->first()) {
|
||||
if ($dbOption->first())
|
||||
{
|
||||
$dbOptionInstance = Option::find($dbOption->first()->id);
|
||||
Cache::forget($option);
|
||||
|
||||
Log::debug('Changing db configuration option', [
|
||||
'old_value' => $dbOptionInstance->option_value,
|
||||
'new_value' => $newValue,
|
||||
'new_value' => $newValue
|
||||
]);
|
||||
|
||||
$dbOptionInstance->option_value = $newValue;
|
||||
@@ -86,20 +71,24 @@ class Options
|
||||
|
||||
Log::debug('New db configuration option saved',
|
||||
[
|
||||
'option' => $dbOptionInstance->option_value,
|
||||
'option' => $dbOptionInstance->option_value
|
||||
]);
|
||||
|
||||
Cache::put('option_name', $newValue, now()->addDay());
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('This option does not exist.');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function optionExists(string $option): bool
|
||||
{
|
||||
$dbOption = Option::where('option_name', $option)->first();
|
||||
$locallyCachedOption = Cache::get($option);
|
||||
|
||||
return ! is_null($dbOption) || ! is_null($locallyCachedOption);
|
||||
return !is_null($dbOption) || !is_null($locallyCachedOption);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user