-
Notifications
You must be signed in to change notification settings - Fork 52
Static Enumeration vs Non-Static Enumeration Battle ;) #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?php | ||
/*************************************************************************** | ||
* Copyright (C) 2012 by Georgiy T. Kutsurua * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU Lesser General Public License as * | ||
* published by the Free Software Foundation; either version 3 of the * | ||
* License, or (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
/** | ||
* Parent of all enumeration classes. | ||
* | ||
* @see MimeType for example | ||
* | ||
* @ingroup Base | ||
* @ingroup Module | ||
**/ | ||
abstract class Enum extends NamedObject | ||
implements | ||
Serializable | ||
{ | ||
protected static $names = array(/* override me */); | ||
|
||
/** | ||
* @param integer $id | ||
* @return Enum | ||
*/ | ||
public static function create($id) | ||
{ | ||
$className = get_called_class(); | ||
return new $className($id); | ||
} | ||
|
||
public function __construct($id) | ||
{ | ||
$this->setInternalId($id); | ||
} | ||
|
||
/** | ||
* @param $id | ||
* @return Enum | ||
* @throws MissingElementException | ||
*/ | ||
protected function setInternalId($id) | ||
{ | ||
$names = static::$names; | ||
|
||
if (isset($names[$id])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а isset(static::$names[$id]) не работает? |
||
$this->id = $id; | ||
$this->name = $names[$id]; | ||
} else | ||
throw new MissingElementException( | ||
'knows nothing about such id == '.$id | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function serialize() | ||
{ | ||
return (string) $this->id; | ||
} | ||
|
||
/** | ||
* @param $serialized | ||
*/ | ||
public function unserialize($serialized) | ||
{ | ||
$this->setInternalId($serialized); | ||
} | ||
|
||
/** | ||
* Array of object | ||
* @static | ||
* @return array | ||
*/ | ||
public static function getList() | ||
{ | ||
$list = array(); | ||
foreach (array_keys(static::$names) as $id) | ||
$list[] = static::create($id); | ||
|
||
return $list; | ||
} | ||
|
||
/** | ||
* must return any existent ID | ||
* 1 should be ok for most enumerations | ||
* @return integer | ||
**/ | ||
public static function getAnyId() | ||
{ | ||
return 1; | ||
} | ||
|
||
/** | ||
* @return null|integer | ||
*/ | ||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
|
||
/** | ||
* Alias for getList() | ||
* @static | ||
* @deprecated | ||
* @return array | ||
*/ | ||
public static function getObjectList() | ||
{ | ||
return static::getList(); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function toString() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* Plain list | ||
* @static | ||
* @return array | ||
*/ | ||
public static function getNameList() | ||
{ | ||
return static::$names; | ||
} | ||
|
||
/** | ||
* @return Enum | ||
**/ | ||
public function setId($id) | ||
{ | ||
throw new UnsupportedMethodException('You can not change id here, because it is politics for Enum!'); | ||
} | ||
} | ||
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/*************************************************************************** | ||
* Copyright (C) 2012 by Georgiy T. Kutsurua * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU Lesser General Public License as * | ||
* published by the Free Software Foundation; either version 3 of the * | ||
* License, or (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
/** | ||
* @ingroup Primitives | ||
**/ | ||
class PrimitiveEnum extends IdentifiablePrimitive | ||
{ | ||
public function getList() | ||
{ | ||
if ($this->value) | ||
return ClassUtils::callStaticMethod(get_class($this->value).'::getObjectList'); | ||
elseif ($this->default) | ||
return ClassUtils::callStaticMethod(get_class($this->default).'::getObjectList'); | ||
else { | ||
$object = new $this->className( | ||
ClassUtils::callStaticMethod($this->className.'::getAnyId') | ||
); | ||
|
||
return $object->getObjectList(); | ||
} | ||
|
||
Assert::isUnreachable(); | ||
} | ||
|
||
/** | ||
* @throws WrongArgumentException | ||
* @return PrimitiveEnum | ||
**/ | ||
public function of($class) | ||
{ | ||
$className = $this->guessClassName($class); | ||
|
||
Assert::classExists($className); | ||
|
||
Assert::isInstance($className, 'Enum'); | ||
|
||
$this->className = $className; | ||
|
||
return $this; | ||
} | ||
|
||
public function importValue(/* Identifiable */ $value) | ||
{ | ||
if ($value) | ||
Assert::isEqual(get_class($value), $this->className); | ||
else | ||
return parent::importValue(null); | ||
|
||
return $this->import(array($this->getName() => $value->getId())); | ||
} | ||
|
||
public function import($scope) | ||
{ | ||
if (!$this->className) | ||
throw new WrongStateException( | ||
"no class defined for PrimitiveEnum '{$this->name}'" | ||
); | ||
|
||
$result = parent::import($scope); | ||
|
||
if ($result === true) { | ||
try { | ||
$this->value = new $this->className($this->value); | ||
} catch (MissingElementException $e) { | ||
$this->value = null; | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return $result; | ||
} | ||
} | ||
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/*************************************************************************** | ||
* Copyright (C) 2012 by Georgiy T. Kutsurua * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU Lesser General Public License as * | ||
* published by the Free Software Foundation; either version 3 of the * | ||
* License, or (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
/** | ||
* @ingroup Primitives | ||
**/ | ||
final class PrimitiveEnumByValue extends PrimitiveEnum | ||
{ | ||
public function import($scope) | ||
{ | ||
if (!$this->className) | ||
throw new WrongStateException( | ||
"no class defined for PrimitiveEnum '{$this->name}'" | ||
); | ||
|
||
if (isset($scope[$this->name])) { | ||
$scopedValue = urldecode($scope[$this->name]); | ||
|
||
$names = ClassUtils::callStaticMethod($this->className.'::getNameList'); | ||
|
||
foreach ($names as $key => $value) { | ||
if ($value == $scopedValue) { | ||
try { | ||
$this->value = new $this->className($key); | ||
} catch (MissingElementException $e) { | ||
$this->value = null; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
?> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Еще раз рассматривал классы, вот тут вот можно сделать не через get_called_class(), а просто:
return new static($id);