File: D:/HostingSpaces/SBogers10/plateau.komma.nl/vendor/komma/kms/src/Core/AttributeValidatorFactory.php
<?php declare(strict_types=1);
namespace Komma\KMS\Core;
use Illuminate\Support\Collection;
use Illuminate\Validation\Factory;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Traits\LabelTrait;
class AttributeValidatorFactory extends Factory
{
/**
* @param array $data
* @param Collection $attributes
* @param array $rules
* @param array $messages
* @param array $customAttributes
*
* @return \Illuminate\Validation\Validator
*/
public function makeForAttributes(array $data, Collection $attributes, array $rules = [], array $messages = [], array $customAttributes = [])
{
//Get the validation rules and messages from the attributes. And create custom validation attribute translations.
$attributeRules = [];
$attributeMessages = [];
$customValidationAttributeTranslations = [];
foreach ($attributes as $attribute) {
if(!is_a($attribute, Attribute::class)) throw new \InvalidArgumentException('The attributes collection must contain '.Attribute::class.' instances');
/** @var Attribute $attribute */
$key = (string)$attribute->getKey();
$attributeRules[$key] = $attribute->getRules();
$attributeMessages[$key] = $attribute->getMessages();
if(in_array(LabelTrait::class, class_uses($attribute), true)) {
/** @var LabelTrait $attribute */
$customValidationAttributeTranslations[$key] = $attribute->getLabelText();
}
}
//Merge the rules, messages and custom attributes from the parameters with the ones from the attributes
$rules = array_merge($rules, $attributeRules);
$messages = array_merge($messages, $attributeMessages);
$customAttributes = array_merge($customAttributes, $customValidationAttributeTranslations);
//Create and return a validator
return parent::make($data, $rules, $messages, $customAttributes);
}
}