diff --git a/composer.json b/composer.json index 1dab883..928d2cf 100644 --- a/composer.json +++ b/composer.json @@ -21,13 +21,10 @@ "require": { "ext-curl": "^7.3", "php": "^7.3", - "illuminate/filesystem": "^8.52", - "illuminate/support": "^8.52", - "illuminate/translation": "^8.52", - "illuminate/validation": "^8.52", "kwaadpepper/enum": "^0.1.4", "vlucas/phpdotenv": "^5.3", - "guzzlehttp/guzzle": "^7.3" + "guzzlehttp/guzzle": "^7.3", + "respect/validation": "^2.2" }, "require-dev": { "phpunit/phpunit": "~9.0", diff --git a/composer.lock b/composer.lock index 27affc1..dcf7f2c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "89b14a69db5103b9d01833ed279ab009", + "content-hash": "9dc1405dc55d3ea0102d0eb888f7dd62", "packages": [ { "name": "brick/math", @@ -2044,6 +2044,130 @@ ], "time": "2020-08-18T17:17:46+00:00" }, + { + "name": "respect/stringifier", + "version": "0.2.0", + "source": { + "type": "git", + "url": "https://github.com/Respect/Stringifier.git", + "reference": "e55af3c8aeaeaa2abb5fa47a58a8e9688cc23b59" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Respect/Stringifier/zipball/e55af3c8aeaeaa2abb5fa47a58a8e9688cc23b59", + "reference": "e55af3c8aeaeaa2abb5fa47a58a8e9688cc23b59", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.8", + "malukenho/docheader": "^0.1.7", + "phpunit/phpunit": "^6.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "Respect\\Stringifier\\": "src/" + }, + "files": [ + "src/stringify.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Respect/Stringifier Contributors", + "homepage": "https://github.com/Respect/Stringifier/graphs/contributors" + } + ], + "description": "Converts any value to a string", + "homepage": "http://respect.github.io/Stringifier/", + "keywords": [ + "respect", + "stringifier", + "stringify" + ], + "support": { + "issues": "https://github.com/Respect/Stringifier/issues", + "source": "https://github.com/Respect/Stringifier/tree/0.2.0" + }, + "time": "2017-12-29T19:39:25+00:00" + }, + { + "name": "respect/validation", + "version": "2.2.3", + "source": { + "type": "git", + "url": "https://github.com/Respect/Validation.git", + "reference": "4c21a7ffc9a4915673cb2c2843963919e664e627" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Respect/Validation/zipball/4c21a7ffc9a4915673cb2c2843963919e664e627", + "reference": "4c21a7ffc9a4915673cb2c2843963919e664e627", + "shasum": "" + }, + "require": { + "php": "^7.3 || ^8.0", + "respect/stringifier": "^0.2.0", + "symfony/polyfill-mbstring": "^1.2" + }, + "require-dev": { + "egulias/email-validator": "^3.0", + "malukenho/docheader": "^0.1", + "mikey179/vfsstream": "^1.6", + "phpstan/phpstan": "^0.12", + "phpstan/phpstan-deprecation-rules": "^0.12", + "phpstan/phpstan-phpunit": "^0.12", + "phpunit/phpunit": "^9.3", + "psr/http-message": "^1.0", + "respect/coding-standard": "^3.0", + "squizlabs/php_codesniffer": "^3.5", + "symfony/validator": "^3.0||^4.0", + "zendframework/zend-validator": "^2.1" + }, + "suggest": { + "egulias/email-validator": "Strict (RFC compliant) email validation", + "ext-bcmath": "Arbitrary Precision Mathematics", + "ext-fileinfo": "File Information", + "ext-mbstring": "Multibyte String Functions", + "symfony/validator": "Use Symfony validator through Respect\\Validation", + "zendframework/zend-validator": "Use Zend Framework validator through Respect\\Validation" + }, + "type": "library", + "autoload": { + "psr-4": { + "Respect\\Validation\\": "library/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Respect/Validation Contributors", + "homepage": "https://github.com/Respect/Validation/graphs/contributors" + } + ], + "description": "The most awesome validation engine ever created for PHP", + "homepage": "http://respect.github.io/Validation/", + "keywords": [ + "respect", + "validation", + "validator" + ], + "support": { + "issues": "https://github.com/Respect/Validation/issues", + "source": "https://github.com/Respect/Validation/tree/2.2.3" + }, + "time": "2021-03-19T14:12:45+00:00" + }, { "name": "swiftmailer/swiftmailer", "version": "v6.2.7", diff --git a/src/Models/ArcadiaMailDefault.php b/src/Models/ArcadiaMailDefault.php index 3bdc215..0ba1ecd 100644 --- a/src/Models/ArcadiaMailDefault.php +++ b/src/Models/ArcadiaMailDefault.php @@ -124,7 +124,7 @@ class ArcadiaMailDefault extends CrmClientModel 'operation' => 'required|int', 'support_code' => 'required|int', 'label_type_support' => 'required|string|min:2|max:255', - 'order_date' => 'required|date', + 'order_date' => 'required|date_format:Y/m/d H:i:s', 'order_num' => 'required|int', 'state_order' => ['required'], 'package_unit_weight' => 'required|int' diff --git a/src/Models/ArcadiaMailGame.php b/src/Models/ArcadiaMailGame.php index 71e6e63..718e842 100644 --- a/src/Models/ArcadiaMailGame.php +++ b/src/Models/ArcadiaMailGame.php @@ -108,7 +108,7 @@ class ArcadiaMailGame extends CrmClientModel 'operation' => 'required|int', 'support_code' => 'required|int', 'label_type_support' => 'required|string|min:2|max:255', - 'order_date' => 'required|date', + 'order_date' => 'required|date_format:Y/m/d H:i:s', 'code_unique' => 'required|string', 'code_on_pack' => 'sometimes|string' ]; diff --git a/src/Models/CrmClientModel.php b/src/Models/CrmClientModel.php index be997f4..8e06de5 100644 --- a/src/Models/CrmClientModel.php +++ b/src/Models/CrmClientModel.php @@ -175,7 +175,7 @@ abstract class CrmClientModel [$field => $value], [$field => static::$validation[$field]] ); - if ($validator->fails()) { + if ($validator()) { throw new ValidationException(implode(' ', $validator->errors()->get($field))); } return true; @@ -286,7 +286,7 @@ abstract class CrmClientModel ['id' => $primaryKey], ['id' => 'required|int'] ); - if ($validator->fails()) { + if ($validator()) { throw new ValidationException(implode(' ', $validator->errors()->get('id'))); } } @@ -304,7 +304,7 @@ abstract class CrmClientModel ['hashcode' => $hashcode], ['hashcode' => 'required|uuid'] ); - if ($validator->fails()) { + if ($validator()) { throw new ValidationException(implode(' ', $validator->errors()->get('hashcode'))); } } diff --git a/src/Models/Customer.php b/src/Models/Customer.php index a39514e..70a7da0 100644 --- a/src/Models/Customer.php +++ b/src/Models/Customer.php @@ -269,7 +269,7 @@ class Customer extends CrmClientModel */ protected static $validation = [ 'hashcode' => 'required|uuid', - 'email' => 'required|email:rfc,dns,spoof,filter', + 'email' => 'required|email', 'firstname' => 'required|string|min:2|max:255', 'lastname' => 'required|string|min:2|max:255', 'password' => 'required|min:8|max:255', diff --git a/src/Models/SingleOptin.php b/src/Models/SingleOptin.php index cbdcabb..b5021b1 100644 --- a/src/Models/SingleOptin.php +++ b/src/Models/SingleOptin.php @@ -24,7 +24,7 @@ abstract class SingleOptin extends CrmClientModel * @var array */ protected static $validation = [ - 'email' => 'required|email:rfc,dns,spoof,filter', + 'email' => 'required|email', 'opt_in' => 'required|bool', ]; diff --git a/src/Utils.php b/src/Utils.php index cbcc99f..851b3a3 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -3,12 +3,10 @@ namespace LDC\LDCCRM; use Dotenv\Dotenv; -use Illuminate\Filesystem\Filesystem; -use Illuminate\Translation\FileLoader; -use Illuminate\Translation\Translator; -use Illuminate\Validation\Factory; use LDC\LDCCRM\Enums\HttpMethods; +use LDC\LDCCRM\Exceptions\ValidationException; use LDC\LDCCRM\Transport\Request; +use Respect\Validation\Validator; /** * This is the LDCCRM utility class @@ -48,19 +46,157 @@ class Utils */ public static function makeValidator( array $data, - array $rules, - array $messages = [], - array $customAttributes = [] - ) { - $filesystem = new Filesystem(); - $fileLoader = new FileLoader($filesystem, dirname(dirname(__FILE__)) . '/resources/lang'); - $fileLoader->addNamespace('lang', dirname(dirname(__FILE__)) . '/resources/lang'); - $fileLoader->load('en', 'validation', 'lang'); - $translator = new Translator($fileLoader, 'en'); - $translator->load('lang', 'validation', 'en'); - $factory = new Factory($translator); - - return $factory->make($data, $rules, $messages, $customAttributes); + array $rules + ): object { + foreach ($rules as $attribute => $attrRules) { + $validators = []; + if (\is_string($attrRules)) { + $attrRules = \explode('|', $attrRules); + } + foreach ($attrRules as $rule) { + $closureFail = function ($message) { + throw new ValidationException($message); + }; + $closure = null; + $rParams = []; + if (\is_callable($rule) and !is_string($rule)) { + $closure = $rule; + $rule = 'closure'; + } elseif (\strpos($rule, ':') !== false) { + $rParams = \explode(':', $rule, 2); + $rule = \array_shift($rParams); + } + switch ($rule) { + case 'required': + $validators[] = [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute) { + return isset($data[$attribute]); + }, + 'error' => "$attribute is required" + ]; + break; + case 'sometimes': + $validators[] = [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute) { + return true; + }, + 'error' => "$attribute is required" + ]; + break; + case 'int': + $validators[] = + [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute) { + return \is_int($data[$attribute]); + }, + 'error' => "$attribute must be an integer" + ]; + break; + case 'string': + $validators[] = + [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute) { + return \is_string($data[$attribute]); + }, + 'error' => "$attribute must be a string" + ]; + break; + case 'bool': + $validators[] = + [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute) { + return \is_bool($data[$attribute]); + }, + 'error' => "$attribute must be a boolean" + ]; + break; + case 'min': + $validators[] = [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute, $rParams) { + return \is_string($data[$attribute]) and + \strlen($data[$attribute]) >= (int)$rParams[0]; + }, + 'error' => "$attribute must be a string and have less or equals $rParams[0] chars" + ]; + break; + case 'max': + $validators[] = [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute, $rParams) { + return \is_string($data[$attribute]) and + \strlen($data[$attribute]) <= (int)$rParams[0]; + }, + 'error' => "$attribute must be a string and have more or equals $rParams[0] chars" + ]; + break; + case 'uuid': + $validators[] = [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute) { + return Validator::uuid()->validate($data[$attribute]); + }, + 'error' => "$attribute must be an uuid" + ]; + break; + case 'date': + $validators[] = [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute) { + return Validator::dateTime()->validate($data[$attribute]); + }, + 'error' => "$attribute must be valid date" + ]; + break; + case 'date_format': + $validators[] = [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute, $rParams) { + return Validator::dateTime($rParams[0])->validate($data[$attribute]); + }, + 'error' => "$attribute must be valid date with format $rParams[0]" + ]; + break; + case 'email': + $validators[] = [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute) { + return Validator::email()->validate($data[$attribute]); + }, + 'error' => "$attribute must be a valid email" + ]; + break; + case 'closure': + $validators[] = [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute, $closure, $closureFail) { + $closure($attribute, $data[$attribute], $closureFail); + return true; + }, + 'error' => "$attribute must validate" + ]; + break; + default: + throw new \Exception("Unknown validation rule $rule " . implode(': ', $rParams)); + } + } + } + return function () use ($validators) { + foreach ($validators as $validator) { + if (!$validator['validate']()) { + throw new ValidationException(sprintf( + '%s : %s', + $validator['type'], + $validator['error'] + )); + } + } + }; } /**