diff --git a/apiConfig.readonly.json b/apiConfig.readonly.json index 66c9adb..f02840b 100644 --- a/apiConfig.readonly.json +++ b/apiConfig.readonly.json @@ -1,5 +1,5 @@ { - "version": "2.0.11", + "version": "2.0.12", "tokenTimeout": 60, "apiRequestRatePerMinute": 256 } \ No newline at end of file diff --git a/composer.json b/composer.json index afe5dbe..ab4f6d1 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "type": "library", "description": "LDC CRM Client V2", "license": "MIT", - "version": "2.0.11", + "version": "2.0.12", "authors": [ { "name": "Jérémy Munsch", diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php deleted file mode 100644 index 9296ce2..0000000 --- a/resources/lang/en/validation.php +++ /dev/null @@ -1,154 +0,0 @@ - 'The :attribute must be accepted.', - 'active_url' => 'The :attribute is not a valid URL.', - 'after' => 'The :attribute must be a date after :date.', - 'after_or_equal' => 'The :attribute must be a date after or equal to :date.', - 'alpha' => 'The :attribute may only contain letters.', - 'alpha_dash' => 'The :attribute may only contain letters, numbers, dashes and underscores.', - 'alpha_num' => 'The :attribute may only contain letters and numbers.', - 'array' => 'The :attribute must be an array.', - 'before' => 'The :attribute must be a date before :date.', - 'before_or_equal' => 'The :attribute must be a date before or equal to :date.', - 'between' => [ - 'numeric' => 'The :attribute must be between :min and :max.', - 'file' => 'The :attribute must be between :min and :max kilobytes.', - 'string' => 'The :attribute must be between :min and :max characters.', - 'array' => 'The :attribute must have between :min and :max items.', - ], - 'boolean' => 'The :attribute field must be true or false.', - 'confirmed' => 'The :attribute confirmation does not match.', - 'date' => 'The :attribute is not a valid date.', - 'date_equals' => 'The :attribute must be a date equal to :date.', - 'date_format' => 'The :attribute does not match the format :format.', - 'different' => 'The :attribute and :other must be different.', - 'digits' => 'The :attribute must be :digits digits.', - 'digits_between' => 'The :attribute must be between :min and :max digits.', - 'dimensions' => 'The :attribute has invalid image dimensions.', - 'distinct' => 'The :attribute field has a duplicate value.', - 'email' => 'The :attribute must be a valid email address.', - 'ends_with' => 'The :attribute must end with one of the following: :values.', - 'exists' => 'The selected :attribute is invalid.', - 'file' => 'The :attribute must be a file.', - 'filled' => 'The :attribute field must have a value.', - 'gt' => [ - 'numeric' => 'The :attribute must be greater than :value.', - 'file' => 'The :attribute must be greater than :value kilobytes.', - 'string' => 'The :attribute must be greater than :value characters.', - 'array' => 'The :attribute must have more than :value items.', - ], - 'gte' => [ - 'numeric' => 'The :attribute must be greater than or equal :value.', - 'file' => 'The :attribute must be greater than or equal :value kilobytes.', - 'string' => 'The :attribute must be greater than or equal :value characters.', - 'array' => 'The :attribute must have :value items or more.', - ], - 'image' => 'The :attribute must be an image.', - 'in' => 'The selected :attribute is invalid.', - 'in_array' => 'The :attribute field does not exist in :other.', - 'integer' => 'The :attribute must be an integer.', - 'ip' => 'The :attribute must be a valid IP address.', - 'ipv4' => 'The :attribute must be a valid IPv4 address.', - 'ipv6' => 'The :attribute must be a valid IPv6 address.', - 'json' => 'The :attribute must be a valid JSON string.', - 'lt' => [ - 'numeric' => 'The :attribute must be less than :value.', - 'file' => 'The :attribute must be less than :value kilobytes.', - 'string' => 'The :attribute must be less than :value characters.', - 'array' => 'The :attribute must have less than :value items.', - ], - 'lte' => [ - 'numeric' => 'The :attribute must be less than or equal :value.', - 'file' => 'The :attribute must be less than or equal :value kilobytes.', - 'string' => 'The :attribute must be less than or equal :value characters.', - 'array' => 'The :attribute must not have more than :value items.', - ], - 'max' => [ - 'numeric' => 'The :attribute may not be greater than :max.', - 'file' => 'The :attribute may not be greater than :max kilobytes.', - 'string' => 'The :attribute may not be greater than :max characters.', - 'array' => 'The :attribute may not have more than :max items.', - ], - 'mimes' => 'The :attribute must be a file of type: :values.', - 'mimetypes' => 'The :attribute must be a file of type: :values.', - 'min' => [ - 'numeric' => 'The :attribute must be at least :min.', - 'file' => 'The :attribute must be at least :min kilobytes.', - 'string' => 'The :attribute must be at least :min characters.', - 'array' => 'The :attribute must have at least :min items.', - ], - 'multiple_of' => 'The :attribute must be a multiple of :value', - 'not_in' => 'The selected :attribute is invalid.', - 'not_regex' => 'The :attribute format is invalid.', - 'numeric' => 'The :attribute must be a number.', - 'password' => 'The password is incorrect.', - 'present' => 'The :attribute field must be present.', - 'regex' => 'The :attribute format is invalid.', - 'required' => 'The :attribute field is required.', - 'required_if' => 'The :attribute field is required when :other is :value.', - 'required_unless' => 'The :attribute field is required unless :other is in :values.', - 'required_with' => 'The :attribute field is required when :values is present.', - 'required_with_all' => 'The :attribute field is required when :values are present.', - 'required_without' => 'The :attribute field is required when :values is not present.', - 'required_without_all' => 'The :attribute field is required when none of :values are present.', - 'same' => 'The :attribute and :other must match.', - 'size' => [ - 'numeric' => 'The :attribute must be :size.', - 'file' => 'The :attribute must be :size kilobytes.', - 'string' => 'The :attribute must be :size characters.', - 'array' => 'The :attribute must contain :size items.', - ], - 'starts_with' => 'The :attribute must start with one of the following: :values.', - 'string' => 'The :attribute must be a string.', - 'timezone' => 'The :attribute must be a valid zone.', - 'unique' => 'The :attribute has already been taken.', - 'uploaded' => 'The :attribute failed to upload.', - 'url' => 'The :attribute format is invalid.', - 'uuid' => 'The :attribute must be a valid UUID.', - - /* - |-------------------------------------------------------------------------- - | Custom Validation Language Lines - |-------------------------------------------------------------------------- - | - | Here you may specify custom validation messages for attributes using the - | convention "attribute.rule" to name the lines. This makes it quick to - | specify a specific custom language line for a given attribute rule. - | - */ - - 'require_at_least_field' => 'At least one of these fields :attributes must be present.', - - /* - |-------------------------------------------------------------------------- - | Custom Validation Attributes - |-------------------------------------------------------------------------- - | - | The following language lines are used to swap our attribute placeholder - | with something more reader friendly such as "E-Mail Address" instead - | of "email". This simply helps us make our message more expressive. - | - */ - - 'attributes' => [ - // Rajouté - 'postcode' => 'post code', - 'birth_date' => 'birth date', - 'lastname' => 'last name', - 'firstname' => 'first name' - ], - -]; diff --git a/resources/lang/fr.php b/resources/lang/fr.php new file mode 100644 index 0000000..beaefd6 --- /dev/null +++ b/resources/lang/fr.php @@ -0,0 +1,31 @@ + [ + 'support_code' => 'code support', + 'label_type_support' => 'label type de support', + 'order_date' => 'date de commande', + 'order_num' => 'numéro de commande', + 'state_order' => 'état commande', + 'package_unit_weight' => 'poids unitaire du paquet', + 'code_unique' => 'code unique', + 'code_on_pack' => 'code du paquet', + 'answer' => 'réponse', + 'email' => 'e-mail', + 'firstname' => 'prénom', + 'lastname' => 'nom de famille', + 'lastname' => 'nom de famille', + 'password' => 'mot de passe', + 'birth_date' => 'date de naissance', + 'phone' => 'téléphone', + 'address' => 'adresse', + 'address2' => 'adresse complémentaire', + 'city' => 'ville', + 'postcode' => 'code postal', + 'country' => 'pays', + 'opt_in' => 'newsletter', + 'status' => 'état', + 'last_connection' => 'denière connexion', + 'answers' => 'réponses' + ] +]; diff --git a/src/Models/Customer.php b/src/Models/Customer.php index 32cf3dc..b6c77a8 100644 --- a/src/Models/Customer.php +++ b/src/Models/Customer.php @@ -269,7 +269,7 @@ class Customer extends CrmClientModel 'email' => 'required|email', 'firstname' => 'required|string|min:2|max:255', 'lastname' => 'required|string|min:2|max:255', - 'password' => 'required|min:8|max:255', + 'password' => 'required|min:8|max:255|mixedCase|containDigit|containSymbol', 'birth_date' => 'required|date_format:Y-m-d', 'phone' => 'sometimes|string|min:10|max:255', 'address' => 'sometimes|string|max:255', @@ -293,20 +293,6 @@ class Customer extends CrmClientModel $fail("Country ($type)$value is not in Country enum list"); } }; - self::$validation['password'] = array_merge(explode('|', self::$validation['password']), [ - // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed,Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed - function ($attribute, $value, $fail) { - if (!\preg_match('/[a-z]/', $value) or !\preg_match('/[A-Z]/', $value)) { - $fail('The password must contain uppercase and lowercase chars.'); - } - if (!\preg_match('/[0-9]/', $value)) { - $fail('The password must contain numbers.'); - } - if (!\preg_match('/[^\w]/', $value)) { - $fail('The password must contain symbols.'); - } - } - ]); } $this->fill($properties); return $this; diff --git a/src/Utils.php b/src/Utils.php index 851b3a3..b48af39 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -35,6 +35,13 @@ class Utils */ private static $config; + /** + * Loaded translations + * + * @var array + */ + private static $translations = []; + /** * Create a new Validator instance. * @@ -44,6 +51,7 @@ class Utils * @param array $customAttributes * @return \Illuminate\Validation\Validator */ + // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded public static function makeValidator( array $data, array $rules @@ -73,7 +81,7 @@ class Utils 'validate' => function () use ($data, $attribute) { return isset($data[$attribute]); }, - 'error' => "$attribute is required" + 'error' => self::translate($attribute) . ' est obligatoire' ]; break; case 'sometimes': @@ -82,7 +90,7 @@ class Utils 'validate' => function () use ($data, $attribute) { return true; }, - 'error' => "$attribute is required" + 'error' => self::translate($attribute) . ' est falcutatif' ]; break; case 'int': @@ -92,7 +100,7 @@ class Utils 'validate' => function () use ($data, $attribute) { return \is_int($data[$attribute]); }, - 'error' => "$attribute must be an integer" + 'error' => self::translate($attribute) . ' doit être un entier' ]; break; case 'string': @@ -102,7 +110,7 @@ class Utils 'validate' => function () use ($data, $attribute) { return \is_string($data[$attribute]); }, - 'error' => "$attribute must be a string" + 'error' => self::translate($attribute) . ' doit être une chaine de charactères' ]; break; case 'bool': @@ -112,7 +120,7 @@ class Utils 'validate' => function () use ($data, $attribute) { return \is_bool($data[$attribute]); }, - 'error' => "$attribute must be a boolean" + 'error' => self::translate($attribute) . ' doit être un booléen' ]; break; case 'min': @@ -122,7 +130,7 @@ class Utils 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" + 'error' => self::translate($attribute) . " doit avoir moins ou $rParams[0] caratères" ]; break; case 'max': @@ -132,7 +140,7 @@ class Utils 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" + 'error' => self::translate($attribute) . " doit avoir plus ou $rParams[0] caratères" ]; break; case 'uuid': @@ -141,7 +149,36 @@ class Utils 'validate' => function () use ($data, $attribute) { return Validator::uuid()->validate($data[$attribute]); }, - 'error' => "$attribute must be an uuid" + 'error' => self::translate($attribute) . ' doit être un uuid' + ]; + break; + case 'mixedCase': + $validators[] = [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute) { + return \preg_match('/[a-z]/', $data[$attribute]) and + \preg_match('/[A-Z]/', $data[$attribute]); + }, + 'error' => self::translate($attribute) . ' doit contenir des lettre minuscules et majuscules' + ]; + break; + case 'containDigit': + $validators[] = [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute) { + return \preg_match('/[0-9]/', $data[$attribute]); + }, + 'error' => self::translate($attribute) . ' doit contenir au moins un chiffre' + ]; + break; + case 'containSymbol': + $validators[] = [ + 'type' => $rule, + 'validate' => function () use ($data, $attribute) { + return \preg_match('/[^\w]/', $data[$attribute]); + }, + // phpcs:ignore Generic.Files.LineLength.TooLong + 'error' => self::translate($attribute) . ' doit contenir au moins un caractère spécial comme une virgule par exemple' ]; break; case 'date': @@ -150,7 +187,7 @@ class Utils 'validate' => function () use ($data, $attribute) { return Validator::dateTime()->validate($data[$attribute]); }, - 'error' => "$attribute must be valid date" + 'error' => self::translate($attribute) . ' doit être une date valide' ]; break; case 'date_format': @@ -159,7 +196,7 @@ class Utils 'validate' => function () use ($data, $attribute, $rParams) { return Validator::dateTime($rParams[0])->validate($data[$attribute]); }, - 'error' => "$attribute must be valid date with format $rParams[0]" + 'error' => self::translate($attribute) . " doit être une date au format $rParams[0]" ]; break; case 'email': @@ -168,7 +205,7 @@ class Utils 'validate' => function () use ($data, $attribute) { return Validator::email()->validate($data[$attribute]); }, - 'error' => "$attribute must be a valid email" + 'error' => self::translate($attribute) . ' doit être un email valide' ]; break; case 'closure': @@ -178,7 +215,7 @@ class Utils $closure($attribute, $data[$attribute], $closureFail); return true; }, - 'error' => "$attribute must validate" + 'error' => self::translate($attribute) . ' doit être validé' ]; break; default: @@ -189,11 +226,7 @@ class Utils return function () use ($validators) { foreach ($validators as $validator) { if (!$validator['validate']()) { - throw new ValidationException(sprintf( - '%s : %s', - $validator['type'], - $validator['error'] - )); + throw new ValidationException($validator['error']); } } }; @@ -288,4 +321,19 @@ class Utils } return static::$packageInfos; } + + public static function translate(string $key, string $lang = 'fr'): string + { + try { + if (!array_key_exists($lang, self::$translations)) { + self::$translations = require __DIR__ . "/../resources/lang/$lang.php"; + } + } finally { + if (!isset(self::$translations['validation'][$key])) { + return $key; + } else { + return self::$translations['validation'][$key]; + } + } + } } diff --git a/src/WordPress/WPCrmQuestion.php b/src/WordPress/WPCrmQuestion.php index 0d14701..e54b8f5 100644 --- a/src/WordPress/WPCrmQuestion.php +++ b/src/WordPress/WPCrmQuestion.php @@ -39,7 +39,7 @@ abstract class WPCrmQuestion * @throws \LDC\LDCCRM\Exceptions\ApiErrorException * If any error or \LDC\LDCCRM\Enums\ApiError occurs */ - public function getAll(): array + public static function getAll(): array { return Question::all(); } @@ -64,13 +64,13 @@ abstract class WPCrmQuestion * @throws \LDC\LDCCRM\Exceptions\ApiErrorException * If any error or \LDC\LDCCRM\Enums\ApiError occurs */ - public function allWithCustomerAnswers(WPCrmUser $user): array + public static function allWithCustomerAnswers(WPCrmUser $user): array { - if (!self::getRawProperty($user, 'customer')) { + if (!self::getRawProperty($user, 'info')) { WPCrm::getUserFromApi($user->hashcode); } /** @var \LDC\LDCCRM\Models\Customer */ - $customer = self::getRawProperty($user, 'customer'); + $customer = self::getRawProperty($user, 'info'); return Question::allWithCustomerAnswers($customer); } @@ -84,13 +84,13 @@ abstract class WPCrmQuestion * @throws \LDC\LDCCRM\Exceptions\ApiErrorException * If any error or \LDC\LDCCRM\Enums\ApiError occurs */ - public function unansweredBy(WPCrmUser $user): array + public static function unansweredBy(WPCrmUser $user): array { - if (!self::getRawProperty($user, 'customer')) { + if (!self::getRawProperty($user, 'info')) { WPCrm::getUserFromApi($user->hashcode); } /** @var \LDC\LDCCRM\Models\Customer */ - $customer = self::getRawProperty($user, 'customer'); + $customer = self::getRawProperty($user, 'info'); return Question::unansweredBy($customer); }