<commit_before><?php

namespace Application\Web\Admin\Form;

use Application\Entity\Role;
use Application\Entity\User;
use Application\Repository\RoleRepository;
use Application\Repository\UserRepository;
use MiniFrame\ServiceLoader;
use MiniFrame\WebApplication\BaseForm;
use Respect\Validation\Validator;

class UserSaveForm extends BaseForm
{
    /**
     * @var int
     */
    public $id;

    /**
     * @var string
     */
    public $username;

    /**
     * @var string
     */
    public $email;

    /**
     * @var int
     */
    public $role;

    /**
     * @var string
     */
    public $name;

    /**
     * @var string
     */
    public $surname;

    /**
     * @var string
     */
    public $password;

    /**
     * @var string
     */
    public $passwordRepeat;

    /**
     * @var array
     */
    public $allRoles;

    /**
     * @var User
     */
    protected $currentUser;

    /**
     * @var Role
     */
    protected $selectedRole;

    public function __construct(ServiceLoader $serviceLoader)
    {
        parent::__construct($serviceLoader);
        $this->allRoles = $this->getRoleRepository()->findAll();
    }

    public function loadParamsFromRequest()
    {
        $this->id = intval($this->getRequest()->get('id'));
        $this->username = $this->getRequest()->get('username');
        $this->email = $this->getRequest()->get('email');
        $this->name = $this->getRequest()->get('name');
        $this->surname = $this->getRequest()->get('surname');
        $this->password = trim($this->getRequest()->get('password'));
        $this->passwordRepeat = trim($this->getRequest()->get('password_repeat'));
        $this->role = intval($this->getRequest()->get('role'));
    }

    public function loadParamsFromCurrentUser()
    {
        $this->id = intval($this->getRequest()->get('id'));

        if ($this->getCurrentUser() != null) {
            $this->username = $this->getCurrentUser()->getUsername();
            $this->email = $this->getCurrentUser()->getEmail();
            $this->name = $this->getCurrentUser()->getName();
            $this->surname = $this->getCurrentUser()->getSurname();
            $this->role = $this->getCurrentUser()->getRole()->getId();
            $this->selectedRole = $this->getCurrentUser()->getRole();
        }
    }

    /**
     * @return bool
     */
    protected function validateFields()
    {
        $this->validateUsername();
        $this->validateEmail();
        $this->validateRole();
        $this->validatePassword();
    }

    protected function validateUsername()
    {
        // TODO : Karakter kontrolü yapılmalı.
        if (!Validator::create()->notEmpty()->validate($this->username)) {
            $this->setMessage('username', 'empty', 'Kullanıcı adı gerekli.');
        } else {
            if ($this->getUserRepository()->isUsernameUsing($this->username, $this->id)) {
                $this->setMessage('username', 'using', 'Seçtiğiniz kullanıcı adı kullanılıyor.');
            }
        }
    }
<commit_msg>Kullanıcı adı için karakter ve uzunluk kontrolü yapıldı.<commit_after><?php

namespace Application\Web\Admin\Form;

use Application\Entity\Role;
use Application\Entity\User;
use Application\Repository\RoleRepository;
use Application\Repository\UserRepository;
use MiniFrame\ServiceLoader;
use MiniFrame\WebApplication\BaseForm;
use Respect\Validation\Validator;

class UserSaveForm extends BaseForm
{
    /**
     * @var int
     */
    public $id;

    /**
     * @var string
     */
    public $username;

    /**
     * @var string
     */
    public $email;

    /**
     * @var int
     */
    public $role;

    /**
     * @var string
     */
    public $name;

    /**
     * @var string
     */
    public $surname;

    /**
     * @var string
     */
    public $password;

    /**
     * @var string
     */
    public $passwordRepeat;

    /**
     * @var array
     */
    public $allRoles;

    /**
     * @var User
     */
    protected $currentUser;

    /**
     * @var Role
     */
    protected $selectedRole;

    public function __construct(ServiceLoader $serviceLoader)
    {
        parent::__construct($serviceLoader);
        $this->allRoles = $this->getRoleRepository()->findAll();
    }

    public function loadParamsFromRequest()
    {
        $this->id = intval($this->getRequest()->get('id'));
        $this->username = $this->getRequest()->get('username');
        $this->email = $this->getRequest()->get('email');
        $this->name = $this->getRequest()->get('name');
        $this->surname = $this->getRequest()->get('surname');
        $this->password = trim($this->getRequest()->get('password'));
        $this->passwordRepeat = trim($this->getRequest()->get('password_repeat'));
        $this->role = intval($this->getRequest()->get('role'));
    }

    public function loadParamsFromCurrentUser()
    {
        $this->id = intval($this->getRequest()->get('id'));

        if ($this->getCurrentUser() != null) {
            $this->username = $this->getCurrentUser()->getUsername();
            $this->email = $this->getCurrentUser()->getEmail();
            $this->name = $this->getCurrentUser()->getName();
            $this->surname = $this->getCurrentUser()->getSurname();
            $this->role = $this->getCurrentUser()->getRole()->getId();
            $this->selectedRole = $this->getCurrentUser()->getRole();
        }
    }

    /**
     * @return bool
     */
    protected function validateFields()
    {
        $this->validateUsername();
        $this->validateEmail();
        $this->validateRole();
        $this->validatePassword();
    }

    protected function validateUsername()
    {
        if (preg_match('#[^a-z0-9]#', $this->username)) {
            $message = 'Kullanıcı adında boşluk olmamalı, türkçe karakter içermemeli'
                . ' ve sadece şu karakterlerden oluşmalı: a-z, 0-9';
            $this->setMessage('username', 'format', $message);
        } else {
            if ($this->getUserRepository()->isUsernameUsing($this->username, $this->id)) {
                $this->setMessage('username', 'using', 'Seçtiğiniz kullanıcı adı kullanılıyor.');
            }
        }

        if (!Validator::create()->length(3, 16)->validate($this->username)) {
            $this->setMessage('username', 'length', 'Kullanıcı adı en az 3 en fazla 16 karakter uzunluğunda olabilir.');
        }
    }

{'fraction_non_alphanumeric': 0.17237068291920613, 'fraction_numerical': 0.001562744178777934, 'mean_word_length': 2.352540597171294, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 131, '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': '6712250', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1886, 'n_words': 321}