Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update docs readme, fix bug, and refactor code #44

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ Right now project supported next validation types:
'ends_with',
'doesnt_start_with',
'doesnt_end_with',
'multiple_of'
'multiple_of',
'same'
];
```

Expand Down
40 changes: 4 additions & 36 deletions src/ValidationMessages/DefaultMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
trait DefaultMessages
{
use ValidationServices;

public array $ruleNames = [
ValidationConstant::RULE_REQUIRED,
ValidationConstant::RULE_INTEGER,
Expand Down Expand Up @@ -110,7 +112,7 @@ public function messages(): array
foreach ($keyRules as $keyRule) {
if (is_object($keyRule)) {
$className = (new ReflectionClass($keyRule))->getShortName();
$messages["{$key}.{$className}"] = strtr($keyRule->message(), [ValidationConstant::KEY_ATTRIBUTE => $key]);
$messages["{$key}.{$className}"] = $this->getKeyObjectMessage($keyRule, $key);
continue;
}
if (in_array($keyRule, $this->getRuleNames(), true)) {
Expand All @@ -119,7 +121,7 @@ public function messages(): array
}
$extract = explode(ValidationConstant::COLON, $keyRule);
if (isset($extract[1]) && in_array($extract[0], $this->getRuleNames(), true)) {
$messages["{$key}.{$extract[0]}"] = $this->getKeyValueMessage($extract, $key);
$messages["{$key}.{$extract[0]}"] = $this->getKeyValueMessage($extract[0], $key, $extract[1]);
}
}
}
Expand Down Expand Up @@ -162,38 +164,4 @@ public function addRulesToMessages(array $newRules): self

return $this;
}

/**
* @param string $keyRule
* @param string $key
* @return string
*/
protected function getKeyMessage(string $keyRule, string $key): string
{
$template = $this->getRulesToMessages()[$keyRule] ?? ValidationConstant::INVALID_DATA_FOR_KEY;

return strtr($template, [ValidationConstant::KEY_ATTRIBUTE => $key]);
}

/**
* @param array $extract
* @param string $key
* @return string
*/
protected function getKeyValueMessage(array $extract = [], string $key = ''): string
{
if ([] == $extract) {
return 'Invalid data for ' . $key;
}

$template = $this->getRulesToMessages()[$extract[0]] ?? ValidationConstant::INVALID_DATA_FOR_KEY;

return strtr(
$template,
[
ValidationConstant::KEY_ATTRIBUTE => $key,
ValidationConstant::RULE_VALUE => trim(implode(', ', explode(',', $extract[1])))
]
);
}
}
6 changes: 3 additions & 3 deletions src/ValidationMessages/ValidationConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class ValidationConstant

public const RULE_NUMERIC = 'numeric';
public const RULE_VALUE = ':value';

public const KEY_ATTRIBUTE = ':attribute';
public const KEY_OTHER = ':other';
public const INVALID_DATA_FOR_KEY = 'Invalid data for :attribute';
Expand All @@ -66,7 +65,8 @@ class ValidationConstant
public const MESSAGE_KEY_IMAGE = ':attribute must be an image (jpg, jpeg, png)';
public const MESSAGE_KEY_ACCEPTED = 'The :attribute must be yes, on, 1, or true.';
public const MESSAGE_KEY_ARRAY = 'The :attribute must be array.';
public const MESSAGE_KEY_BOOLEAN = 'The :attribute must be able to be cast as a boolean. Accepted input are true, false, 1 and 0.';
public const MESSAGE_KEY_BOOLEAN = 'The :attribute must be able to be cast as a boolean.
Accepted input are true, false, 1 and 0.';
public const MESSAGE_SELECTED_VALUE_IS_INVALID = 'The selected :attribute: is invalid';
public const MESSAGE_KEY_REGEX = 'The :attribute: format is invalid';
public const MESSAGE_KEY_EXISTS = 'The :attribute: doesn\'t exist';
Expand All @@ -81,5 +81,5 @@ class ValidationConstant
public const MESSAGE_KEY_DOESNT_START_WITH = 'The :attribute cannot start with :value';
public const MESSAGE_KEY_DOESNT_END_WITH = 'The :attribute cannot end with :value';
public const MESSAGE_KEY_MULTIPLE_OF = 'The :attribute must be a multiple of :value';
public const MESSAGE_KEY_SAME ='The :attribute and :other must match';
public const MESSAGE_KEY_SAME = 'The :attribute and :other must match';
}
49 changes: 49 additions & 0 deletions src/ValidationMessages/ValidationServices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Setnemo\ValidationMessages;

trait ValidationServices
{
/**
* @param string $keyRule
* @param string $key
* @return string
*/
public function getKeyMessage(string $keyRule, string $key): string
{
$template = $this->getRulesToMessages()[$keyRule] ?? ValidationConstant::INVALID_DATA_FOR_KEY;

return strtr($template, [ValidationConstant::KEY_ATTRIBUTE => $key]);
}

/**
* @param array $extract
* @param string $key
* @return string
*/
public function getKeyValueMessage(string $string, string $key, string $value): string
{
$template = $this->getRulesToMessages()[$string] ?? ValidationConstant::INVALID_DATA_FOR_KEY;

return strtr(
$template,
[
ValidationConstant::KEY_ATTRIBUTE => $key,
ValidationConstant::RULE_VALUE => trim(implode(', ', explode(',', $value))),
ValidationConstant::KEY_OTHER => $value,
]
);
}

/**
* @param object $objectRule
* @param string $key
* @return string
*/
public function getKeyObjectMessage(object $objectRule, string $key): string
{
return strtr($objectRule->message(), [ValidationConstant::KEY_ATTRIBUTE => $key]);
}
}
Loading
Loading