szurubooru/src/Helpers/EnumHelper.php

110 lines
2.7 KiB
PHP
Raw Normal View History

2014-09-06 10:00:26 +02:00
<?php
namespace Szurubooru\Helpers;
use Szurubooru\Entities\Post;
use Szurubooru\Entities\Snapshot;
use Szurubooru\Entities\User;
2014-09-06 10:00:26 +02:00
class EnumHelper
{
2014-09-14 17:11:21 +02:00
private static $accessRankMap =
[
'anonymous' => User::ACCESS_RANK_ANONYMOUS,
'regularUser' => User::ACCESS_RANK_REGULAR_USER,
'powerUser' => User::ACCESS_RANK_POWER_USER,
'moderator' => User::ACCESS_RANK_MODERATOR,
'administrator' => User::ACCESS_RANK_ADMINISTRATOR,
2014-09-14 17:11:21 +02:00
];
private static $avatarStyleMap =
[
'gravatar' => User::AVATAR_STYLE_GRAVATAR,
'manual' => User::AVATAR_STYLE_MANUAL,
'none' => User::AVATAR_STYLE_BLANK,
'blank' => User::AVATAR_STYLE_BLANK,
2014-09-14 17:11:21 +02:00
];
2014-09-15 11:38:24 +02:00
private static $postSafetyMap =
[
'safe' => Post::POST_SAFETY_SAFE,
'sketchy' => Post::POST_SAFETY_SKETCHY,
'unsafe' => Post::POST_SAFETY_UNSAFE,
2014-09-15 11:38:24 +02:00
];
private static $postTypeMap =
[
'image' => Post::POST_TYPE_IMAGE,
'video' => Post::POST_TYPE_VIDEO,
'flash' => Post::POST_TYPE_FLASH,
'youtube' => Post::POST_TYPE_YOUTUBE,
2014-09-15 11:38:24 +02:00
];
2014-09-26 20:41:28 +02:00
private static $snapshotTypeMap =
[
'post' => Snapshot::TYPE_POST,
2014-09-26 20:41:28 +02:00
];
2014-09-06 10:00:26 +02:00
public static function accessRankToString($accessRank)
{
2014-09-14 17:11:21 +02:00
return self::enumToString(self::$accessRankMap, $accessRank);
2014-09-06 10:00:26 +02:00
}
public static function accessRankFromString($accessRankString)
{
2014-09-14 17:11:21 +02:00
return self::stringToEnum(self::$accessRankMap, $accessRankString);
}
public static function avatarStyleToString($avatarStyle)
{
return self::enumToString(self::$avatarStyleMap, $avatarStyle);
}
public static function avatarStyleFromString($avatarStyleString)
{
2014-09-14 17:11:21 +02:00
return self::stringToEnum(self::$avatarStyleMap, $avatarStyleString);
}
2014-09-15 11:38:24 +02:00
public static function postSafetyToString($postSafety)
{
return self::enumToString(self::$postSafetyMap, $postSafety);
}
public static function postSafetyFromString($postSafetyString)
{
return self::stringToEnum(self::$postSafetyMap, $postSafetyString);
}
public static function postTypeToString($postType)
{
return self::enumToString(self::$postTypeMap, $postType);
}
2014-10-03 13:43:09 +02:00
public static function postTypeFromString($postTypeString)
{
return self::stringToEnum(self::$postTypeMap, $postTypeString);
}
2014-09-26 20:41:28 +02:00
public static function snapshotTypeFromString($snapshotTypeString)
{
return self::stringToEnum(self::$snapshotTypeMap, $snapshotTypeString);
}
2014-09-14 17:11:21 +02:00
private static function enumToString($enumMap, $enumValue)
{
$reverseMap = array_flip($enumMap);
if (!isset($reverseMap[$enumValue]))
throw new \RuntimeException('Invalid value!');
return $reverseMap[$enumValue];
}
private static function stringToEnum($enumMap, $enumString)
{
$key = trim(strtolower($enumString));
2014-09-17 11:24:54 +02:00
$lowerEnumMap = array_change_key_case($enumMap, \CASE_LOWER);
if (!isset($lowerEnumMap[$key]))
2014-09-15 11:38:24 +02:00
throw new \DomainException('Unrecognized value: ' . $enumString);
2014-09-14 17:11:21 +02:00
2014-09-17 11:24:54 +02:00
return $lowerEnumMap[$key];
}
2014-09-06 10:00:26 +02:00
}