Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

Commit

Permalink
Applied style fix from PHP-CS-Fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Nov 14, 2018
1 parent 7074a78 commit 74617cf
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 187 deletions.
35 changes: 19 additions & 16 deletions src/Adapter/ClientSideValidationAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -52,27 +53,29 @@ public function __construct(RequestSchemaInterface $schema, MessageTranslator $t
public function setSchema(RequestSchemaInterface $schema)
{
$this->schema = $schema;

return $this;
}

/**
* 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);
}
97 changes: 50 additions & 47 deletions src/Adapter/FormValidationAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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;
}
Expand All @@ -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']} ";
Expand All @@ -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']} ";
Expand All @@ -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);
Expand All @@ -166,26 +167,26 @@ 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)
{
$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'];
}
Expand All @@ -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'];
}
Expand All @@ -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'];
}
Expand All @@ -224,57 +225,59 @@ 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;
}

/**
* 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 {
return $attr;
}
$attr .= "$prefix-message=\"$msg\" ";
}

return $attr;
}
}
Loading

0 comments on commit 74617cf

Please sign in to comment.