Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
144 changes: 144 additions & 0 deletions core/Base/Enum.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?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)
{
return new static($id);
}

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

/**
* @param $id
* @return Enum
* @throws MissingElementException
*/
protected function setInternalId($id)
{
if (isset(static::$names[$id])) {
$this->id = $id;
$this->name = static::$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);
}
}
?>
132 changes: 132 additions & 0 deletions core/Form/Primitives/PrimitiveEnum.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?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 implements ListedPrimitive
{
public function getList()
{
if ($this->value)
return ClassUtils::callStaticMethod(get_class($this->value).'::getList');
elseif ($this->default)
return ClassUtils::callStaticMethod(get_class($this->default).'::getList');
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)
{
$result = parent::import($scope);

if ($result === true) {
try {
$this->value = $this->makeEnumById($this->value);
} catch (MissingElementException $e) {
$this->value = null;

return false;
}

return true;
}

return $result;
}

/**
* @param $list
* @throws UnsupportedMethodException
*/
public function setList($list)
{
throw new UnsupportedMethodException('you cannot set list here, it is impossible, because list getted from enum classes');
}

/**
* @return null|string
*/
public function getChoiceValue()
{
if(
($value = $this->getValue() ) &&
$value instanceof Enum
)
return $value->getName();

return null;
}


/**
* @return Enum|mixed|null
*/
public function getActualChoiceValue()
{
if(
!$this->getChoiceValue() &&
$this->getDefault()
)
return $this->getDefault()->getName();

return null;
}

/**
* @param $id
* @return Enum|mixed
*/
protected function makeEnumById($id)
{
if (!$this->className)
throw new WrongStateException(
"no class defined for PrimitiveEnum '{$this->name}'"
);

return new $this->className($id);
}
}
?>
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