<commit_before><?php

namespace Metrique\Building\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Metrique\Building\Rules\ComponentIsBoundRule;
use Metrique\Building\Services\BuildingServiceInterface;

class ComponentEditRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
    
    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        return [
        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return $this->fetchRules(
            $this->request->get(
                '_parent',
                $this->request->get('_id')
            )
        );
    }

    public function prepareForValidation()
    {
        abort_unless(
            (new ComponentIsBoundRule)->passes(
                null,
                $this->request->get('_component')
            ),
            403
        );

        $this->merge(
            collect($this->request)->mapWithKeys(function ($value, $key) {
                return [
                    str_replace($this->_id . ':', '', $key) => $value
                ];
            })->toArray()
        );
    }

    private function fetchRules($componentId)
    {
        $component = resolve(BuildingServiceInterface::class)
            ->readComponentOnPage(
                $componentId,
                $this->page
            );
        
        $rules = collect($component->rules());
        
        if ($this->request->get('_type', '') == 'attributes') {
            return $rules->intersectByKeys(
                $component->attributes()
            )->toArray();
        }

        if ($this->request->get('_type', '') == 'multiple') {
            return $rules->intersectByKeys(
                collect($component->properties())->except('name')
            )->toArray();
        }

        if ($this->request->get('_type', '') == 'properties') {
            return $rules->intersectByKeys(
                $component->properties()
            )->toArray();
        }
    }
}
<commit_msg>Validation fix for multiple component types<commit_after><?php

namespace Metrique\Building\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Metrique\Building\Rules\ComponentIsBoundRule;
use Metrique\Building\Services\BuildingServiceInterface;

class ComponentEditRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
    
    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        return [
        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return $this->fetchRules(
            $this->request->get(
                '_parent',
                $this->request->get('_id')
            )
        );
    }

    public function prepareForValidation()
    {
        abort_unless(
            (new ComponentIsBoundRule)->passes(
                null,
                $this->request->get('_component')
            ),
            403
        );

        $this->merge(
            collect($this->request)->mapWithKeys(function ($value, $key) {
                return [
                    str_replace($this->_id . ':', '', $key) => $value
                ];
            })->toArray()
        );
    }

    private function fetchRules($componentId)
    {
        $component = resolve(BuildingServiceInterface::class)
            ->readComponentOnPage(
                $componentId,
                $this->page
            );
        
        $rules = collect($component->rules());
        
        if ($this->request->get('_type', '') == 'attributes') {
            return $rules->intersectByKeys(
                $component->attributes()
            )->toArray();
        }

        if ($this->request->get('_type', '') == 'multiple') {
            return $rules->intersectByKeys(
                collect(
                    $component->properties()
                )->merge(
                    $component->attributes()
                )->except(
                    'name'
                )
            )->toArray();
        }

        if ($this->request->get('_type', '') == 'properties') {
            return $rules->intersectByKeys(
                $component->properties()
            )->toArray();
        }
    }
}

{'fraction_non_alphanumeric': 0.13105472845460264, 'fraction_numerical': 0.0012581253931641854, 'mean_word_length': 1.4921630094043887, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4928038', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1196, 'n_words': 230}