<?php
namespace SabaiApps\Framework\User;

/**
 * User class for representing a user in the system.
 * Provides methods for accessing user identity and checking user roles.
 */
class User {

	/**
	 * @var AbstractIdentity User identity object.
	 */
	protected $_identity;

	/**
	 * @var bool Flag indicating whether the user is an administrator.
	 */
	protected $_administrator = false;

	/**
	 * Constructor.
	 *
	 * @param AbstractIdentity $identity User identity object.
	 */
	public function __construct( AbstractIdentity $identity ) {
		$this->_identity = $identity;
	}

	/**
	 * Magic method for accessing identity properties.
	 *
	 * @param string $key Property name.
	 * @return mixed Property value.
	 */
	public function __get( $key ) {
		return $this->_identity->$key;
	}

	/**
	 * Gets the identity object for the user.
	 *
	 * @return AbstractIdentity
	 */
	public function getIdentity() {
		return $this->_identity;
	}

	/**
	 * Checks if the user has an anonymous identity.
	 *
	 * @return bool True if anonymous, false otherwise.
	 */
	public function isAnonymous() {
		return $this->_identity->isAnonymous();
	}

	/**
	 * Sets the user as an administrator.
	 *
	 * @param bool $flag True to set as administrator, false otherwise.
	 * @return $this
	 */
	public function setAdministrator( $flag = true ) {
		$this->_administrator = $flag;

		return $this;
	}

	/**
	 * Checks whether the user is an administrator.
	 *
	 * @return bool True if administrator, false otherwise.
	 */
	public function isAdministrator() {
		return $this->_administrator;
	}
}
