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

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;
use Komma\KMS\Globalization\Languages\Models\Language;
use Komma\KMS\Core\Attributes\Attribute;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Validation\Validator as ValidatorContract;

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

    private $afterSaveQueue;

    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;

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

            if($attribute->getsValueFrom() == Attribute::NonInteractiveAttribute) continue;

            //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 ValidatorContract|null
     */
    public function validateInputAndReturnValidator(Collection $attributes, array $input = []): ? ValidatorContract
    {
        if(!$this->containsAttributes($attributes)) return null;

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

        return Validator::makeForAttributes($input, $attributes);
    }

    /**
     * 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;
    }
}