File: D:/HostingSpaces/SBogers10/hem-mechatronics.komma.pro/app/Komma/Kms/Core/ValidationSet.php
<?php
namespace App\Komma\Kms\Core;
use App\Komma\Kms\Core\Sections\AbstractAttributeKey;
use App\Komma\Kms\Core\Sections\AttributeKey;
use function GuzzleHttp\Psr7\str;
/**
* Class ValidationSet
* @package App\Komma\Kms\Core
*
* This class holds validation rules and corresponding error messages for laravel's validator
*
* Validation is done against attribute value references as of the time of writing this but it may be different in the future.
*/
class ValidationSet
{
/**
* The rules that need to apply to the field that you specify with setFieldName();
* If you want to add more rules for a single field name you can seperate them with pipes (|).
*
* @see https://laravel.com/docs/5.5/validation#available-validation-rules
* @see https://laravel.com/docs/5.5/validation#manually-creating-validators
* @see ValidationSet::setRules()
*
* @var string $rules
*/
private $rules;
/**
* @var string[] Associative array containing rule names (strings) as keys and error messages (strings) as values.
* You may use placeholders in the error messages. :attribute for the field name and other for the related other field name
*
* @see https://laravel.com/docs/5.5/validation#custom-error-messages
*/
private $messages;
/**
* ValidationSet constructor.
*/
public function __construct()
{
$this->rules = '';
$this->messages = [];
}
/**
* Returns the number of rules
*/
private function getRuleCount() {
if(is_string($this->rules)) {
$rulesSplit = explode('|', $this->rules);
return count($rulesSplit);
} elseif(is_array($this->rules)) {
return count($this->rules);
}
}
/**
* Returns the amount of messages
*
* @return int
*/
private function getMessageCount()
{
return count($this->messages);
}
/**
* Returns true if there are rules and messages inside this validation set. false otherwise
*
* @return bool
*/
public function hasRulesAndMessages(): bool
{
return $this->getRuleCount() > 0 && $this->getMessageCount() > 0;
}
/**
* @return string|array
*/
public function getRules()
{
return $this->rules;
}
/**
* The rules that need to apply to the field this validation set is used on (via an attribute).
* If you want to add more rules for a single field name you can seperate them with pipes (|).
*
* @see https://laravel.com/docs/5.5/validation#available-validation-rules
* @see https://laravel.com/docs/5.5/validation#manually-creating-validators
*
* @param string|array $rules
* @return ValidationSet
*/
public function setRules($rules): ValidationSet
{
$this->rules = $rules;
return $this;
}
/**
* @return string[] Associative array containing rule names (strings) as keys and error messages (strings) as values.
*/
public function getMessages(): array
{
$this->validateIntegrity();
return $this->messages;
}
/**
* Prefixes each message rule with a string followed by a dot. But only of the rules does not already have a dot in it.
* Example. If your message rule is: "'required' => 'please enter a value for :attribute'" it wil modify it to look like
* "'myprefix.required' => 'please enter a value for :attribute'" if the prefix is "myprefix"
*
* @param string $prefix
*/
public function prefixMessageRulesWith(string $prefix)
{
foreach($this->getMessages() as $rule => $message)
{
if(strpos($rule, '.') !== false) continue; //Already is a custom message bound to a specific field. Skip it
$originalMessage = $message;
unset($this->messages[$rule]);
$this->messages[$prefix.'.'.$rule] = $originalMessage;
}
}
/**
* Loops over all messages and replaces $what with $with
*
* @param string $what
* @param string $with
*/
public function modifyMessages(string $what, string $with)
{
$this->messages = str_replace($what, $with, $this->messages);
}
/**
* Validates that there are as much rules as there are messages for those rules.
*/
private function validateIntegrity()
{
if($this->getRuleCount() != $this->getMessageCount()) throw new \InvalidArgumentException("The validation set's integrity check has failed. Please make sure that you have as much rules as you have messages in it.");
}
/**
* @param string[] $messages Associative array containing rule names (strings) as keys and error messages (strings) as values.
* You may use placeholders in the error messages. :attribute for the field name and other for the related other field name
* @return $this
*/
public function setMessages(array $messages)
{
foreach($messages as $ruleName => $message)
{
if(!is_string($ruleName) || !is_string($message)) throw new \InvalidArgumentException("The validation set expects the messages to be an associative array with both keys and values of type string but at least one of them respectively was of type: ".gettype($ruleName)." and ".gettype($message));
}
$this->messages = $messages;
return $this;
}
}