Refactored enums

This commit is contained in:
Marcin Kurczewski 2014-05-18 22:24:31 +02:00
parent 4f33d0bd5b
commit 118cf31ab1
7 changed files with 36 additions and 16 deletions

View file

@ -25,7 +25,7 @@ class EditUserAccessRankJob extends AbstractJob
Logger::log('{user} changed {subject}\'s access rank to {rank}', [ Logger::log('{user} changed {subject}\'s access rank to {rank}', [
'user' => TextHelper::reprUser(Auth::getCurrentUser()), 'user' => TextHelper::reprUser(Auth::getCurrentUser()),
'subject' => TextHelper::reprUser($user), 'subject' => TextHelper::reprUser($user),
'rank' => $newAccessRank->toString()]); 'rank' => $newAccessRank->toDisplayString()]);
return $user; return $user;
} }

View file

@ -1,19 +1,12 @@
<?php <?php
abstract class Enum abstract class AbstractEnum implements IEnum
{ {
public abstract function toString(); public abstract function toString();
public function _toString($constant)
{
$cls = new ReflectionClass(get_called_class());
$constants = $cls->getConstants();
return array_search($constant, $constants);
}
public function toDisplayString() public function toDisplayString()
{ {
return TextCaseConverter::convert($this->toString(), return TextCaseConverter::convert($this->toString(),
TextCaseConverter::CAMEL_CASE, TextCaseConverter::SPINAL_CASE,
TextCaseConverter::BLANK_CASE); TextCaseConverter::BLANK_CASE);
} }

View file

@ -1,5 +1,5 @@
<?php <?php
class AccessRank extends Enum implements IValidatable class AccessRank extends AbstractEnum implements IEnum, IValidatable
{ {
const Anonymous = 0; const Anonymous = 0;
const Registered = 1; const Registered = 1;
@ -22,7 +22,16 @@ class AccessRank extends Enum implements IValidatable
public function toString() public function toString()
{ {
return self::_toString($this->accessRank); switch ($this->accessRank)
{
case self::Anonymous: return 'anonymous';
case self::Registered: return 'registered';
case self::PowerUser: return 'power-user';
case self::Moderator: return 'moderator';
case self::Admin: return 'admin';
case self::Nobody: return 'nobody';
}
return null;
} }
public static function getAll() public static function getAll()

6
src/Enums/IEnum.php Normal file
View file

@ -0,0 +1,6 @@
<?php
interface IEnum
{
public function toString();
public function toDisplayString();
}

View file

@ -1,5 +1,5 @@
<?php <?php
class PostSafety extends Enum implements IValidatable class PostSafety extends AbstractEnum implements IEnum, IValidatable
{ {
const Safe = 1; const Safe = 1;
const Sketchy = 2; const Sketchy = 2;

View file

@ -1,5 +1,5 @@
<?php <?php
class PostType extends Enum implements IValidatable class PostType extends AbstractEnum implements IEnum, IValidatable
{ {
const Image = 1; const Image = 1;
const Flash = 2; const Flash = 2;
@ -20,7 +20,14 @@ class PostType extends Enum implements IValidatable
public function toString() public function toString()
{ {
return self::_toString($this->type); switch ($this->type)
{
case self::Image: return 'image';
case self::Flash: return 'flash';
case self::Youtube: return 'youtube';
case self::Video: return 'video';
}
return null;
} }
public function validate() public function validate()

View file

@ -1,5 +1,5 @@
<?php <?php
class Privilege extends Enum class Privilege extends AbstractEnum implements IEnum
{ {
const ListPosts = 'listPosts'; const ListPosts = 'listPosts';
const ViewPost = 'viewPost'; const ViewPost = 'viewPost';
@ -71,4 +71,9 @@ class Privilege extends Enum
$string .= '.' . $this->secondary; $string .= '.' . $this->secondary;
return $string; return $string;
} }
public function toDisplayString()
{
return $this->toString();
}
} }