[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/User/ -> UserFactory.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\User;
  11  
  12  use Joomla\Database\DatabaseInterface;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('_JEXEC') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Default factory for creating User objects
  20   *
  21   * @since  4.0.0
  22   */
  23  class UserFactory implements UserFactoryInterface
  24  {
  25      /**
  26       * The database.
  27       *
  28       * @var  DatabaseInterface
  29       */
  30      private $db;
  31  
  32      /**
  33       * UserFactory constructor.
  34       *
  35       * @param   DatabaseInterface  $db  The database
  36       */
  37      public function __construct(DatabaseInterface $db)
  38      {
  39          $this->db = $db;
  40      }
  41  
  42      /**
  43       * Method to get an instance of a user for the given id.
  44       *
  45       * @param   int  $id  The id
  46       *
  47       * @return  User
  48       *
  49       * @since   4.0.0
  50       */
  51      public function loadUserById(int $id): User
  52      {
  53          return new User($id);
  54      }
  55  
  56      /**
  57       * Method to get an instance of a user for the given username.
  58       *
  59       * @param   string  $username  The username
  60       *
  61       * @return  User
  62       *
  63       * @since   4.0.0
  64       */
  65      public function loadUserByUsername(string $username): User
  66      {
  67          // Initialise some variables
  68          $query = $this->db->getQuery(true)
  69              ->select($this->db->quoteName('id'))
  70              ->from($this->db->quoteName('#__users'))
  71              ->where($this->db->quoteName('username') . ' = :username')
  72              ->bind(':username', $username)
  73              ->setLimit(1);
  74          $this->db->setQuery($query);
  75  
  76          return $this->loadUserById((int) $this->db->loadResult());
  77      }
  78  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer