Skip to content

Commit

Permalink
Using PSR-2 coding style for this source code
Browse files Browse the repository at this point in the history
  • Loading branch information
peter279k committed Nov 17, 2018
1 parent 064f3ac commit 91ff927
Show file tree
Hide file tree
Showing 80 changed files with 159 additions and 211 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ php:

before_script:
- composer install
- ./vendor/bin/phpcs -n --standard=PSR2 src/ tests/

script: phpunit --coverage-text
script:
- phpunit --coverage-text
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
"phpunit/phpunit": "^6.5",
"squizlabs/php_codesniffer": "^3"
}
}
7 changes: 3 additions & 4 deletions src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(Validation $validation, $key, $alias = null, array $
$this->validation = $validation;
$this->alias = $alias;
$this->key = $key;
foreach($rules as $rule) {
foreach ($rules as $rule) {
$this->addRule($rule);
}
}
Expand All @@ -49,7 +49,7 @@ public function getPrimaryAttribute()
public function setOtherAttributes(array $otherAttributes)
{
$this->otherAttributes = [];
foreach($otherAttributes as $otherAttribute) {
foreach ($otherAttributes as $otherAttribute) {
$this->addOtherAttribute($otherAttribute);
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ public function getHumanizedKey()
// Resolve key from array validation
if ($primaryAttribute) {
$split = explode('.', $key);
$key = implode(' ', array_map(function($word) {
$key = implode(' ', array_map(function ($word) {
if (is_numeric($word)) {
$word = $word + 1;
}
Expand All @@ -169,5 +169,4 @@ public function getAlias()
{
return $this->alias;
}

}
23 changes: 13 additions & 10 deletions src/ErrorBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function has($key)
if (!$ruleName) {
return !empty($messages);
} else {
return !empty($messages) AND isset($messages[$ruleName]);
return !empty($messages) and isset($messages[$ruleName]);
}
}
}
Expand Down Expand Up @@ -71,15 +71,17 @@ public function get($key, $format = ':message')
$results = [];
if ($this->isWildcardKey($key)) {
$messages = $this->filterMessagesForWildcardKey($key, $ruleName);
foreach($messages as $explicitKey => $keyMessages) {
foreach ($messages as $explicitKey => $keyMessages) {
foreach ($keyMessages as $rule => $message) {
$results[$explicitKey][$rule] = $this->formatMessage($message, $format);
}
}
} else {
$keyMessages = isset($this->messages[$key])? $this->messages[$key] : [];
foreach($keyMessages as $rule => $message) {
if ($ruleName AND $ruleName != $rule) continue;
foreach ($keyMessages as $rule => $message) {
if ($ruleName and $ruleName != $rule) {
continue;
}
$results[$rule] = $this->formatMessage($message, $format);
}
}
Expand All @@ -91,8 +93,8 @@ public function all($format = ':message')
{
$messages = $this->messages;
$results = [];
foreach($messages as $key => $keyMessages) {
foreach($keyMessages as $message) {
foreach ($messages as $key => $keyMessages) {
foreach ($keyMessages as $message) {
$results[] = $this->formatMessage($message, $format);
}
}
Expand All @@ -103,7 +105,7 @@ public function firstOfAll($format = ':message', $dotNotation = false)
{
$messages = $this->messages;
$results = [];
foreach($messages as $key => $keyMessages) {
foreach ($messages as $key => $keyMessages) {
if ($dotNotation) {
$results[$key] = $this->formatMessage(array_shift($messages[$key]), $format);
} else {
Expand Down Expand Up @@ -144,8 +146,10 @@ protected function filterMessagesForWildcardKey($key, $ruleName = null)
continue;
}

foreach($keyMessages as $rule => $message) {
if ($ruleName AND $rule != $ruleName) continue;
foreach ($keyMessages as $rule => $message) {
if ($ruleName and $rule != $ruleName) {
continue;
}
$filteredMessages[$k][$rule] = $message;
}
}
Expand All @@ -157,5 +161,4 @@ protected function formatMessage($message, $format)
{
return str_replace(':message', $message, $format);
}

}
1 change: 0 additions & 1 deletion src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,4 @@ public static function snakeCase($value, $delimiter = '_')

return $value;
}

}
1 change: 0 additions & 1 deletion src/MimeTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,5 +788,4 @@ public function getMimeType($extension)
$key = array_search($extension, $this->mimeTypes);
return $key ?: null;
}

}
11 changes: 6 additions & 5 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class Rule

protected $params = [];

protected $fillable_params = [];
protected $fillableParams = [];

protected $message = "The :attribute is invalid";

Expand Down Expand Up @@ -66,8 +66,10 @@ public function setParameter($key, $value)

public function fillParameters(array $params)
{
foreach($this->fillable_params as $key) {
if (empty($params)) break;
foreach ($this->fillableParams as $key) {
if (empty($params)) {
break;
}
$this->params[$key] = array_shift($params);
}
return $this;
Expand Down Expand Up @@ -101,12 +103,11 @@ public function getMessage()

protected function requireParameters(array $params)
{
foreach($params as $param) {
foreach ($params as $param) {
if (!isset($this->params[$param])) {
$rule = $this->getKey();
throw new MissingRequiredParameterException("Missing required parameter '{$param}' on rule '{$rule}'");
}
}
}

}
1 change: 0 additions & 1 deletion src/Rules/Accepted.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ public function check($value)
$acceptables = ['yes', 'on', '1', 1, true, 'true'];
return in_array($value, $acceptables, true);
}

}
6 changes: 3 additions & 3 deletions src/Rules/After.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class After extends Rule

protected $message = "The :attribute must be a date after :time.";

protected $fillable_params = ['time'];
protected $fillableParams = ['time'];

public function check($value)
{
$this->requireParameters($this->fillable_params);
$this->requireParameters($this->fillableParams);
$time = $this->parameter('time');

if (!$this->isValidDate($value)){
if (!$this->isValidDate($value)) {
throw $this->throwException($value);
}

Expand Down
1 change: 0 additions & 1 deletion src/Rules/Alpha.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ public function check($value)
{
return is_string($value) && preg_match('/^[\pL\pM]+$/u', $value);
}

}
1 change: 0 additions & 1 deletion src/Rules/AlphaDash.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ public function check($value)

return preg_match('/^[\pL\pM\pN_-]+$/u', $value) > 0;
}

}
1 change: 0 additions & 1 deletion src/Rules/AlphaNum.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ public function check($value)

return preg_match('/^[\pL\pM\pN]+$/u', $value) > 0;
}

}
6 changes: 3 additions & 3 deletions src/Rules/Before.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class Before extends Rule

protected $message = "The :attribute must be a date before :time.";

protected $fillable_params = ['time'];
protected $fillableParams = ['time'];

public function check($value)
{
$this->requireParameters($this->fillable_params);
$this->requireParameters($this->fillableParams);
$time = $this->parameter('time');

if (!$this->isValidDate($value)){
if (!$this->isValidDate($value)) {
throw $this->throwException($value);
}

Expand Down
15 changes: 7 additions & 8 deletions src/Rules/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@ class Between extends Rule

protected $message = "The :attribute must be between :min and :max";

protected $fillable_params = ['min', 'max'];
protected $fillableParams = ['min', 'max'];

public function check($value)
{
$this->requireParameters($this->fillable_params);
$this->requireParameters($this->fillableParams);

$min = (int) $this->parameter('min');
$max = (int) $this->parameter('max');

if (is_int($value) || is_float($value)) {
return $value >= $min AND $value <= $max;
} elseif(is_string($value)) {
return mb_strlen($value, 'UTF-8') >= $min AND mb_strlen($value, 'UTF-8') <= $max;
} elseif(is_array($value)) {
return count($value) >= $min AND count($value) <= $max;
return $value >= $min and $value <= $max;
} elseif (is_string($value)) {
return mb_strlen($value, 'UTF-8') >= $min and mb_strlen($value, 'UTF-8') <= $max;
} elseif (is_array($value)) {
return count($value) >= $min and count($value) <= $max;
} else {
return false;
}
}

}
7 changes: 3 additions & 4 deletions src/Rules/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Callback extends Rule

protected $message = "The :attribute is not valid";

protected $fillable_params = ['callback'];
protected $fillableParams = ['callback'];

public function setCallback(Closure $callback)
{
Expand All @@ -20,7 +20,7 @@ public function setCallback(Closure $callback)

public function check($value)
{
$this->requireParameters($this->fillable_params);
$this->requireParameters($this->fillableParams);

$callback = $this->parameter('callback');
if (false === $callback instanceof Closure) {
Expand All @@ -34,11 +34,10 @@ public function check($value)
if (is_string($invalidMessage)) {
$this->setMessage($invalidMessage);
return false;
} elseif(false === $invalidMessage) {
} elseif (false === $invalidMessage) {
return false;
}

return true;
}

}
5 changes: 2 additions & 3 deletions src/Rules/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ class Date extends Rule

protected $message = "The :attribute is not valid date format";

protected $fillable_params = ['format'];
protected $fillableParams = ['format'];

protected $params = [
'format' => 'Y-m-d'
];

public function check($value)
{
$this->requireParameters($this->fillable_params);
$this->requireParameters($this->fillableParams);

$format = $this->parameter('format');
return date_create_from_format($format, $value) !== false;
}

}
5 changes: 2 additions & 3 deletions src/Rules/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ class Defaults extends Rule

protected $message = "The :attribute default is :default";

protected $fillable_params = ['default'];
protected $fillableParams = ['default'];

public function check($value)
{
$this->requireParameters($this->fillable_params);
$this->requireParameters($this->fillableParams);

$default = $this->parameter('default');
return $default;
}

}
9 changes: 4 additions & 5 deletions src/Rules/Different.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ class Different extends Rule

protected $message = "The :attribute must be different with :field";

protected $fillable_params = ['field'];
protected $fillableParams = ['field'];

public function check($value)
{
$this->requireParameters($this->fillable_params);
$this->requireParameters($this->fillableParams);

$field = $this->parameter('field');
$another_value = $this->validation->getValue($field);
$anotherValue = $this->validation->getValue($field);

return $value != $another_value;
return $value != $anotherValue;
}

}
5 changes: 2 additions & 3 deletions src/Rules/Digits.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ class Digits extends Rule

protected $message = "The :attribute must be numeric and must have an exact length of :length";

protected $fillable_params = ['length'];
protected $fillableParams = ['length'];

public function check($value)
{
$this->requireParameters($this->fillable_params);
$this->requireParameters($this->fillableParams);

$length = (int) $this->parameter('length');

return ! preg_match('/[^0-9]/', $value)
&& strlen((string) $value) == $length;
}

}
5 changes: 2 additions & 3 deletions src/Rules/DigitsBetween.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class DigitsBetween extends Rule

protected $message = "The :attribute must have a length between the given :min and :max";

protected $fillable_params = ['min', 'max'];
protected $fillableParams = ['min', 'max'];

public function check($value)
{
$this->requireParameters($this->fillable_params);
$this->requireParameters($this->fillableParams);

$min = (int) $this->parameter('min');
$max = (int) $this->parameter('max');
Expand All @@ -23,5 +23,4 @@ public function check($value)
return ! preg_match('/[^0-9]/', $value)
&& $length >= $min && $length <= $max;
}

}
1 change: 0 additions & 1 deletion src/Rules/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ public function check($value)
{
return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
}

}
Loading

0 comments on commit 91ff927

Please sign in to comment.