Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions core/Base/Enum.class.php
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);
Copy link
Member

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);

}

public function __construct($id)
{
$this->setInternalId($id);
}

/**
* @param $id
* @return Enum
* @throws MissingElementException
*/
protected function setInternalId($id)
{
$names = static::$names;

if (isset($names[$id])) {
Copy link
Member

Choose a reason for hiding this comment

The 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!');
}
}
?>
24 changes: 24 additions & 0 deletions core/Form/Primitive.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,5 +362,29 @@ public static function ipRange($name)
{
return new PrimitiveIpRange($name);
}

/**
* @return PrimitiveEnum
**/
public static function enum($name)
{
return new PrimitiveEnum($name);
}

/**
* @return PrimitiveEnumByValue
**/
public static function enumByValue($name)
{
return new PrimitiveEnumByValue($name);
}

/**
* @return PrimitiveEnumList
**/
public static function enumList($name)
{
return new PrimitiveEnumList($name);
}
}
?>
85 changes: 85 additions & 0 deletions core/Form/Primitives/PrimitiveEnum.class.php
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;
}
}
?>
48 changes: 48 additions & 0 deletions core/Form/Primitives/PrimitiveEnumByValue.class.php
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;
}
}
?>
Loading