Skip to content

Commit

Permalink
Merge pull request #67 from bim-g/array_number
Browse files Browse the repository at this point in the history
[UPD] add filter to array string
  • Loading branch information
bim-g authored Jan 25, 2023
2 parents 39b4883 + 6843560 commit c679345
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,18 @@ use this method to validate your array data object.
- `string` : check if content of an array are string,
- `number` : check if content of an array are numbers,

```php
$schema = [
"names" => $schema->array()->min(1)->max(10)->string()->generate();
"ages" => $schema->array()->min(2)->max(5)->number()->generate()
]
// name should be an array and should have a minimum of 1 element and maximum should be 10, each element should be a type string
// ages should be an array and should have a minimum of 2 elements and does not exceed 5, each element should be a type number

```
In some case the array can have children, and
method, the array module as also it own method to control elements,
- `body`: will allow to multidimensional array.
- `structure`: will allow to multidimensional array.
Resources
```php
$source = [
Expand All @@ -154,18 +163,19 @@ $source = [
];
```
Validation Schema

```php
$rules =[
"name" => $schema->string()->min(1)->max(10)->required()->generate(),
"email" => $schema->string()->email()->required()->generate(),
"possessions" => $schema->array()->min(1)->max(2)->required()->elements([
"possessions" => $schema->array()->min(1)->max(2)->required()->structure([
"cars" => $schema->number()->min(1)->required()->generate(),
"plane" => $schema->number()->min(1)->required()->generate(),
"houses" => $schema->number()->min(6)->required()->generate(),
"children" => $schema->number()->positive()->required()->generate(),
"children_name" => $schema->array()->string()->generate(),
"children_age" => $schema->array()->number()->generate(),
"location" => $schema->array()->min(2)->elements([
"location" => $schema->array()->min(2)->structure([
"goma" => $schema->string()->min(20)->generate(),
"bukabu" => $schema->string()->min(20)->generate(),
"kinshasa" => $schema->string()->min(20)->generate(),
Expand All @@ -175,10 +185,11 @@ $rules =[
```
The method take an array of a schema generated.
In case you want to validate array with string content use `string` from array schema
`Note`: while using array you can not use `string` method or `number` with `elements` at the sametime it will cause an error,
`Note`: while using array you can not use `string` method or `number` with `structure` at the sametime it will cause an error,
event you can not use `string` with `number` at once, it should be one or another.

```php
$schema->array()->string()->elements([])->generate()
$schema->array()->string()->structure([])->generate()
```
This will throw an error, each one should be used separately.
You can check the example folder then you can get all exercises for each method.
Expand Down
4 changes: 2 additions & 2 deletions example/arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
$rules =[
"name" => $schema->string()->min(1)->max(10)->required()->generate(),
"email" => $schema->string()->email()->required()->generate(),
"possessions" => $schema->array()->min(1)->max(20)->required()->elements([
"possessions" => $schema->array()->min(1)->max(20)->required()->structure([
"cars" => $schema->number()->min(1)->required()->generate(),
"plane" => $schema->number()->min(1)->required()->generate(),
"houses" => $schema->number()->min(6)->required()->generate(),
"children" => $schema->number()->positive()->required()->generate(),
'children_name' => $schema->array()->string()->generate(),
'children_age' => $schema->array()->number()->generate(),
"location" => $schema->array()->min(2)->elements([
"location" => $schema->array()->min(2)->structure([
"goma" => $schema->string()->min(20)->generate(),
"bukabu" => $schema->string()->min(20)->generate(),
"kinshasa" => $schema->string()->min(20)->generate(),
Expand Down
4 changes: 2 additions & 2 deletions src/Schema/ArraySchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public function __construct()
* @param array $elements data array to be validated
* @return $this
*/
public function elements(array $elements):ArraySchema{
public function structure(array $elements):?ArraySchema{
if(isset($this->schema[$this->class_name]['string']) || isset($this->schema[$this->class_name]['number'])) {
return false;
}
$this->schema[$this->class_name]['elements'] = $elements;
$this->schema[$this->class_name]['structure'] = $elements;
return $this;
}

Expand Down
46 changes: 29 additions & 17 deletions src/Validator/ArrayValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
namespace Wepesi\App\Validator;

use Wepesi\App\Providers\ValidatorProvider;
use Wepesi\App\Resolver\Option;
use Wepesi\App\Resolver\OptionsResolver;
use Wepesi\App\Validate;

final class ArrayValidator extends ValidatorProvider
Expand All @@ -27,6 +25,11 @@ public function __construct(string $item, array $data_source=[])
parent::__construct();
}

/**
* check the minimum length of an array should be about
* @param int $rule
* @return void
*/
public function min(int $rule)
{
// TODO: Implement min() method.
Expand Down Expand Up @@ -59,7 +62,8 @@ public function max(int $rule)
* @param array $elements validate an array if elements, it should be and array with key value to be well set
* @return void
*/
public function elements(array $elements){
public function structure(array $elements)
{
$validate = new Validate();
$element_source = $this->data_source[$this->field_name];
$validate->check($element_source,$elements);
Expand All @@ -72,26 +76,34 @@ public function elements(array $elements){
* check content are string, in other case handler an error
* @return void
*/
public function string(){
$message = [
'type' => 'array.string',
'message' => "`$this->field_name` should have at least 1 elements",
'label' => $this->field_name,
'limit' => 1
];
public function string()
{
$len = count($this->field_value);
if ($len < 1) {
$this->addError($message);
$this->min(1);
}else{
for($i=0; $i<$len; $i++){
if(!is_string($this->field_value[$i])){
$message['message'] = "`$this->field_name[$i]` should be a string";
$this->addError($message);
}
$filter = array_filter($this->field_value,function($element){
if(!is_string($element)) return $element;
});
$keys = array_keys($filter);
for($i=0; $i<count($keys); $i++){
$position = $keys[$i];
$message = [
'type' => 'array.string',
'message' => "`$this->field_name[$position]` should be a string",
'label' => $this->field_name,
'limit' => 1
];
$this->addError($message);
}
}
}
public function number(){

/**
* @return void
*/
public function number():void
{
$len = count($this->field_value);
if ($len < 1) {
$this->min(1);
Expand Down

0 comments on commit c679345

Please sign in to comment.