Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
webbingbrasil/core-module / Http / Requests / BaseFormRequest.php
Size: Mime:
<?php

namespace Modules\Core\Http\Requests;

use Modules\Core\Facades\LocalizationFacade as Localization;

abstract class BaseFormRequest extends Request
{
    /**
     * Set the translation key prefix for attributes.
     * @var string
     */
    protected $attrKey = 'validation.attributes.';

    /**
     * Current processed locale
     * @var string
     */
    protected $localeKey;

    /**
     * Return an array of rules for translatable fields
     * @return array
     */
    public function translationRules()
    {
        return [];
    }

    /**
     * Return an array of messages for translatable fields
     * @return array
     */
    public function translationMessages()
    {
        return [];
    }

    /**
     * @return array
     */
    public function withTranslations()
    {
        $results = $this->all();
        $locales = $translations = [];
        foreach (array_keys($this->requiredLocales()) as $key) {
            $locales[] = $key;
            $translations[$key] = $this->get($key);
        }
        $results['translations'] = $translations;
        array_forget($results, $locales);
        return $results;
    }

    /**
     * @return array
     */
    public function requiredLocales()
    {
        return Localization::getSupportedLocales();
    }

    /**
     * Get the validator instance for the request.
     * @return \Illuminate\Validation\Validator
     */
    protected function getValidatorInstance()
    {
        $factory = $this->container->make('Illuminate\Validation\Factory');
        $rules = $this->container->call([$this, 'rules']);
        $attributes = $this->attributes();
        $messages = [];
        $attrKey = $this->getTranslationsAttributesKey();

        foreach (array_keys($this->requiredLocales()) as $localeKey) {
            $this->localeKey = $localeKey;
            foreach ($this->container->call([$this, 'translationRules']) as $attribute => $rule) {
                $key = $localeKey . '.' . $attribute;
                $rules[$key] = $rule;
                $attributes[$key] = trans($attrKey . $attribute);
            }
            foreach ($this->container->call([$this, 'translationMessages']) as $attributeAndRule => $message) {
                $messages[$localeKey . '.' . $attributeAndRule] = $message;
            }
        }
        return $factory->make(
            $this->sanitizeInput(),
            $rules,
            array_merge($this->messages(), $messages),
            $attributes
        );
    }

    /**
     * Get the validation for attributes key from the implementing class
     * or use a sensible default
     * @return string
     */
    private function getTranslationsAttributesKey()
    {
        return rtrim($this->translationsAttributesKey, '.') . '.';
    }
}