Added additional type safety
This commit is contained in:
parent
e6073ba7c7
commit
20b3dfc76d
7 changed files with 54 additions and 50 deletions
|
@ -20,7 +20,7 @@ class UserViewProxy extends AbstractViewProxy
|
|||
$result->accessRank = \Szurubooru\Helpers\EnumHelper::accessRankToString($user->getAccessRank());
|
||||
$result->registrationTime = $user->getRegistrationTime();
|
||||
$result->lastLoginTime = $user->getLastLoginTime();
|
||||
$result->avatarStyle = $user->getAvatarStyle();
|
||||
$result->avatarStyle = \Szurubooru\Helpers\EnumHelper::avatarStyleToString($user->getAvatarStyle());
|
||||
|
||||
if ($this->privilegeService->isLoggedIn($user))
|
||||
{
|
||||
|
|
|
@ -7,7 +7,7 @@ abstract class Entity
|
|||
|
||||
public function __construct($id = null)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->id = $id === null ? null : intval($id);
|
||||
}
|
||||
|
||||
public function getId()
|
||||
|
|
|
@ -3,9 +3,9 @@ namespace Szurubooru\Entities;
|
|||
|
||||
final class Token extends Entity
|
||||
{
|
||||
const PURPOSE_LOGIN = 0;
|
||||
const PURPOSE_ACTIVATE = 1;
|
||||
const PURPOSE_PASSWORD_RESET = 2;
|
||||
const PURPOSE_LOGIN = 1;
|
||||
const PURPOSE_ACTIVATE = 2;
|
||||
const PURPOSE_PASSWORD_RESET = 3;
|
||||
|
||||
protected $name;
|
||||
protected $purpose;
|
||||
|
@ -28,7 +28,7 @@ final class Token extends Entity
|
|||
|
||||
public function setPurpose($purpose)
|
||||
{
|
||||
$this->purpose = $purpose;
|
||||
$this->purpose = intval($purpose);
|
||||
}
|
||||
|
||||
public function getAdditionalData()
|
||||
|
|
|
@ -10,9 +10,9 @@ final class User extends Entity
|
|||
const ACCESS_RANK_MODERATOR = 4;
|
||||
const ACCESS_RANK_ADMINISTRATOR = 5;
|
||||
|
||||
const AVATAR_STYLE_GRAVATAR = 'gravatar';
|
||||
const AVATAR_STYLE_MANUAL = 'manual';
|
||||
const AVATAR_STYLE_BLANK = 'blank';
|
||||
const AVATAR_STYLE_GRAVATAR = 1;
|
||||
const AVATAR_STYLE_MANUAL = 2;
|
||||
const AVATAR_STYLE_BLANK = 3;
|
||||
|
||||
protected $name;
|
||||
protected $email;
|
||||
|
@ -82,7 +82,7 @@ final class User extends Entity
|
|||
|
||||
public function setAccessRank($accessRank)
|
||||
{
|
||||
$this->accessRank = $accessRank;
|
||||
$this->accessRank = intval($accessRank);
|
||||
}
|
||||
|
||||
public function getRegistrationTime()
|
||||
|
@ -112,7 +112,7 @@ final class User extends Entity
|
|||
|
||||
public function setAvatarStyle($avatarStyle)
|
||||
{
|
||||
$this->avatarStyle = $avatarStyle;
|
||||
$this->avatarStyle = intval($avatarStyle);
|
||||
}
|
||||
|
||||
public function getBrowsingSettings()
|
||||
|
|
|
@ -3,55 +3,58 @@ namespace Szurubooru\Helpers;
|
|||
|
||||
class EnumHelper
|
||||
{
|
||||
private static $accessRankMap =
|
||||
[
|
||||
'anonymous' => \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS,
|
||||
'regularUser' => \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER,
|
||||
'powerUser' => \Szurubooru\Entities\User::ACCESS_RANK_POWER_USER,
|
||||
'moderator' => \Szurubooru\Entities\User::ACCESS_RANK_MODERATOR,
|
||||
'administrator' => \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR,
|
||||
];
|
||||
|
||||
private static $avatarStyleMap =
|
||||
[
|
||||
'gravatar' => \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR,
|
||||
'manual' => \Szurubooru\Entities\User::AVATAR_STYLE_MANUAL,
|
||||
'none' => \Szurubooru\Entities\User::AVATAR_STYLE_BLANK,
|
||||
'blank' => \Szurubooru\Entities\User::AVATAR_STYLE_BLANK,
|
||||
];
|
||||
|
||||
public static function accessRankToString($accessRank)
|
||||
{
|
||||
$map =
|
||||
[
|
||||
\Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS => 'anonymous',
|
||||
\Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER => 'regularUser',
|
||||
\Szurubooru\Entities\User::ACCESS_RANK_POWER_USER => 'powerUser',
|
||||
\Szurubooru\Entities\User::ACCESS_RANK_MODERATOR => 'moderator',
|
||||
\Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR => 'administrator',
|
||||
];
|
||||
|
||||
if (!isset($map[$accessRank]))
|
||||
throw new \DomainException('Invalid access rank!');
|
||||
|
||||
return $map[$accessRank];
|
||||
return self::enumToString(self::$accessRankMap, $accessRank);
|
||||
}
|
||||
|
||||
public static function accessRankFromString($accessRankString)
|
||||
{
|
||||
$map =
|
||||
[
|
||||
'anonymous' => \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS,
|
||||
'regularUser' => \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER,
|
||||
'powerUser' => \Szurubooru\Entities\User::ACCESS_RANK_POWER_USER,
|
||||
'moderator' => \Szurubooru\Entities\User::ACCESS_RANK_MODERATOR,
|
||||
'administrator' => \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR,
|
||||
];
|
||||
return self::stringToEnum(self::$accessRankMap, $accessRankString);
|
||||
}
|
||||
|
||||
$key = trim(strtolower($accessRankString));
|
||||
if (!isset($map[$key]))
|
||||
throw new \DomainException('Unrecognized access rank: ' . $accessRankString);
|
||||
|
||||
return $map[$key];
|
||||
public static function avatarStyleToString($avatarStyle)
|
||||
{
|
||||
return self::enumToString(self::$avatarStyleMap, $avatarStyle);
|
||||
}
|
||||
|
||||
public static function avatarStyleFromString($avatarStyleString)
|
||||
{
|
||||
$map =
|
||||
[
|
||||
'gravatar' => \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR,
|
||||
'manual' => \Szurubooru\Entities\User::AVATAR_STYLE_MANUAL,
|
||||
'none' => \Szurubooru\Entities\User::AVATAR_STYLE_BLANK,
|
||||
'blank' => \Szurubooru\Entities\User::AVATAR_STYLE_BLANK,
|
||||
];
|
||||
return self::stringToEnum(self::$avatarStyleMap, $avatarStyleString);
|
||||
}
|
||||
|
||||
$key = trim(strtolower($avatarStyleString));
|
||||
if (!isset($map[$key]))
|
||||
throw new \DomainException('Unrecognized avatar style: ' . $avatarStyleString);
|
||||
private static function enumToString($enumMap, $enumValue)
|
||||
{
|
||||
$reverseMap = array_flip($enumMap);
|
||||
if (!isset($reverseMap[$enumValue]))
|
||||
throw new \RuntimeException('Invalid value!');
|
||||
|
||||
return $map[$key];
|
||||
return $reverseMap[$enumValue];
|
||||
}
|
||||
|
||||
private static function stringToEnum($enumMap, $enumString)
|
||||
{
|
||||
$key = trim(strtolower($enumString));
|
||||
if (!isset($enumMap[$key]))
|
||||
throw new \DomainException('Unrecognized avatar style: ' . $enumString);
|
||||
|
||||
return $enumMap[$key];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,6 +75,7 @@ class AuthService
|
|||
$user = new \Szurubooru\Entities\User();
|
||||
$user->setName('Anonymous user');
|
||||
$user->setAccessRank(\Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS);
|
||||
$user->setAvatarStyle(\Szurubooru\Entities\User::AVATAR_STYLE_BLANK);
|
||||
return $user;
|
||||
}
|
||||
|
||||
|
|
|
@ -58,8 +58,8 @@ class PrivilegeServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|||
|
||||
public function testIsLoggedInByUserId()
|
||||
{
|
||||
$testUser1 = new \Szurubooru\Entities\User('dummy');
|
||||
$testUser2 = new \Szurubooru\Entities\User('godzilla');
|
||||
$testUser1 = new \Szurubooru\Entities\User(5);
|
||||
$testUser2 = new \Szurubooru\Entities\User(13);
|
||||
$this->authServiceMock->expects($this->atLeastOnce())->method('getLoggedInUser')->willReturn($testUser1);
|
||||
|
||||
$privilegeService = $this->getPrivilegeService();
|
||||
|
|
Loading…
Reference in a new issue