Skip to content

Commit

Permalink
upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrick Peter committed Nov 2, 2023
1 parent 43a6937 commit 644d4a5
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 93 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ $form->rules([
<option value="USA">United States of America</option>
</select>
<input type="hidden" name="csrf_token" value="749c345a1d407f29e777349f5e46a8d6d2cd51454b6719228b5ee28f94c30432">
<input type="hidden" name="_token" value="749c345a1d407f29e777349f5e46a8d6d2cd51454b6719228b5ee28f94c30432">
<input type="email" name="email" placeholder="Email Address">
</form>
```

### Validate
- Takes an [optional] `callable` function as the param
- Takes an [optional] `closure` function as the param

```
$form->rules([
Expand All @@ -252,7 +252,7 @@ $form->rules([
```

### Save
- Expects a `callable` function as the param
- Expects a `closure` function as the param
- Message property will be empty string on success `$response->message`

```
Expand Down Expand Up @@ -297,7 +297,7 @@ $form->rules([


### noInterface
- Expects a `callable` function as the param
- Expects a `closure` function as the param
- have access to form data without any validation

```
Expand All @@ -310,7 +310,7 @@ $form->noInterface(function($response){
```

### Before
- Expects a `callable` function as the param
- Expects a `closure` function as the param
- Will only execute code within when Request is [GET]
- CSRF Token `does'nt` apply to this method

Expand All @@ -324,7 +324,7 @@ $form->rules([
```

### After
- Expects a `callable` function as the param
- Expects a `closure` function as the param
- Will always execute no matter the request method type
- CSRF Token `does'nt` apply to this method

Expand Down
3 changes: 1 addition & 2 deletions src/Methods/CsrfToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CsrfToken{
*
* @var string
*/
static private $session = 'csrf_token';
static private $session = '_token';

/**
* Use Csrf Token
Expand All @@ -38,7 +38,6 @@ class CsrfToken{
public function __construct($csrf = true)
{
self::$csrf = $csrf;
self::$session = 'csrf_token';
self::$token = bin2hex(random_bytes(32));

// Start the session if it has not already been started
Expand Down
35 changes: 30 additions & 5 deletions src/Methods/Datatype.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ protected static function validateForminput($rulesData)
{
// lowercase
$rulesData['data_type'] = Str::lower($rulesData['data_type']);
$request = self::$validator->config['request'];
$param = self::$validator->param[$rulesData['input_name']];
$request = self::$validator->config['request'];
$param = self::$validator->param[$rulesData['input_name']];

switch($rulesData['data_type']){

Expand All @@ -108,17 +108,29 @@ protected static function validateForminput($rulesData)
break;

// integer validation
case (in_array($rulesData['data_type'], ['int', 'i'])):
case (in_array($rulesData['data_type'], ['int', 'integer', 'i'])):
$type = filter_input($request, $rulesData['input_name'], FILTER_VALIDATE_INT);

// set error to false if value is 0
// changing the value for validation to be ignored, since 0 also seen as false
if($param == '0'){
$type = 'false';
}
break;

// float validation
case (in_array($rulesData['data_type'], ['float', 'f'])):
$type = filter_input($request, $rulesData['input_name'], FILTER_VALIDATE_FLOAT);

// set error to false if value is 0
// changing the value for validation to be ignored, since 0 also seen as false
if($param == '0'){
$type = 'false';
}
break;

// url validation
case (in_array($rulesData['data_type'], ['url', 'u'])):
case (in_array($rulesData['data_type'], ['url', 'link', 'u'])):
$type = filter_input($request, $rulesData['input_name'], FILTER_VALIDATE_URL);
break;

Expand All @@ -138,7 +150,7 @@ protected static function validateForminput($rulesData)
break;

// enum validation
case (in_array($rulesData['data_type'], ['enum', 'en'])):
case (in_array($rulesData['data_type'], ['enum', 'en', 'enm'])):
// if value is not set -- it will return null
if(is_null(filter_input($request, $rulesData['input_name']))){
$type = '';
Expand All @@ -150,6 +162,12 @@ protected static function validateForminput($rulesData)
if(empty($type) && $type != '0') {
$type = false;
}

// set error to false if value is 0
// changing the value for validation to be ignored, since 0 also seen as false
if($param == '0'){
$type = 'false';
}
break;

// string validation
Expand All @@ -162,6 +180,13 @@ protected static function validateForminput($rulesData)
if(empty($type) && $type != '0') {
$type = false;
}

// set error to false if value is 0
// changing the value for validation to be ignored, since 0 also seen as false
if($param == '0'){
$type = 'false';
}

break;
}

Expand Down
89 changes: 66 additions & 23 deletions src/Methods/ValidatorMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,48 +161,58 @@ public static function has($key = null)
/**
* Remove value of parameters form objects
*
* @param array|null $keys
* @param array|null $data
* @param array|null|Collection $keys
* @param array|null|Collection $data
*
* @return array
*/
public static function merge($keys = null, $data = null)
{
$keys = ValidatorMethod::isCollectionInstance($keys) ? $keys?->toArray() : $keys;
$data = ValidatorMethod::isCollectionInstance($data) ? $data?->toArray() : $data;

if(is_array($keys) && is_array($data)){
return array_merge($keys, $data);
}
}

/**
* Get needed data from array
* @param array $keys of needed data
* @param array|null $allData param to check from
* @param array|null|Collection $keys of needed data
* @param array|null|Collection $allData param to check from
*
* @return array
*/
public static function onlyData($keys = null, $allData = null)
public static function onlyData($keys = null, $data = null)
{
$data = [];
$allData = [];

$keys = ValidatorMethod::isCollectionInstance($keys) ? $keys?->toArray() : $keys;
$data = ValidatorMethod::isCollectionInstance($data) ? $data?->toArray() : $data;

if(is_array($keys) && is_array($data)){
foreach($keys as $key){
if(in_array($key, array_keys($allData))){
$data[$key] = $allData[$key];
if(in_array($key, array_keys($data))){
$allData[$key] = $data[$key];
}
}

return $data;
return $allData;
}
}

/**
* Get all needed params except the removed onces
* @param array|null $keys of to remove from parameters
* @param array|null $data param to check from
* @param array|null|Collection $keys of to remove from parameters
* @param array|null|Collection $data param to check from
*
* @return mixed
*/
public static function exceptData($keys = null, $data = null)
{
$keys = ValidatorMethod::isCollectionInstance($keys) ? $keys?->toArray() : $keys;
$data = ValidatorMethod::isCollectionInstance($data) ? $data?->toArray() : $data;

if(is_array($keys) && is_array($data)){
foreach($keys as $key){
if(in_array($key, array_keys($data))){
Expand All @@ -229,19 +239,36 @@ public static function old($key = null, $default = null)
// in array keys
$formData = self::getForm();

if(is_array($formData) && in_array($key, array_keys($formData))){
// get data using key
$data = $formData[$key] ?? $default;
// Split the key into an array of segments
$keySegments = explode('.', $key);

// check if the data to be returned is an array
if(is_array($data)){
return array_combine($data, $data);
}

// Initialize a variable to keep track of the current data
$data = $formData;

// Traverse through the key segments to access nested data
foreach ($keySegments as $segment) {
if (is_array($data) && array_key_exists($segment, $data)) {
$data = $data[$segment];
}
}

// Check if the final data is an array
if (is_array($data)) {
$data = array_combine($data, $data);
}

// if not an array
if(!is_array($data)){
return $data;
}

// if not empty and segment count is 1
if(!empty($data) && count($keySegments) === 1){
return $data;
}

return $default;
// return data or default
return $data[end($keySegments)] ?? $default;
}

/**
Expand Down Expand Up @@ -323,9 +350,25 @@ public static function getClass()
public static function getForm()
{
$mainForm = self::$validator->param->toArray();
return !empty($mainForm)
? $mainForm
: self::$validator->all()->param->toArray();

if(!empty($mainForm)){
return $mainForm;
} else{
$allForm = self::$validator->all()->param->toArray();
return !empty($allForm)
? $allForm
: array_merge($_POST, $_GET);
}
}

/**
* If instance of collection
* @param mixed $data
* @return bool
*/
public static function isCollectionInstance($data = null)
{
return ($data instanceof Collection);
}

/**
Expand Down
13 changes: 11 additions & 2 deletions src/Traits/ValidateSuccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
use Tamedevelopers\Validator\Methods\ExceptionMessage;

trait ValidateSuccessTrait {


/**
* csrf_token
*
* @var string
*/
private $csrf_token = '_token';


/**
* Validate Rules
Expand Down Expand Up @@ -58,10 +67,10 @@ private function submitInitialization(?array $data = [])
// set error to true
$this->setErrorTrue();

if(!$this->param->has('csrf_token')){
if(!$this->param->has($this->csrf_token)){
$this->message = ExceptionMessage::csrfTokenNotFound();
return $this;
} elseif(!CsrfToken::validateToken($this->param->csrf_token)){
} elseif(!CsrfToken::validateToken($this->param->{$this->csrf_token})){
$this->message = ExceptionMessage::csrfTokenMismatch();
return $this;
}
Expand Down
Loading

0 comments on commit 644d4a5

Please sign in to comment.