<commit_before><?php
require_once __DIR__ . '/../lib/utils.php';
require_once __DIR__ . '/login.php';

class SignUp extends RawDataContainer {
  public function verify(): SignUpInfo {
    $session = $this->get('session');
    $postData = $this->get('post-data');
    $cookie = $this->get('cookie');
    $urlQuery = $this->get('url-query');
    $dbQuerySet = $this->get('db-query-set');
    $fullname = $postData->getDefault('fullname', '');
    $username = $postData->getDefault('username', '');
    $password = $postData->getDefault('password', '');
    $rePassword = $postData->getDefault('re-password', '');

    if ($postData->getDefault('signed-up', 'off') === 'on') {
      $signup = static::checkSignUp([
        'fullname' => $fullname,
        'username' => $username,
        'password' => <PASSWORD>,
        're-password' => $rePassword,
      ]);

      if ($signup->succeed()) {
        $session->without([
          'fullname',
          'username',
          'password',
          're-password',
        ])->update();

        $session->without([
          'sign-up-succeed',
          'sign-up-error-field',
          'sign-up-error-reason',
        ])->update();

        $cookie->assign([
          'logged-in' => 'on',
          'username' => $username,
          'password' => $password,
        ])->update();

        $login = LoginInfo::instance([
          'username' => $username,
          'password' => $password,
        ]);

        $query = $dbQuerySet->get('create-account');
        $dbResponse = $query->executeOnce([
          $fullname,
          $username,
          $password,
        ]);

        $urlQuery->set('page', $urlQuery->get('previous-page'))->redirect();
      } else {
        $error = $signup->error();

        $session->assign([
          'sign-up-succeed' => 'off',
          'sign-up-error-field' => $error['field'],
          'sign-up-error-reason' => $error['reason'],
        ])->update();

        return $signup;
      }
    }

    return SignUpInfo::instance();
  }

  static public function checkSignUp(array $param): SignUpInfo {
    [
      'fullname' => $fullname,
      'username' => $username,
      'password' => $password,
      're-password' => $rePassword,
    ] = $param;

    if (!$fullname) return SignUpInfo::mkerror('fullname', 'empty');
    if (!$username) return SignUpInfo::mkerror('username', 'empty');
    if (!$password) return SignUpInfo::mkerror('password', 'empty');
    if (strlen($password) < 6) return SignUpInfo::mkerror('password', 'insufficient-lenth');
    if ($password !== $rePassword) return SignUpInfo::mkerror('re-password', 'mismatch');

    return SignUpInfo::instance(array_merge($param, [
      'succeed' => true,
    ]));
  }
}

class SignUpInfo extends RawDataContainer {
  public function succeed(): bool {
    return $this->getDefault('succeed', false);
  }

  public function error(): ?array {
    return $this->getDefault('error', null);
  }

  public function login(): ?LoginInfo {
    return $this->getDefault('login', null);
  }

  static public function mkerror(string $field, string $reason): self {
    return self::instance([
      'field' => $field,
      'reason' => $reason,
    ]);<commit_msg>Check for taken username<commit_after><?php
require_once __DIR__ . '/../lib/utils.php';
require_once __DIR__ . '/login.php';

class SignUp extends RawDataContainer {
  public function verify(): SignUpInfo {
    $session = $this->get('session');
    $postData = $this->get('post-data');
    $cookie = $this->get('cookie');
    $urlQuery = $this->get('url-query');
    $dbQuerySet = $this->get('db-query-set');
    $fullname = $postData->getDefault('fullname', '');
    $username = $postData->getDefault('username', '');
    $password = $postData->getDefault('password', '');
    $rePassword = $postData->getDefault('re-password', '');

    if ($postData->getDefault('signed-up', 'off') === 'on') {
      $signup = static::checkSignUp([
        'fullname' => $fullname,
        'username' => $username,
        'password' => <PASSWORD>,
        're-password' => $<PASSWORD>,
        'db-query-set' => $dbQuerySet,
      ]);

      if ($signup->succeed()) {
        $session->without([
          'fullname',
          'username',
          'password',
          're-password',
        ])->update();

        $session->without([
          'sign-up-succeed',
          'sign-up-error-field',
          'sign-up-error-reason',
        ])->update();

        $cookie->assign([
          'logged-in' => 'on',
          'username' => $username,
          'password' => <PASSWORD>,
        ])->update();

        $login = LoginInfo::instance([
          'username' => $username,
          'password' => <PASSWORD>,
        ]);

        $query = $dbQuerySet->get('create-account');
        $dbResponse = $query->executeOnce([
          $fullname,
          $username,
          $password,
        ]);

        $urlQuery->set('page', $urlQuery->get('previous-page'))->redirect();
      } else {
        $error = $signup->error();

        $session->assign([
          'sign-up-succeed' => 'off',
          'sign-up-error-field' => $error['field'],
          'sign-up-error-reason' => $error['reason'],
        ])->update();

        return $signup;
      }
    }

    return SignUpInfo::instance();
  }

  static public function checkSignUp(array $param): SignUpInfo {
    [
      'fullname' => $fullname,
      'username' => $username,
      'password' => <PASSWORD>,
      're-password' => $<PASSWORD>,
      'db-query-set' => $dbQuerySet,
    ] = $param;

    $userAccountExistence = $dbQuerySet
      ->get('user-account-existence')
      ->executeOnce([$username], 1)
      ->rows()
    ;

    if (!$fullname) return SignUpInfo::mkerror('fullname', 'empty');
    if (!$username) return SignUpInfo::mkerror('username', 'empty');
    if ($userAccountExistence) return SignUpInfo::mkerror('username', 'taken');
    if (!$password) return SignUpInfo::mkerror('password', 'empty');
    if (strlen($password) < 6) return SignUpInfo::mkerror('password', '<PASSWORD>');
    if ($password !== $rePassword) return SignUpInfo::mkerror('re-password', 'mismatch');

    return SignUpInfo::instance(array_merge($param, [
      'succeed' => true,
    ]));
  }
}

class SignUpInfo extends RawDataContainer {
  public function succeed(): bool {
    return $this->getDefault('succeed', false);
  }

  public function error(): ?array {
    return $this->getDefault('error', null);
  }

  public function login(): ?LoginInfo {
    return $this->getDefault('login', null);
  }

  static public function mkerror(string $field, string $reason): self {
    return self::instance([
      'field' => $field,
      'reason' => $reason,
    ]);
{'fraction_non_alphanumeric': 0.1930059523809524, 'fraction_numerical': 0.0004464285714285714, 'mean_word_length': 2.6726775956284152, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 110, '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': '4410029', 'n_tokens_mistral': 2063, 'n_tokens_neox': 2031, 'n_words': 384}