<?php
namespace nur\b\authnz;

use ArrayAccess;
use nur\A;
use nur\b\coll\TBaseArray;
use nur\b\coll\TGenericArray;

class UnknownUser implements IAuthzUser, ArrayAccess {
  use TBaseArray, TGenericArray;

  /** @var self */
  private static $instance;

  static final function with(?string $username=null): self {
    if ($username !== null) return new self($username);
    if (self::$instance === null) self::$instance = new self();
    return self::$instance;
  }

  function __construct(?string $username=null) {
    $this->username = $username;
    $this->data = [];
  }

  function isValid(): bool {
    return false;
  }

  private $username;

  function getUsername(): string {
    $username = $this->username;
    return $username !== null? $username: "INCONNU";
  }

  function validatePassword(string $password): bool {
    return false;
  }

  function getDisplayName(): ?string {
    return $this->username;
  }

  function getShortName(): ?string {
    return $this->username;
  }

  function getMail(): ?string {
    return null;
  }

  function getRole(): ?string {
    return null;
  }

  function isRole($roles): bool {
    return $roles === null || in_array(self::ROLE_ANON, A::with($roles));
  }

  function isPerm($perm): bool {
    return $perm === null;
  }

  function has($key): bool {
    return false;
  }
}
