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/ste.komma.pro/vendor/komma/kms/src/Core/AttributeDataService.php
<?php declare(strict_types=1);
namespace Komma\KMS\Core;

use Illuminate\Support\Facades\Request;
use Komma\KMS\Documents\Kms\DocumentServiceInterface;
use Komma\KMS\Globalization\Languages\Models\Language;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Traits\LabelTrait;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator as ValidatorFacade;
use Illuminate\Validation\Validator;

/**
 * Class DataService
 *
 * Validates input and fills attributes with data
 *
 * @package App\Kms\Core
 */
class AttributeDataService implements AttributeDataServiceInterface
{
    private $afterSaveQueueTranslations;

    private $afterSaveQueue;
    /**
     * @var DocumentServiceInterface
     */
    protected $documentService;

    public function __construct()
    {
        $this->afterSaveQueue = new Collection();
        $this->afterSaveQueueTranslations = new Collection();
    }

    /**
     * Fills all attributes with values from the forms / session
     * @param Collection $attributes
     * @return Collection
     */
    public function fillAttributesFromInput(Collection $attributes)
    {
        if(!$this->containsAttributes($attributes)) return $attributes;

        foreach ($attributes as $attribute) {
            /** @var Attribute $attribute */

            //Insert the the appropriate value in the attribute
            $input = Request::get((string)$attribute->getKey());

            if ($input !== null) $attribute->setValue($input);

            //Insert the associated language (if any) into the attribute
            if($attribute->getKey() == null) dd('Attribute without key detected: ', $attribute);
            if ($attribute->getKey()->getTranslationIso2() != '') {
                //get the language associated with the attribute]
                $language = Language::where('iso_2', $attribute->getKey()->getTranslationIso2())->first();
                $attribute->setAssociatedLanguage($language);
            }
        }

        return $attributes;
    }

    /**
     * Iterates over all attribute instances and builds a Validator
     * from the rules and messages in them.
     *
     * @param Collection $attributes
     * @param array $input The input to validate. The function wil retrieve the input itself if you don't specify this.
     * If you specify it must be an associative array containing input names as keys and values as values from
     * those inputs.
     *
     * @return Validator
     */
    public function validateInputAndReturnValidator(Collection $attributes, array $input = []): ? Validator
    {
        if(!$this->containsAttributes($attributes)) return null;

        if (empty($input)) $input = Request::all();

        $rules = [];
        $messages = [];

        //Static rules.
        foreach ($attributes as $attribute) {
            /** @var Attribute $attribute */
            $set = $attribute->getValidationSet();
            if($set->getSometimesClosure()) continue; //Skip the rule if it is a "Complex Conditional validation" rule

            if ( ! $set->hasRulesAndMessages()) {
                continue;
            }

            //prepend the attribute key in front of each message rule followed by a dot to make sure the message is unique for each attribute.
            $set->prefixMessageRulesWith((string)$attribute->getKey());

            //And replace :attribute with the label text of the attribute
            if (in_array(LabelTrait::class, class_uses($attribute))) {
                /** @var LabelTrait $attribute */
                $set->modifyMessages(":attribute", $attribute->getLabelText());
            }

            $rules = array_merge($rules, [(string)$attribute->getKey() => $set->getRules()]);
            $messages = array_merge($messages, $set->getMessages());
        }

        $validator = ValidatorFacade::make($input, $rules, $messages);

        //Complex Conditional rules
        foreach ($attributes as $attribute) {
            /** @var Attribute $attribute */
            $set = $attribute->getValidationSet();
            if($sometimesClosure = $set->getSometimesClosure()) {
                $validator->sometimes((string) $attribute->getKey(), $set->getRules(), $set->getSometimesClosure());
            }
        }

        return $validator;
    }

    /**
     * Returns true if a collection contains attributes. false if not
     *
     * @param Collection $attributes
     * @return bool
     */
    protected function containsAttributes(Collection $attributes)
    {
        $containsAttributes = true;
        $attributes->each(function ($attribute) use(&$containsAttributes) {
            if(!is_a($attribute, Attribute::class)) {
                $containsAttributes = false;
                return false;
            }
        });

        return $containsAttributes;
    }
}