master

laravel/framework

Last updated at: 29/12/2023 09:20

GenericUser.php

TLDR

The provided file, GenericUser.php, is a class in the Illuminate\Auth namespace. It implements the UserContract interface and represents a generic user object. It contains methods for retrieving and setting user attributes, such as the user's identifier, password, and remember me token.

Methods

__construct(array $attributes)

This method is the constructor for the GenericUser class. It accepts an array of attributes and sets the $attributes property of the object.

getAuthIdentifierName()

This method returns the name of the unique identifier for the user. In this implementation, it always returns the string 'id'.

getAuthIdentifier()

This method returns the unique identifier for the user. It retrieves the value of the identifier attribute from the $attributes array.

getAuthPasswordName()

This method returns the name of the password attribute for the user. In this implementation, it always returns the string 'password'.

getAuthPassword()

This method returns the password for the user. It retrieves the value of the password attribute from the $attributes array.

getRememberToken()

This method returns the "remember me" token value for the user. It retrieves the value from the $attributes array using the getRememberTokenName() method.

setRememberToken($value)

This method sets the value of the "remember me" token for the user. It assigns the $value to the corresponding attribute in the $attributes array using the getRememberTokenName() method.

getRememberTokenName()

This method returns the column name for the "remember me" token. In this implementation, it always returns the string 'remember_token'.

__get($key)

This magic method allows dynamically accessing the user's attributes. It retrieves the value of the attribute specified by the $key from the $attributes array.

__set($key, $value)

This magic method allows dynamically setting an attribute on the user. It assigns the $value to the attribute specified by the $key in the $attributes array.

__isset($key)

This magic method allows dynamically checking if a value is set on the user. It checks if the attribute specified by the $key exists in the $attributes array.

__unset($key)

This magic method allows dynamically unsetting a value on the user. It removes the attribute specified by the $key from the $attributes array.

<?php

namespace Illuminate\Auth;

use Illuminate\Contracts\Auth\Authenticatable as UserContract;

class GenericUser implements UserContract
{
    /**
     * All of the user's attributes.
     *
     * @var array
     */
    protected $attributes;

    /**
     * Create a new generic User object.
     *
     * @param  array  $attributes
     * @return void
     */
    public function __construct(array $attributes)
    {
        $this->attributes = $attributes;
    }

    /**
     * Get the name of the unique identifier for the user.
     *
     * @return string
     */
    public function getAuthIdentifierName()
    {
        return 'id';
    }

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->attributes[$this->getAuthIdentifierName()];
    }

    /**
     * Get the name of the password attribute for the user.
     *
     * @return string
     */
    public function getAuthPasswordName()
    {
        return 'password';
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->attributes['password'];
    }

    /**
     * Get the "remember me" token value.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->attributes[$this->getRememberTokenName()];
    }

    /**
     * Set the "remember me" token value.
     *
     * @param  string  $value
     * @return void
     */
    public function setRememberToken($value)
    {
        $this->attributes[$this->getRememberTokenName()] = $value;
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'remember_token';
    }

    /**
     * Dynamically access the user's attributes.
     *
     * @param  string  $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->attributes[$key];
    }

    /**
     * Dynamically set an attribute on the user.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return void
     */
    public function __set($key, $value)
    {
        $this->attributes[$key] = $value;
    }

    /**
     * Dynamically check if a value is set on the user.
     *
     * @param  string  $key
     * @return bool
     */
    public function __isset($key)
    {
        return isset($this->attributes[$key]);
    }

    /**
     * Dynamically unset a value on the user.
     *
     * @param  string  $key
     * @return void
     */
    public function __unset($key)
    {
        unset($this->attributes[$key]);
    }
}