HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.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);
    }


}