<commit_before><?php

namespace SleepingOwl\Admin\Form\Element;

use Illuminate\Http\Request;

class Password extends NamedFormElement
{
    public function __construct($path, $label = null)
    {
        parent::__construct($path, $label);

        $this->setHtmlAttributes([
            'class' => 'passwd form-control pr-5',
            'type' => 'password',
        ]);
    }

    /**
     * @var bool
     */
    protected $allowEmpty = false;
    protected $canGenerate = false;
    protected $generateLength = 8;

    /**
     * @var string
     */
    protected $view = 'form.element.password';

    /**
     * @param  Request  $request
     * @return void
     */
    public function save(Request $request)
    {
        $value = $this->getValueFromModel();

        if (! $this->isAllowedEmptyValue() && $this->getModel()->exists && empty($value)) {
            return;
        }

        parent::save($request);
    }

    /**
     * Checks if value exists only inside request instance. Otherwise it'll return null, because password hash
     * should not be returned from model and rendered inside forms.
     *
     * @return array|mixed|null|string
     */
    public function getValueFromModel()
    {
        if (($value = $this->getValueFromRequest(request())) !== null) {
            return $value;
        }

        return $this->getDefaultValue();
    }

    /**
     * @return array
     */
    public function getValidationRules()
    {
        $data = parent::getValidationRules();

        if (! $this->isAllowedEmptyValue() && $this->getModel()->exists) {
            foreach ($data as $field => $rules) {
                foreach ($rules as $i => $rule) {
                    if ($rule == 'required') {
                        unset($data[$field][$i]);
                    }
                }
            }
        }

        return $data;
    }

    /**
     * @return bool
     */
    public function isAllowedEmptyValue()
    {
        return $this->allowEmpty;
    }

    /**
     * @return $this
     */
    public function allowEmptyValue()
    {
        $this->allowEmpty = true;

        return $this;
    }

    /**
     * @return $this
     */
    public function hashWithBcrypt()
    {
        return $this->mutateValue(function ($value) {
            return bcrypt($value);
        });
    }

    /**
     * @return $this
     */
    public function hashWithMD5()
    {
        return $this->mutateValue(function ($value) {
            return md5($value);
        });
    }

    /**
     * @return $this
     */
    public function hashWithSHA1()
    {
        return $this->mutateValue(function ($value) {
            return sha1($value);
        });
    }

    /**
     * @return array
     */
    public function toArray(): array
    {
        return parent::toArray() + [
            'canGenerate' => $this->canGenerate,
            'generateLength' => $this->generateLength,
        ];
    }

    /**
     * Добавляет возможность генерировать пароль.
     * Длина пароля может задаваться параметром.
     *
     * @param null $length
     * @return $this
     */
    public function canGenerate($length = null): self
    {
        $this->canGenerate = true;

        if ($length) {
            $this->generateLength = (int) $length;
        }

        return $this;
    }
}<commit_msg>Apply fixes from StyleCI<commit_after><?php

namespace SleepingOwl\Admin\Form\Element;

use Illuminate\Http\Request;

class Password extends NamedFormElement
{
    public function __construct($path, $label = null)
    {
        parent::__construct($path, $label);

        $this->setHtmlAttributes([
            'class' => 'passwd form-control pr-5',
            'type' => 'password',
        ]);
    }

    /**
     * @var bool
     */
    protected $allowEmpty = false;
    protected $canGenerate = false;
    protected $generateLength = 8;

    /**
     * @var string
     */
    protected $view = 'form.element.password';

    /**
     * @param  Request  $request
     * @return void
     */
    public function save(Request $request)
    {
        $value = $this->getValueFromModel();

        if (! $this->isAllowedEmptyValue() && $this->getModel()->exists && empty($value)) {
            return;
        }

        parent::save($request);
    }

    /**
     * Checks if value exists only inside request instance. Otherwise it'll return null, because password hash
     * should not be returned from model and rendered inside forms.
     *
     * @return array|mixed|null|string
     */
    public function getValueFromModel()
    {
        if (($value = $this->getValueFromRequest(request())) !== null) {
            return $value;
        }

        return $this->getDefaultValue();
    }

    /**
     * @return array
     */
    public function getValidationRules()
    {
        $data = parent::getValidationRules();

        if (! $this->isAllowedEmptyValue() && $this->getModel()->exists) {
            foreach ($data as $field => $rules) {
                foreach ($rules as $i => $rule) {
                    if ($rule == 'required') {
                        unset($data[$field][$i]);
                    }
                }
            }
        }

        return $data;
    }

    /**
     * @return bool
     */
    public function isAllowedEmptyValue()
    {
        return $this->allowEmpty;
    }

    /**
     * @return $this
     */
    public function allowEmptyValue()
    {
        $this->allowEmpty = true;

        return $this;
    }

    /**
     * @return $this
     */
    public function hashWithBcrypt()
    {
        return $this->mutateValue(function ($value) {
            return bcrypt($value);
        });
    }

    /**
     * @return $this
     */
    public function hashWithMD5()
    {
        return $this->mutateValue(function ($value) {
            return md5($value);
        });
    }

    /**
     * @return $this
     */
    public function hashWithSHA1()
    {
        return $this->mutateValue(function ($value) {
            return sha1($value);
        });
    }

    /**
     * @return array
     */
    public function toArray(): array
    {
        return parent::toArray() + [
            'canGenerate' => $this->canGenerate,
            'generateLength' => $this->generateLength,
        ];
    }

    /**
     * Добавляет возможность генерировать пароль.
     * Длина пароля может задаваться параметром.
     *
     * @param  null  $length
     * @return $this
     */
    public function canGenerate($length = null): self
    {
        $this->canGenerate = true;

        if ($length) {
            $this->generateLength = (int) $length;
        }

        return $this;
    }
}
{'fraction_non_alphanumeric': 0.1364522417153996, 'fraction_numerical': 0.001799370220422852, 'mean_word_length': 1.7004048582995952, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '5805692', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1891, 'n_words': 415}