Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Nov 9, 2024
1 parent 7968ae0 commit 428f0d8
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 54 deletions.
16 changes: 8 additions & 8 deletions src/Laravel/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public function boot()
);

// add rules to laravel validator
foreach ($this->getRuleShortnames() as $rulename) {
foreach ($this->ruleShortnames() as $rulename) {
$this->app['validator']->extend(
$rulename,
function ($attribute, $value, $parameters, $validator) use ($rulename) {
return $this->getInterventionRule($rulename, $parameters)
return $this->interventionRule($rulename, $parameters)
->isValid($value);
},
$this->getErrorMessage($rulename)
$this->errorMessage($rulename)
);
}
}
Expand All @@ -44,7 +44,7 @@ function ($attribute, $value, $parameters, $validator) use ($rulename) {
* @return Rule
* @throws NotExistingRuleException
*/
private function getInterventionRule(string $rulename, array $parameters): Rule
private function interventionRule(string $rulename, array $parameters): Rule
{
$classname = sprintf("Intervention\Validation\Rules\%s", ucfirst($rulename));

Expand All @@ -56,11 +56,11 @@ private function getInterventionRule(string $rulename, array $parameters): Rule
}

/**
* List all shortnames of new rule objects
* List all shortnames of Intervention validation rule objects
*
* @return array<string>
*/
private function getRuleShortnames(): array
private function ruleShortnames(): array
{
return array_map(function ($filename) {
return mb_strtolower(substr($filename, 0, -4));
Expand All @@ -73,13 +73,13 @@ private function getRuleShortnames(): array
* @param string $rulename
* @return string
*/
protected function getErrorMessage(string $rulename): string
protected function errorMessage(string $rulename): string
{
return $this->app['translator']->get('validation::validation.' . $rulename);
}

/**
* Get the services provided by the provider.
* Return the services provided by the provider.
*
* @return array<string>
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Domainname.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Domainname extends AbstractRule
*/
public function isValid(mixed $value): bool
{
$labels = $this->getLabels($value); // get labels of domainname
$labels = $this->labels($value); // get labels of domainname
$tld = end($labels); // most right label of domainname is tld

// domain must have 2 labels minimum
Expand Down Expand Up @@ -43,7 +43,7 @@ public function isValid(mixed $value): bool
*
* @return array<string>
*/
private function getLabels(mixed $value): array
private function labels(mixed $value): array
{
return explode('.', $this->idnToAscii($value));
}
Expand Down
16 changes: 8 additions & 8 deletions src/Rules/Iban.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function isValid(mixed $value): bool
$value = str_replace(' ', '', strtoupper(strval($value)));

// check iban length and checksum
return $this->hasValidLength($value) && $this->getChecksum($value) === 1;
return $this->hasValidLength($value) && $this->checksum($value) === 1;
}

/**
Expand All @@ -141,12 +141,12 @@ public function isValid(mixed $value): bool
* @param string $iban
* @return int
*/
private function getChecksum(string $iban): int
private function checksum(string $iban): int
{
$iban = substr($iban, 4) . substr($iban, 0, 4);
$iban = str_replace(
$this->getReplacementsChars(),
$this->getReplacementsValues(),
$this->replacementsChars(),
$this->replacementsValues(),
$iban
);

Expand All @@ -167,7 +167,7 @@ private function getChecksum(string $iban): int
* @param string $iban
* @return int|false
*/
private function getDesignatedIbanLength(string $iban): int|false
private function designatedIbanLength(string $iban): int|false
{
$countrycode = substr($iban, 0, 2);

Expand All @@ -182,15 +182,15 @@ private function getDesignatedIbanLength(string $iban): int|false
*/
private function hasValidLength(string $iban): bool
{
return $this->getDesignatedIbanLength($iban) == strlen($iban);
return $this->designatedIbanLength($iban) == strlen($iban);
}

/**
* Get chars to be replaced in checksum calculation
*
* @return array<string>
*/
private function getReplacementsChars(): array
private function replacementsChars(): array
{
return range('A', 'Z');
}
Expand All @@ -200,7 +200,7 @@ private function getReplacementsChars(): array
*
* @return array<string>
*/
private function getReplacementsValues(): array
private function replacementsValues(): array
{
$values = [];
foreach (range(10, 35) as $value) {
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Isbn.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function isValid(mixed $value): bool
*/
private function shortChecksumMatches(string $value): bool
{
return $this->getShortChecksum($value) % 11 === 0;
return $this->shortChecksum($value) % 11 === 0;
}

/**
Expand All @@ -57,7 +57,7 @@ private function shortChecksumMatches(string $value): bool
* @param string $value
* @return int
*/
private function getShortChecksum(string $value): int
private function shortChecksum(string $value): int
{
$checksum = 0;
$multiplier = 10;
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Isin.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function isValid(mixed $value): bool
*/
public function normalize(string $value): string
{
return $this->replaceChars($this->getValueWithoutLastDigit($value)) . $this->getLastDigit($value);
return $this->replaceChars($this->valueWithoutLastDigit($value)) . $this->lastDigit($value);
}

/**
Expand All @@ -78,7 +78,7 @@ private function replaceChars(string $value): string
* @param string $value
* @return string
*/
private function getValueWithoutLastDigit(string $value): string
private function valueWithoutLastDigit(string $value): string
{
return substr($value, 0, -1);
}
Expand All @@ -89,7 +89,7 @@ private function getValueWithoutLastDigit(string $value): string
* @param string $value
* @return string
*/
private function getLastDigit(string $value): string
private function lastDigit(string $value): string
{
return substr($value, -1);
}
Expand Down
13 changes: 2 additions & 11 deletions src/Rules/Lowercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,8 @@ class Lowercase extends AbstractRule
*/
public function isValid(mixed $value): bool
{
return $value === $this->getLowerCaseValue($value);
}
$lowerCaseValue = mb_strtolower(strval($value), mb_detect_encoding($value));

/**
* Return value as lowercase
*
* @param mixed $value
* @return string
*/
private function getLowerCaseValue(mixed $value): string
{
return mb_strtolower(strval($value), mb_detect_encoding($value));
return $value === $lowerCaseValue;
}
}
4 changes: 2 additions & 2 deletions src/Rules/Luhn.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Luhn extends AbstractRule
*/
public function isValid(mixed $value): bool
{
return $this->checksumIsValid($this->getChecksum($value));
return $this->checksumIsValid($this->checksum($value));
}

/**
Expand All @@ -35,7 +35,7 @@ private function checksumIsValid($checksum): bool
* @param mixed $value
* @return int
*/
private function getChecksum($value): int
private function checksum($value): int
{
$checksum = 0;
$reverse = strrev(strval($value));
Expand Down
12 changes: 6 additions & 6 deletions src/Rules/Postalcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static function countrycode(array $countrycodes): self
*/
public function isValid(mixed $value): bool
{
foreach ($this->getPatterns() as $pattern) {
foreach ($this->patterns() as $pattern) {
if (preg_match($pattern, $value)) {
return true;
}
Expand All @@ -92,11 +92,11 @@ public function isValid(mixed $value): bool
*
* @return array<string>
*/
private function getPatterns(): array
private function patterns(): array
{
$patterns = array_map(function ($countrycode) {
return $this->getPattern($countrycode);
}, $this->getCountryCodes());
return $this->pattern($countrycode);
}, $this->countryCodes());

return array_filter($patterns, function ($pattern) {
return !is_null($pattern);
Expand All @@ -108,7 +108,7 @@ private function getPatterns(): array
*
* @return array<string>
*/
private function getCountryCodes(): array
private function countryCodes(): array
{
if (count($this->countrycodes) == 0) {
// return country code by reference
Expand All @@ -125,7 +125,7 @@ private function getCountryCodes(): array
*
* @return ?string
*/
private function getPattern(string $countrycode): ?string
private function pattern(string $countrycode): ?string
{
return match (strtolower($countrycode)) {
'dz', 'as', 'ad', 'de', 'ba', 'ic', 'mp', 'hr', 'cu', 'ee', 'fi', 'fr', 'gf', 'gp', 'gu', 'id', 'it', 'kr',
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Titlecase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Titlecase extends AbstractRule
*/
public function isValid(mixed $value): bool
{
foreach ($this->getWords($value) as $word) {
foreach ($this->words($value) as $word) {
if (!$this->isValidWord($word)) {
return false;
}
Expand All @@ -30,7 +30,7 @@ public function isValid(mixed $value): bool
* @param mixed $value
* @return array<string>
*/
private function getWords(mixed $value): array
private function words(mixed $value): array
{
return explode(" ", $value);
}
Expand Down
12 changes: 2 additions & 10 deletions src/Rules/Uppercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,8 @@ class Uppercase extends AbstractRule
*/
public function isValid(mixed $value): bool
{
return $value === $this->getUpperCaseValue($value);
}
$upperCaseValue = mb_strtoupper(strval($value), mb_detect_encoding($value));

/**
* Return value as uppercase
*
* @return string
*/
private function getUpperCaseValue(mixed $value): string
{
return mb_strtoupper(strval($value), mb_detect_encoding($value));
return $value === $upperCaseValue;
}
}

0 comments on commit 428f0d8

Please sign in to comment.