Skip to content

Commit 2b25872

Browse files
committed
refactor & add new features
1 parent 132d7f3 commit 2b25872

File tree

2 files changed

+79
-13
lines changed

2 files changed

+79
-13
lines changed

core/Form/Primitives/PrimitiveEnum.class.php

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
/**
1313
* @ingroup Primitives
1414
**/
15-
class PrimitiveEnum extends IdentifiablePrimitive
15+
class PrimitiveEnum extends IdentifiablePrimitive implements ListedPrimitive
1616
{
1717
public function getList()
1818
{
1919
if ($this->value)
20-
return ClassUtils::callStaticMethod(get_class($this->value).'::getObjectList');
20+
return ClassUtils::callStaticMethod(get_class($this->value).'::getList');
2121
elseif ($this->default)
22-
return ClassUtils::callStaticMethod(get_class($this->default).'::getObjectList');
22+
return ClassUtils::callStaticMethod(get_class($this->default).'::getList');
2323
else {
2424
$object = new $this->className(
2525
ClassUtils::callStaticMethod($this->className.'::getAnyId')
@@ -60,16 +60,11 @@ public function importValue(/* Identifiable */ $value)
6060

6161
public function import($scope)
6262
{
63-
if (!$this->className)
64-
throw new WrongStateException(
65-
"no class defined for PrimitiveEnum '{$this->name}'"
66-
);
67-
6863
$result = parent::import($scope);
6964

7065
if ($result === true) {
7166
try {
72-
$this->value = new $this->className($this->value);
67+
$this->value = $this->makeEnumById($this->value);
7368
} catch (MissingElementException $e) {
7469
$this->value = null;
7570

@@ -81,5 +76,57 @@ public function import($scope)
8176

8277
return $result;
8378
}
79+
80+
/**
81+
* @param $list
82+
* @throws UnsupportedMethodException
83+
*/
84+
public function setList($list)
85+
{
86+
throw new UnsupportedMethodException('you cannot set list here, it is impossible, because list getted from enum classes');
87+
}
88+
89+
/**
90+
* @return null|string
91+
*/
92+
public function getChoiceValue()
93+
{
94+
if(
95+
($value = $this->getValue() ) &&
96+
$value instanceof Enum
97+
)
98+
return $value->getName();
99+
100+
return null;
101+
}
102+
103+
104+
/**
105+
* @return Enum|mixed|null
106+
*/
107+
public function getActualChoiceValue()
108+
{
109+
if(
110+
!$this->getChoiceValue() &&
111+
$this->getDefault()
112+
)
113+
return $this->getDefault()->getName();
114+
115+
return null;
116+
}
117+
118+
/**
119+
* @param $id
120+
* @return Enum|mixed
121+
*/
122+
protected function makeEnumById($id)
123+
{
124+
if (!$this->className)
125+
throw new WrongStateException(
126+
"no class defined for PrimitiveEnum '{$this->name}'"
127+
);
128+
129+
return new $this->className($id);
130+
}
84131
}
85132
?>

test/core/PrimitiveEnumTest.class.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,35 @@ final class PrimitiveEnumTest extends TestCase
1313
{
1414
public function testIntegerValues()
1515
{
16+
$defaultId = 2;
17+
$importId = 1;
18+
19+
$default = MimeType::wrap($defaultId);
20+
$imported = MimeType::wrap($importId);
21+
1622
$form =
1723
Form::create()->
1824
add(
19-
Primitive::enum('enum')->of('MimeType')
25+
Primitive::enum('enum')
26+
->of('MimeType')
27+
->setDefault($default)
2028
);
2129

22-
$form->import(array('enum' => '1'));
30+
$form->import(array('enum' => $importId));
2331

24-
$this->assertEquals($form->getValue('enum')->getId(), 1);
25-
$this->assertSame($form->getValue('enum')->getId(), 1);
32+
$this->assertEquals($form->getValue('enum')->getId(), $importId);
33+
$this->assertSame($form->getValue('enum')->getId(), $importId);
34+
35+
$this->assertEquals($form->getChoiceValue('enum'), $imported->getName());
36+
$this->assertSame($form->getChoiceValue('enum'), $imported->getName());
37+
38+
$form->clean();
39+
40+
$this->assertNull($form->getValue('enum'));
41+
$this->assertNull($form->getChoiceValue('enum'));
42+
$this->assertEquals($form->getActualValue('enum')->getId(), $defaultId);
43+
$this->assertEquals($form->getActualChoiceValue('enum'), $default->getName());
44+
2645
}
2746

2847
public function testGetList()

0 commit comments

Comments
 (0)