diff --git a/src/Adapter/ClientSideValidationAdapter.php b/src/Adapter/ClientSideValidationAdapter.php index 072e33f..8c8fbf7 100644 --- a/src/Adapter/ClientSideValidationAdapter.php +++ b/src/Adapter/ClientSideValidationAdapter.php @@ -3,8 +3,9 @@ * UserFrosting (http://www.userfrosting.com) * * @link https://github.com/userfrosting/fortress - * @license https://github.com/userfrosting/fortress/blob/master/licenses/UserFrosting.md (MIT License) + * @license https://github.com/userfrosting/fortress/blob/master/LICENSE.md (MIT License) */ + namespace UserFrosting\Fortress\Adapter; use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface; @@ -26,24 +27,24 @@ abstract class ClientSideValidationAdapter /** * @var MessageTranslator - */ - protected $translator; + */ + protected $translator; /** * Create a new client-side validator. * - * @param RequestSchemaInterface $schema A RequestSchema object, containing the validation rules. - * @param MessageTranslator $translator A MessageTranslator to be used to translate message ids found in the schema. - */ + * @param RequestSchemaInterface $schema A RequestSchema object, containing the validation rules. + * @param MessageTranslator $translator A MessageTranslator to be used to translate message ids found in the schema. + */ public function __construct(RequestSchemaInterface $schema, MessageTranslator $translator) - { + { // Set schema $this->setSchema($schema); - + // Set translator $this->setTranslator($translator); } - + /** * Set the schema for this validator. * @@ -52,6 +53,7 @@ public function __construct(RequestSchemaInterface $schema, MessageTranslator $t public function setSchema(RequestSchemaInterface $schema) { $this->schema = $schema; + return $this; } @@ -59,20 +61,21 @@ public function setSchema(RequestSchemaInterface $schema) * Set the translator for this validator, as a valid MessageTranslator object. * * @param MessageTranslator $translator A MessageTranslator to be used to translate message ids found in the schema. - */ + */ public function setTranslator(MessageTranslator $translator) { $this->translator = $translator; + return $this; } - + /** * Generate and return the validation rules for this specific validation adapter. * * This method returns a collection of rules, in the format required by the specified plugin. - * @param string $format The format in which to return the rules. For example, "json" or "html5". - * @param bool $stringEncode In the case of JSON rules, specify whether or not to encode the result as a serialized JSON string. - * @return mixed The validation rule collection. - */ - abstract public function rules($format = "json", $stringEncode = true); + * @param string $format The format in which to return the rules. For example, "json" or "html5". + * @param bool $stringEncode In the case of JSON rules, specify whether or not to encode the result as a serialized JSON string. + * @return mixed The validation rule collection. + */ + abstract public function rules($format = 'json', $stringEncode = true); } diff --git a/src/Adapter/FormValidationAdapter.php b/src/Adapter/FormValidationAdapter.php index 81aa133..4cdb2a6 100644 --- a/src/Adapter/FormValidationAdapter.php +++ b/src/Adapter/FormValidationAdapter.php @@ -3,8 +3,9 @@ * UserFrosting (http://www.userfrosting.com) * * @link https://github.com/userfrosting/fortress - * @license https://github.com/userfrosting/fortress/blob/master/licenses/UserFrosting.md (MIT License) + * @license https://github.com/userfrosting/fortress/blob/master/LICENSE.md (MIT License) */ + namespace UserFrosting\Fortress\Adapter; /** @@ -17,11 +18,11 @@ class FormValidationAdapter extends ClientSideValidationAdapter { /** - * {@inheritDoc} + * {@inheritdoc} */ - public function rules($format = "json", $stringEncode = true) + public function rules($format = 'json', $stringEncode = true) { - if ($format == "html5") { + if ($format == 'html5') { return $this->formValidationRulesHtml5(); } else { return $this->formValidationRulesJson($stringEncode); @@ -32,7 +33,7 @@ public function rules($format = "json", $stringEncode = true) * Generate FormValidation compatible rules from the specified RequestSchema, as a JSON document. * See [this](http://formvalidation.io/getting-started/#calling-plugin) as an example of what this function will generate. * - * @param boolean $encode Specify whether to return a PHP array, or a JSON-encoded string. + * @param bool $encode Specify whether to return a PHP array, or a JSON-encoded string. * @return string|array Returns either the array of rules, or a JSON-encoded representation of that array. */ public function formValidationRulesJson($encode = true) @@ -51,7 +52,7 @@ public function formValidationRulesJson($encode = true) } } if ($encode) { - return json_encode($clientRules, JSON_PRETTY_PRINT|JSON_FORCE_OBJECT); + return json_encode($clientRules, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT); } else { return $clientRules; } @@ -62,30 +63,30 @@ public function formValidationRulesJson($encode = true) * See [Setting validator options via HTML attributes](http://formvalidation.io/examples/attribute/) as an example of what this function will generate. * * @return array Returns an array of rules, mapping field names -> string of data-* attributes, separated by spaces. - * Example: `data-fv-notempty data-fv-notempty-message="The gender is required"`. + * Example: `data-fv-notempty data-fv-notempty-message="The gender is required"`. */ public function formValidationRulesHtml5() { - $clientRules = array(); - $implicitRules = array(); + $clientRules = []; + $implicitRules = []; foreach ($this->schema->getSchema() as $fieldName => $field) { - $fieldRules = ""; + $fieldRules = ''; $validators = $field['validators']; foreach ($validators as $validatorName => $validator) { // Skip messages that are for server-side use only - if (isset($validator['domain']) && $validator['domain'] == "server") { + if (isset($validator['domain']) && $validator['domain'] == 'server') { continue; } // Required validator - if ($validatorName == "required") { - $prefix = "data-fv-notempty"; + if ($validatorName == 'required') { + $prefix = 'data-fv-notempty'; $fieldRules .= $this->html5Attributes($validator, $prefix); } // String length validator - if ($validatorName == "length"){ - $prefix = "data-fv-stringlength"; + if ($validatorName == 'length') { + $prefix = 'data-fv-stringlength'; $fieldRules .= $this->html5Attributes($validator, $prefix); if (isset($validator['min'])) { $fieldRules .= "$prefix-min={$validator['min']} "; @@ -95,34 +96,34 @@ public function formValidationRulesHtml5() } } // Numeric range validator - if ($validatorName == "range") { + if ($validatorName == 'range') { if (isset($validator['min']) && isset($validator['max'])) { - $prefix = "data-fv-between"; + $prefix = 'data-fv-between'; $fieldRules .= $this->html5Attributes($validator, $prefix); $fieldRules .= "$prefix-min={$validator['min']} "; $fieldRules .= "$prefix-max={$validator['max']} "; } else { if (isset($validator['min'])) { - $prefix = "data-fv-greaterthan"; + $prefix = 'data-fv-greaterthan'; $fieldRules .= $this->html5Attributes($validator, $prefix); $fieldRules .= "$prefix-value={$validator['min']} "; } if (isset($validator['max'])) { - $prefix = "data-fv-lessthan"; + $prefix = 'data-fv-lessthan'; $fieldRules .= $this->html5Attributes($validator, $prefix); $fieldRules .= "$prefix-value={$validator['max']} "; } } } // Integer validator - if ($validatorName == "integer") { - $prefix = "data-fv-integer"; + if ($validatorName == 'integer') { + $prefix = 'data-fv-integer'; $fieldRules .= $this->html5Attributes($validator, $prefix); } // Array validator - if ($validatorName == "array") { - $prefix = "data-fv-choice"; + if ($validatorName == 'array') { + $prefix = 'data-fv-choice'; $fieldRules .= $this->html5Attributes($validator, $prefix); if (isset($validator['min'])) { $fieldRules .= "$prefix-min={$validator['min']} "; @@ -132,17 +133,17 @@ public function formValidationRulesHtml5() } } // Email validator - if ($validatorName == "email") { - $prefix = "data-fv-emailaddress"; + if ($validatorName == 'email') { + $prefix = 'data-fv-emailaddress'; $fieldRules .= $this->html5Attributes($validator, $prefix); } // Match another field - if ($validatorName == "matches") { - $prefix = "data-fv-identical"; + if ($validatorName == 'matches') { + $prefix = 'data-fv-identical'; if (isset($validator['field'])) { $fieldRules .= "$prefix-field={$validator['field']} "; } else { - return null; // TODO: throw exception + return; // TODO: throw exception } $fieldRules = $this->html5Attributes($validator, $prefix); @@ -166,8 +167,8 @@ public function formValidationRulesHtml5() /** * Transform a validator for a particular field into one or more FormValidation rules. * - * @param string $fieldName - * @param string $validatorName + * @param string $fieldName + * @param string $validatorName * @param string[] $validator */ private function transformValidator($fieldName, $validatorName, array $validator) @@ -175,17 +176,17 @@ private function transformValidator($fieldName, $validatorName, array $validator $params = []; // Message if (isset($validator['message'])) { - $validator = array_merge(["self" => $fieldName], $validator); - $params["message"] = $this->translator->translate($validator['message'], $validator); + $validator = array_merge(['self' => $fieldName], $validator); + $params['message'] = $this->translator->translate($validator['message'], $validator); } $transformedValidatorJson = []; switch ($validatorName) { // Required validator - case "required": + case 'required': $transformedValidatorJson['notEmpty'] = $params; break; - case "length": + case 'length': if (isset($validator['min'])) { $params['min'] = $validator['min']; } @@ -194,13 +195,13 @@ private function transformValidator($fieldName, $validatorName, array $validator } $transformedValidatorJson['stringLength'] = $params; break; - case "integer": + case 'integer': $transformedValidatorJson['integer'] = $params; break; - case "numeric": + case 'numeric': $transformedValidatorJson['numeric'] = $params; break; - case "range": + case 'range': if (isset($validator['min'])) { $params['min'] = $validator['min']; } @@ -215,7 +216,7 @@ private function transformValidator($fieldName, $validatorName, array $validator $transformedValidatorJson['lessThan'] = $params; } break; - case "array": + case 'array': if (isset($validator['min'])) { $params['min'] = $validator['min']; } @@ -224,36 +225,37 @@ private function transformValidator($fieldName, $validatorName, array $validator } $transformedValidatorJson['choice'] = $params; break; - case "email": + case 'email': $transformedValidatorJson['emailAddress'] = $params; break; - case "matches": + case 'matches': if (isset($validator['field'])) { $params['field'] = $validator['field']; } $transformedValidatorJson['identical'] = $params; break; - case "not_matches": + case 'not_matches': if (isset($validator['field'])) { $params['field'] = $validator['field']; } $transformedValidatorJson['different'] = $params; break; - case "member_of": + case 'member_of': if (isset($validator['values'])) { - $params['regexp'] = "^" . implode("|", $validator['values']) . "$"; + $params['regexp'] = '^' . implode('|', $validator['values']) . '$'; } $transformedValidatorJson['regexp'] = $params; break; - case "not_member_of": + case 'not_member_of': if (isset($validator['values'])) { - $params['regexp'] = "^(?!" . implode("|", $validator['values']) . "$).*$"; + $params['regexp'] = '^(?!' . implode('|', $validator['values']) . '$).*$'; } $transformedValidatorJson['regexp'] = $params; break; default: break; } + return $transformedValidatorJson; } @@ -261,13 +263,13 @@ private function transformValidator($fieldName, $validatorName, array $validator * Transform a validator for a particular field into a string of FormValidation rules as HTML data-* attributes. * * @param string[] $validator - * @param string $prefix + * @param string $prefix */ public function html5Attributes($validator, $prefix) { $attr = "$prefix=true "; if (isset($validator['message'])) { - $msg = ""; + $msg = ''; if (isset($validator['message'])) { $msg = $validator['message']; } else { @@ -275,6 +277,7 @@ public function html5Attributes($validator, $prefix) } $attr .= "$prefix-message=\"$msg\" "; } + return $attr; } } diff --git a/src/Adapter/JqueryValidationAdapter.php b/src/Adapter/JqueryValidationAdapter.php index 20b43be..d13b922 100644 --- a/src/Adapter/JqueryValidationAdapter.php +++ b/src/Adapter/JqueryValidationAdapter.php @@ -1,11 +1,11 @@ schema->all() as $fieldNameO => $field) { $fieldNameOnly = $fieldNameO; if ($arrayPrefix != '') { - $fieldName = $arrayPrefix . "[" . $fieldNameO . "]"; + $fieldName = $arrayPrefix . '[' . $fieldNameO . ']'; } else { $fieldName = $fieldNameO; } @@ -67,7 +67,7 @@ public function rules($format = "json", $stringEncode = false, $arrayPrefix = '' } } $result = [ - 'rules' => $clientRules, + 'rules' => $clientRules, 'messages' => $clientMessages ]; @@ -81,8 +81,8 @@ public function rules($format = "json", $stringEncode = false, $arrayPrefix = '' /** * Transform a validator for a particular field into one or more jQueryValidation rules. * - * @param string $fieldName - * @param string $validatorName + * @param string $fieldName + * @param string $validatorName * @param string[] $validator */ private function transformValidator($fieldName, $validatorName, array $validator) @@ -90,18 +90,18 @@ private function transformValidator($fieldName, $validatorName, array $validator $transformedValidatorJson = []; switch ($validatorName) { // Required validator - case "email": + case 'email': $transformedValidatorJson['email'] = true; break; - case "equals": + case 'equals': if (isset($validator['value'])) { $transformedValidatorJson['equals'] = $validator; } break; - case "integer": + case 'integer': $transformedValidatorJson['digits'] = true; break; - case "length": + case 'length': if (isset($validator['min']) && isset($validator['max'])) { $transformedValidatorJson['rangelength'] = [ $validator['min'], @@ -113,41 +113,41 @@ private function transformValidator($fieldName, $validatorName, array $validator $transformedValidatorJson['maxlength'] = $validator['max']; } break; - case "matches": + case 'matches': if (isset($validator['field'])) { $transformedValidatorJson['matchFormField'] = $validator['field']; } break; - case "member_of": + case 'member_of': if (isset($validator['values'])) { $transformedValidatorJson['memberOf'] = $validator['values']; } break; - case "no_leading_whitespace": + case 'no_leading_whitespace': $transformedValidatorJson['noLeadingWhitespace'] = true; break; - case "no_trailing_whitespace": + case 'no_trailing_whitespace': $transformedValidatorJson['noTrailingWhitespace'] = true; break; - case "not_equals": + case 'not_equals': if (isset($validator['value'])) { $transformedValidatorJson['notEquals'] = $validator; } break; - case "not_matches": + case 'not_matches': if (isset($validator['field'])) { $transformedValidatorJson['notMatchFormField'] = $validator['field']; } break; - case "not_member_of": + case 'not_member_of': if (isset($validator['values'])) { $transformedValidatorJson['notMemberOf'] = $validator['values']; } break; - case "numeric": + case 'numeric': $transformedValidatorJson['number'] = true; break; - case "range": + case 'range': if (isset($validator['min']) && isset($validator['max'])) { $transformedValidatorJson['range'] = [ $validator['min'], @@ -159,24 +159,25 @@ private function transformValidator($fieldName, $validatorName, array $validator $transformedValidatorJson['max'] = $validator['max']; } break; - case "regex": + case 'regex': $transformedValidatorJson['pattern'] = $validator['regex']; break; - case "required": + case 'required': $transformedValidatorJson['required'] = true; break; - case "telephone": + case 'telephone': $transformedValidatorJson['phoneUS'] = true; break; - case "uri": + case 'uri': $transformedValidatorJson['url'] = true; break; - case "username": + case 'username': $transformedValidatorJson['username'] = true; break; default: break; } + return $transformedValidatorJson; } } diff --git a/src/RequestDataTransformer.php b/src/RequestDataTransformer.php index a7454df..5059332 100644 --- a/src/RequestDataTransformer.php +++ b/src/RequestDataTransformer.php @@ -3,8 +3,9 @@ * UserFrosting (http://www.userfrosting.com) * * @link https://github.com/userfrosting/fortress - * @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License) + * @license https://github.com/userfrosting/fortress/blob/master/LICENSE.md (MIT License) */ + namespace UserFrosting\Fortress; use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface; @@ -53,11 +54,12 @@ public function __construct(RequestSchemaInterface $schema) public function setSchema(RequestSchemaInterface $schema) { $this->schema = $schema; + return $this; } /** - * {@inheritDoc} + * {@inheritdoc} */ public function transform(array $data, $onUnexpectedVar = 'skip') { @@ -69,12 +71,12 @@ public function transform(array $data, $onUnexpectedVar = 'skip') // Handle values not listed in the schema if (!array_key_exists($name, $schemaFields)) { switch ($onUnexpectedVar) { - case 'allow' : $transformedData[$name] = $value; break; - case 'error' : + case 'allow': $transformedData[$name] = $value; break; + case 'error': $e = new BadRequestException("The field '$name' is not a valid input field."); throw $e; break; - case 'skip' : default: continue; + case 'skip': default: continue; } } else { $transformedData[$name] = $this->transformField($name, $value); @@ -94,7 +96,7 @@ public function transform(array $data, $onUnexpectedVar = 'skip') } /** - * {@inheritDoc} + * {@inheritdoc} */ public function transformField($name, $value) { @@ -112,8 +114,8 @@ public function transformField($name, $value) switch (strtolower($transformation)) { case 'purify': $transformedValue = $this->purifier->purify($transformedValue); break; case 'escape': $transformedValue = $this->escapeHtmlCharacters($transformedValue); break; - case 'purge' : $transformedValue = $this->purgeHtmlCharacters($transformedValue); break; - case 'trim' : $transformedValue = $this->trim($transformedValue); break; + case 'purge': $transformedValue = $this->purgeHtmlCharacters($transformedValue); break; + case 'trim': $transformedValue = $this->trim($transformedValue); break; default: break; } } @@ -125,7 +127,7 @@ public function transformField($name, $value) /** * Autodetect if a field is an array or scalar, and filter appropriately. * - * @param mixed $value + * @param mixed $value * @return mixed */ private function escapeHtmlCharacters($value) @@ -140,7 +142,7 @@ private function escapeHtmlCharacters($value) /** * Autodetect if a field is an array or scalar, and filter appropriately. * - * @param mixed $value + * @param mixed $value * @return mixed */ private function purgeHtmlCharacters($value) @@ -155,7 +157,7 @@ private function purgeHtmlCharacters($value) /** * Autodetect if a field is an array or scalar, and filter appropriately. * - * @param mixed $value + * @param mixed $value * @return mixed */ private function trim($value) diff --git a/src/RequestDataTransformerInterface.php b/src/RequestDataTransformerInterface.php index 8c6b0ba..d0e4e56 100644 --- a/src/RequestDataTransformerInterface.php +++ b/src/RequestDataTransformerInterface.php @@ -3,8 +3,9 @@ * UserFrosting (http://www.userfrosting.com) * * @link https://github.com/userfrosting/fortress - * @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License) + * @license https://github.com/userfrosting/fortress/blob/master/LICENSE.md (MIT License) */ + namespace UserFrosting\Fortress; use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface; @@ -31,20 +32,20 @@ public function setSchema(RequestSchemaInterface $schema); * Example transformations: escape/purge/purify HTML entities * Also, set any default values for unspecified fields. * - * @param array $data The array of data to be transformed. - * @param string $onUnexpectedVar[optional] Determines what to do when a field is encountered that is not in the schema. Set to one of: - * "allow": Treat the field as any other, allowing the value through. - * "error": Raise an exception. - * "skip" (default): Quietly ignore the field. It will not be part of the transformed data array. - * @return array The array of transformed data, mapping field names => values. + * @param array $data The array of data to be transformed. + * @param string $onUnexpectedVar[optional] Determines what to do when a field is encountered that is not in the schema. Set to one of: + * "allow": Treat the field as any other, allowing the value through. + * "error": Raise an exception. + * "skip" (default): Quietly ignore the field. It will not be part of the transformed data array. + * @return array The array of transformed data, mapping field names => values. */ public function transform(array $data, $onUnexpectedVar); /** * Transform a raw field value. * - * @param string $name The name of the field to transform, as specified in the schema. - * @param string $value The value to be transformed. + * @param string $name The name of the field to transform, as specified in the schema. + * @param string $value The value to be transformed. * @return string The transformed value. */ public function transformField($name, $value); diff --git a/src/RequestSchema.php b/src/RequestSchema.php index 77d5984..964a347 100644 --- a/src/RequestSchema.php +++ b/src/RequestSchema.php @@ -3,8 +3,9 @@ * UserFrosting (http://www.userfrosting.com) * * @link https://github.com/userfrosting/fortress - * @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License) + * @license https://github.com/userfrosting/fortress/blob/master/LICENSE.md (MIT License) */ + namespace UserFrosting\Fortress; use UserFrosting\Fortress\RequestSchema\RequestSchemaRepository; @@ -51,7 +52,7 @@ public function getSchema() /** * @deprecated since 4.1 - * @param string $path Path to the schema file. + * @param string $path Path to the schema file. * @throws Exception The file does not exist or is not a valid format. */ public function loadSchema() diff --git a/src/RequestSchema/RequestSchemaInterface.php b/src/RequestSchema/RequestSchemaInterface.php index c40b307..f6cb56d 100644 --- a/src/RequestSchema/RequestSchemaInterface.php +++ b/src/RequestSchema/RequestSchemaInterface.php @@ -3,8 +3,9 @@ * UserFrosting (http://www.userfrosting.com) * * @link https://github.com/userfrosting/fortress - * @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License) + * @license https://github.com/userfrosting/fortress/blob/master/LICENSE.md (MIT License) */ + namespace UserFrosting\Fortress\RequestSchema; /** @@ -28,16 +29,16 @@ public function all(); * If a key IS specified, items will be merged into that key. * Nested keys may be specified using dot syntax. * @param string|null $key - * @param mixed $items + * @param mixed $items */ - public function mergeItems($key = null, $items); + public function mergeItems($key, $items); /** * Set the default value for a specified field. * * If the specified field does not exist in the schema, add it. If a default already exists for this field, replace it with the value specified here. - * @param string $field The name of the field (e.g., "user_name") - * @param string $value The new default value for this field. + * @param string $field The name of the field (e.g., "user_name") + * @param string $value The new default value for this field. * @return RequestSchemaInterface This schema object. */ public function setDefault($field, $value); @@ -47,9 +48,9 @@ public function setDefault($field, $value); * * If the specified field does not exist in the schema, add it. If a validator with the specified name already exists for the field, * replace it with the parameters specified here. - * @param string $field The name of the field for this validator (e.g., "user_name") - * @param string $validatorName A validator rule, as specified in https://github.com/alexweissman/wdvss (e.g. "length") - * @param array $parameters An array of parameters, hashed as parameter_name => parameter value (e.g. [ "min" => 50 ]) + * @param string $field The name of the field for this validator (e.g., "user_name") + * @param string $validatorName A validator rule, as specified in https://github.com/alexweissman/wdvss (e.g. "length") + * @param array $parameters An array of parameters, hashed as parameter_name => parameter value (e.g. [ "min" => 50 ]) * @return RequestSchemaInterface This schema object. */ public function addValidator($field, $validatorName, array $parameters = []); @@ -57,8 +58,8 @@ public function addValidator($field, $validatorName, array $parameters = []); /** * Remove a validator for a specified field. * - * @param string $field The name of the field for this validator (e.g., "user_name") - * @param string $validatorName A validator rule, as specified in https://github.com/alexweissman/wdvss (e.g. "length") + * @param string $field The name of the field for this validator (e.g., "user_name") + * @param string $validatorName A validator rule, as specified in https://github.com/alexweissman/wdvss (e.g. "length") * @return RequestSchemaInterface This schema object. */ public function removeValidator($field, $validatorName); @@ -67,8 +68,8 @@ public function removeValidator($field, $validatorName); * Set a sequence of transformations for a specified field. * * If the specified field does not exist in the schema, add it. - * @param string $field The name of the field for this transformation (e.g., "user_name") - * @param string|array $transformations An array of transformations, as specified in https://github.com/alexweissman/wdvss (e.g. "purge") + * @param string $field The name of the field for this transformation (e.g., "user_name") + * @param string|array $transformations An array of transformations, as specified in https://github.com/alexweissman/wdvss (e.g. "purge") * @return RequestSchemaInterface This schema object. */ public function setTransformations($field, $transformations = []); diff --git a/src/RequestSchema/RequestSchemaRepository.php b/src/RequestSchema/RequestSchemaRepository.php index 7870faa..8380d10 100644 --- a/src/RequestSchema/RequestSchemaRepository.php +++ b/src/RequestSchema/RequestSchemaRepository.php @@ -3,11 +3,11 @@ * UserFrosting (http://www.userfrosting.com) * * @link https://github.com/userfrosting/fortress - * @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License) + * @license https://github.com/userfrosting/fortress/blob/master/LICENSE.md (MIT License) */ + namespace UserFrosting\Fortress\RequestSchema; -use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface; use UserFrosting\Support\Repository\Repository; /** @@ -18,7 +18,7 @@ class RequestSchemaRepository extends Repository implements RequestSchemaInterface { /** - * {@inheritDoc} + * {@inheritdoc} */ public function setDefault($field, $value) { @@ -32,7 +32,7 @@ public function setDefault($field, $value) } /** - * {@inheritDoc} + * {@inheritdoc} */ public function addValidator($field, $validatorName, array $parameters = []) { @@ -50,7 +50,7 @@ public function addValidator($field, $validatorName, array $parameters = []) } /** - * {@inheritDoc} + * {@inheritdoc} */ public function removeValidator($field, $validatorName) { @@ -60,12 +60,12 @@ public function removeValidator($field, $validatorName) } /** - * {@inheritDoc} + * {@inheritdoc} */ public function setTransformations($field, $transformations = []) { if (!is_array($transformations)) { - $transformations = array($transformations); + $transformations = [$transformations]; } if (!isset($this->items[$field])) { diff --git a/src/ServerSideValidator.php b/src/ServerSideValidator.php index e5f2013..3108a67 100644 --- a/src/ServerSideValidator.php +++ b/src/ServerSideValidator.php @@ -3,8 +3,9 @@ * UserFrosting (http://www.userfrosting.com) * * @link https://github.com/userfrosting/fortress - * @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License) + * @license https://github.com/userfrosting/fortress/blob/master/LICENSE.md (MIT License) */ + namespace UserFrosting\Fortress; use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface; @@ -32,8 +33,8 @@ class ServerSideValidator extends Validator implements ServerSideValidatorInterf /** Create a new server-side validator. * - * @param RequestSchemaInterface $schema A RequestSchemaInterface object, containing the validation rules. - * @param MessageTranslator $translator A MessageTranslator to be used to translate message ids found in the schema. + * @param RequestSchemaInterface $schema A RequestSchemaInterface object, containing the validation rules. + * @param MessageTranslator $translator A MessageTranslator to be used to translate message ids found in the schema. */ public function __construct(RequestSchemaInterface $schema, MessageTranslator $translator) { @@ -49,7 +50,7 @@ public function __construct(RequestSchemaInterface $schema, MessageTranslator $t } /** - * {@inheritDoc} + * {@inheritdoc} */ public function setSchema(RequestSchemaInterface $schema) { @@ -57,7 +58,7 @@ public function setSchema(RequestSchemaInterface $schema) } /** - * {@inheritDoc} + * {@inheritdoc} */ public function setTranslator(MessageTranslator $translator) { @@ -65,7 +66,7 @@ public function setTranslator(MessageTranslator $translator) } /** - * {@inheritDoc} + * {@inheritdoc} */ public function validate(array $data = []) { @@ -75,7 +76,7 @@ public function validate(array $data = []) } /** - * {@inheritDoc} + * {@inheritdoc} * * We expose this method to the public interface for testing purposes. */ @@ -90,7 +91,7 @@ public function hasRule($name, $field) * @param string $field * @param mixed $value * @param string $targetValue - * @param bool $caseSensitive + * @param bool $caseSensitive * @return bool */ protected function validateEqualsValue($field, $value, $params) @@ -112,7 +113,7 @@ protected function validateEqualsValue($field, $value, $params) * @param string $field * @param mixed $value * @param string $targetValue - * @param bool $caseSensitive + * @param bool $caseSensitive * @return bool */ protected function validateNotEqualsValue($field, $value, $params) @@ -144,6 +145,7 @@ protected function validateNotEqualsValue($field, $value, $params) protected function validatePhoneUS($field, $value) { $value = preg_replace('/\s+/', '', $value); + return (strlen($value) > 9) && preg_match('/^(\+?1-?)?(\([2-9]([02-9]\d|1[02-9])\)|[2-9]([02-9]\d|1[02-9]))-?[2-9]([02-9]\d|1[02-9])-?\d{4}$/', $value); } @@ -163,14 +165,14 @@ protected function validateUsername($field, $value) /** * Add a rule to the validator, along with a specified error message if that rule is failed by the data. * - * @param string $rule The name of the validation rule. + * @param string $rule The name of the validation rule. * @param string $messageSet The message to display when validation against this rule fails. */ private function ruleWithMessage($rule, $messageSet) { // Weird way to adapt with Valitron's funky interface $params = array_merge([$rule], array_slice(func_get_args(), 2)); - call_user_func_array([$this,"rule"], $params); + call_user_func_array([$this, 'rule'], $params); // Set message. Use Valitron's default message if not specified in the schema. if (!$messageSet) { $messageSet = "'" . $params[1] . "' " . vsprintf(static::$_ruleMessages[$rule], array_slice(func_get_args(), 3)); @@ -191,108 +193,108 @@ private function generateSchemaRules() $validators = $field['validators']; foreach ($validators as $validatorName => $validator) { // Skip messages that are for client-side use only - if (isset($validator['domain']) && $validator['domain'] == "client") { + if (isset($validator['domain']) && $validator['domain'] == 'client') { continue; } // Generate translated message if (isset($validator['message'])) { - $params = array_merge(["self" => $fieldName], $validator); + $params = array_merge(['self' => $fieldName], $validator); $messageSet = $this->translator->translate($validator['message'], $params); } else { $messageSet = null; } // Array validator - if ($validatorName == "array") { + if ($validatorName == 'array') { // For now, just check that it is an array. Really we need a new validation rule here. - $this->ruleWithMessage("array", $messageSet, $fieldName); + $this->ruleWithMessage('array', $messageSet, $fieldName); } // Email validator - if ($validatorName == "email") { - $this->ruleWithMessage("email", $messageSet, $fieldName); + if ($validatorName == 'email') { + $this->ruleWithMessage('email', $messageSet, $fieldName); } // Equals validator - if ($validatorName == "equals") { - $this->ruleWithMessage("equalsValue", $messageSet, $fieldName, $validator['value'], $validator['caseSensitive']); + if ($validatorName == 'equals') { + $this->ruleWithMessage('equalsValue', $messageSet, $fieldName, $validator['value'], $validator['caseSensitive']); } // Integer validator - if ($validatorName == "integer") { - $this->ruleWithMessage("integer", $messageSet, $fieldName); + if ($validatorName == 'integer') { + $this->ruleWithMessage('integer', $messageSet, $fieldName); } // String length validator - if ($validatorName == "length") { + if ($validatorName == 'length') { if (isset($validator['min']) && isset($validator['max'])) { - $this->ruleWithMessage("lengthBetween", $messageSet, $fieldName, $validator['min'], $validator['max']); + $this->ruleWithMessage('lengthBetween', $messageSet, $fieldName, $validator['min'], $validator['max']); } else { if (isset($validator['min'])) { - $this->ruleWithMessage("lengthMin", $messageSet, $fieldName, $validator['min']); + $this->ruleWithMessage('lengthMin', $messageSet, $fieldName, $validator['min']); } if (isset($validator['max'])) { - $this->ruleWithMessage("lengthMax", $messageSet, $fieldName, $validator['max']); + $this->ruleWithMessage('lengthMax', $messageSet, $fieldName, $validator['max']); } } } // Match another field - if ($validatorName == "matches") { - $this->ruleWithMessage("equals", $messageSet, $fieldName, $validator['field']); + if ($validatorName == 'matches') { + $this->ruleWithMessage('equals', $messageSet, $fieldName, $validator['field']); } // Check membership in array - if ($validatorName == "member_of") { - $this->ruleWithMessage("in", $messageSet, $fieldName, $validator['values'], true); // Strict comparison + if ($validatorName == 'member_of') { + $this->ruleWithMessage('in', $messageSet, $fieldName, $validator['values'], true); // Strict comparison } // No leading whitespace - if ($validatorName == "no_leading_whitespace") { - $this->ruleWithMessage("regex", $messageSet, $fieldName, "/^\S.*$/"); + if ($validatorName == 'no_leading_whitespace') { + $this->ruleWithMessage('regex', $messageSet, $fieldName, "/^\S.*$/"); } // No trailing whitespace - if ($validatorName == "no_trailing_whitespace") { - $this->ruleWithMessage("regex", $messageSet, $fieldName, "/^.*\S$/"); + if ($validatorName == 'no_trailing_whitespace') { + $this->ruleWithMessage('regex', $messageSet, $fieldName, "/^.*\S$/"); } // Negation of equals validator - if ($validatorName == "not_equals") { - $this->ruleWithMessage("notEqualsValue", $messageSet, $fieldName, $validator['value'], $validator['caseSensitive']); + if ($validatorName == 'not_equals') { + $this->ruleWithMessage('notEqualsValue', $messageSet, $fieldName, $validator['value'], $validator['caseSensitive']); } // Negation of match another field - if ($validatorName == "not_matches") { - $this->ruleWithMessage("different", $messageSet, $fieldName, $validator['field']); + if ($validatorName == 'not_matches') { + $this->ruleWithMessage('different', $messageSet, $fieldName, $validator['field']); } // Negation of membership - if ($validatorName == "not_member_of") { - $this->ruleWithMessage("notIn", $messageSet, $fieldName, $validator['values'], true); // Strict comparison + if ($validatorName == 'not_member_of') { + $this->ruleWithMessage('notIn', $messageSet, $fieldName, $validator['values'], true); // Strict comparison } // Numeric validator - if ($validatorName == "numeric") { - $this->ruleWithMessage("numeric", $messageSet, $fieldName); + if ($validatorName == 'numeric') { + $this->ruleWithMessage('numeric', $messageSet, $fieldName); } // Numeric range validator - if ($validatorName == "range") { + if ($validatorName == 'range') { if (isset($validator['min'])) { - $this->ruleWithMessage("min", $messageSet, $fieldName, $validator['min']); + $this->ruleWithMessage('min', $messageSet, $fieldName, $validator['min']); } if (isset($validator['max'])) { - $this->ruleWithMessage("max", $messageSet, $fieldName, $validator['max']); + $this->ruleWithMessage('max', $messageSet, $fieldName, $validator['max']); } } // Regex validator - if ($validatorName == "regex") { - $this->ruleWithMessage("regex", $messageSet, $fieldName, "/" . $validator['regex'] . "/"); + if ($validatorName == 'regex') { + $this->ruleWithMessage('regex', $messageSet, $fieldName, '/' . $validator['regex'] . '/'); } // Required validator - if ($validatorName == "required") { - $this->ruleWithMessage("required", $messageSet, $fieldName); + if ($validatorName == 'required') { + $this->ruleWithMessage('required', $messageSet, $fieldName); } // Phone validator - if ($validatorName == "telephone") { - $this->ruleWithMessage("phoneUS", $messageSet, $fieldName); + if ($validatorName == 'telephone') { + $this->ruleWithMessage('phoneUS', $messageSet, $fieldName); } // URI validator - if ($validatorName == "uri") { - $this->ruleWithMessage("url", $messageSet, $fieldName); + if ($validatorName == 'uri') { + $this->ruleWithMessage('url', $messageSet, $fieldName); } // Username - if ($validatorName == "username") { - $this->ruleWithMessage("username", $messageSet, $fieldName); + if ($validatorName == 'username') { + $this->ruleWithMessage('username', $messageSet, $fieldName); } } } diff --git a/src/ServerSideValidatorInterface.php b/src/ServerSideValidatorInterface.php index 1b31009..5b7fe10 100644 --- a/src/ServerSideValidatorInterface.php +++ b/src/ServerSideValidatorInterface.php @@ -3,8 +3,9 @@ * UserFrosting (http://www.userfrosting.com) * * @link https://github.com/userfrosting/fortress - * @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License) + * @license https://github.com/userfrosting/fortress/blob/master/LICENSE.md (MIT License) */ + namespace UserFrosting\Fortress; use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface; @@ -36,8 +37,8 @@ public function setTranslator(MessageTranslator $translator); /** * Validate the specified data against the schema rules. * - * @param array $data An array of data, mapping field names to field values. - * @return boolean True if the data was successfully validated, false otherwise. + * @param array $data An array of data, mapping field names to field values. + * @return bool True if the data was successfully validated, false otherwise. */ public function validate(array $data);