wiki.techinc.nl/includes/user/UserFactory.php
DannyS712 80027cdbbc User is not @newable in 1.36
Bug: T257464
Change-Id: I0d78cfb18166f46a65a65755ca2bef57d504a0b3
2020-07-16 12:14:33 +00:00

168 lines
3.7 KiB
PHP

<?php
/**
* Factory for creating User objects without static coupling.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\User;
use IDBAccessObject;
use User;
/**
* Creates User objects.
*
* For now, there is nothing much interesting in this class. It was meant for preventing static User
* methods causing problems in unit tests.
*
* @since 1.35
*/
class UserFactory implements IDBAccessObject {
/**
* @var UserNameUtils
*/
private $userNameUtils;
/**
* @param UserNameUtils $userNameUtils
*/
public function __construct( UserNameUtils $userNameUtils ) {
$this->userNameUtils = $userNameUtils;
}
/**
* @see User::newFromName
*
* @since 1.35
*
* @param string $name
* @param string $validate Validation strategy, one of the UserNameUtils::RIGOR_*
* constants. For no validation, use UserNameUtils::RIGOR_NONE.
* @return User|bool
*/
public function newFromName(
string $name,
string $validate = UserNameUtils::RIGOR_VALID
) {
return User::newFromName( $name, $validate );
}
/**
* Returns a new anonymous User
*
* @since 1.36
*
* @param string|null $ip (optional)
* @return User
*/
public function newAnonymous( $ip = null ) : User {
$user = new User();
if ( $ip ) {
$validIp = $this->userNameUtils->isIP( $ip );
if ( $validIp ) {
$user->setName( $ip );
} else {
throw new \InvalidArgumentException( 'Invalid IP address' );
}
}
return $user;
}
/**
* @see User::newFromId
*
* @since 1.35
*
* @param int $id
* @return User
*/
public function newFromId( int $id ) : User {
return User::newFromId( $id );
}
/**
* @see User::newFromActorId
*
* @since 1.35
*
* @param int $actorId
* @return User
*/
public function newFromActorId( int $actorId ) : User {
return User::newFromActorId( $actorId );
}
/**
* @see User::newFromIdentity
*
* @since 1.35
*
* @param UserIdentity $userIdentity
* @return User
*/
public function newFromUserIdentity( UserIdentity $userIdentity ) : User {
if ( $userIdentity instanceof User ) {
return $userIdentity;
}
return $this->newFromAnyId(
$userIdentity->getId() === 0 ? null : $userIdentity->getId(),
$userIdentity->getName() === '' ? null : $userIdentity->getName(),
$userIdentity->getActorId() === 0 ? null : $userIdentity->getActorId()
);
}
/**
* @see User::newFromAnyId
*
* @since 1.35
*
* @param ?int $userId
* @param ?string $userName
* @param ?int $actorId
* @param bool|string $dbDomain
* @return User
*/
public function newFromAnyId(
?int $userId,
?string $userName,
?int $actorId,
$dbDomain = false
) : User {
return User::newFromAnyId( $userId, $userName, $actorId, $dbDomain );
}
/**
* @see User::newFromConfirmationCode
*
* @since 1.35
*
* @param string $confirmationCode
* @param int $flags
* @return User|null
*/
public function newFromConfirmationCode(
string $confirmationCode,
int $flags = self::READ_NORMAL
) {
return User::newFromConfirmationCode( $confirmationCode, $flags );
}
}