Skip to content

Commit

Permalink
Merge pull request #68 from bim-g/method_positive
Browse files Browse the repository at this point in the history
[ENH] add method to check min and max params
  • Loading branch information
bim-g committed Jan 25, 2023
2 parents c679345 + 4cacc0c commit 0a0977e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 17 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ use this method your string value, it has built in method for different scenario
- `matches` : this is used tho check if two key has the same value, you should specify the second field to check.
- `generate` : this methode will generate the array structure that will be used to validate the value, and should be called and the end of each element. In case it's not called there will be an error.

### supported `url` :
* `http(s)://[domain].[extension]` ,
* `http(s)://www.[domain].[extension]`,
* `www.[domain].[extension]`

In the example bellow, you can see a complete procured on how to validate data-source

```php
Expand Down
15 changes: 15 additions & 0 deletions src/Providers/ValidatorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,19 @@ public function result(): array
{
return $this->errors;
}

protected function positiveParamMethod(int $rule,bool $max = false):bool{
$status = true;
if($rule<1){
$method = $max?"max":"min";
$message = [
'type' => $this->class_provider . ' method '. $method,
'message' => "'$this->field_name' $method param should be a positive number",
'label' => $this->field_name,
];
$this->addError($message);
$status = false;
}
return $status;
}
}
12 changes: 4 additions & 8 deletions src/Traits/InitTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ trait InitTrait
private function initInstance($source, $schema)
{
if (!is_array($source) || count($source) == 0) {
throw new Exception('Your Source Data should not be en empty array');
throw new \Exception('Your Source Data should not be en empty array');
}
if (!is_array($schema) || count($schema) == 0) {
throw new Exception('Your Schema should not be en empty array');
throw new \Exception('Your Schema should not be en empty array');
}
$fields = array_keys($schema);

Expand All @@ -34,11 +34,7 @@ private function initInstance($source, $schema)
/**
* @param array $schema
*/
protected function extract_data(array $schema ){
$conditions = $schema[$this->field_item]['String'];
foreach ($conditions as $key=>$value){
call_user_func([$this,$key],$value);
return;
}
protected function extract_data(array $schema ):void{
// TODO implement extract data
}
}
4 changes: 0 additions & 4 deletions src/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@

namespace Wepesi\App;

use Exception;
use phpDocumentor\Reflection\Types\Null_;
use ReflectionClass;
use Wepesi\App\Resolver\Option;
use Wepesi\App\Resolver\OptionsResolver;
use function PHPUnit\Framework\throwException;


class Validate
{
Expand Down
10 changes: 6 additions & 4 deletions src/Validator/ArrayValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public function __construct(string $item, array $data_source=[])
* @param int $rule
* @return void
*/
public function min(int $rule)
public function min(int $rule):void
{
// TODO: Implement min() method.
if($this->positiveParamMethod($rule)) return;
if (count($this->field_value) < $rule) {
$message = [
'type' => 'array.min',
Expand All @@ -44,9 +45,10 @@ public function min(int $rule)
}
}

public function max(int $rule)
public function max(int $rule):void
{
// TODO: Implement max() method.
if($this->positiveParamMethod($rule,true)) return;
if (count($this->field_value) > $rule) {
$message = [
'type' => 'array.max',
Expand All @@ -62,7 +64,7 @@ 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 structure(array $elements)
public function structure(array $elements):void
{
$validate = new Validate();
$element_source = $this->data_source[$this->field_name];
Expand All @@ -76,7 +78,7 @@ public function structure(array $elements)
* check content are string, in other case handler an error
* @return void
*/
public function string()
public function string():void
{
$len = count($this->field_value);
if ($len < 1) {
Expand Down
2 changes: 2 additions & 0 deletions src/Validator/NumberValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function __construct(string $item, array $data_source) {
*/
function min(int $rule)
{
if($this->positiveParamMethod($rule)) return;
if ((int) $this->field_value < $rule) {
$message = [
"type" => "number.min",
Expand All @@ -55,6 +56,7 @@ function min(int $rule)
*/
function max(int $rule)
{
if($this->positiveParamMethod($rule,true)) return;
if ((int) $this->field_value > $rule) {
$message = [
"type" => "number.max",
Expand Down
4 changes: 3 additions & 1 deletion src/Validator/StringValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct(string $item, array $data_source=[]) {
*/
public function min(int $rule):void
{
if($this->positiveParamMethod($rule)) return;
if (strlen($this->field_value) < $rule) {
$message=[
"type"=>"string.min",
Expand All @@ -51,8 +52,9 @@ public function min(int $rule):void
* @param int $rule
*
*/
public function max(int $rule)
public function max(int $rule):void
{
if($this->positiveParamMethod($rule,true)) return;
if (strlen($this->field_value) > $rule) {
$message = [
"type" => "string.max",
Expand Down

0 comments on commit 0a0977e

Please sign in to comment.