Skip to content

Commit 87c2459

Browse files
committed
Initial commit
0 parents  commit 87c2459

File tree

5 files changed

+271
-0
lines changed

5 files changed

+271
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.project

LICENSE.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2012, Marc Bennewitz
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification,
5+
are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice,
8+
this list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of the organisation nor the names of its contributors
15+
may be used to endorse or promote products derived from this software
16+
without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
TODO
2+
3+
### New BSD License
4+
5+
The files in this archive are released under the New BSD License.
6+
You can find a copy of this license in LICENSE.txt file.

library/Enum.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* TODO: description
4+
*
5+
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
6+
* @copyright Copyright (c) 2012 Marc Bennewitz
7+
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
8+
*/
9+
10+
abstract class Enum
11+
{
12+
/**
13+
* The current selected value
14+
* @var mixed
15+
*/
16+
protected $value = null;
17+
18+
/**
19+
* An array of available constants
20+
* @var array
21+
*/
22+
private $constants = null;
23+
24+
/**
25+
* Constructor
26+
*
27+
* @param mixed $value The value to select
28+
* @throws InvalidArgumentException
29+
*/
30+
final public function __construct($value = null)
31+
{
32+
$reflectionClass = new \ReflectionClass($this);
33+
$this->constants = $reflectionClass->getConstants();
34+
35+
if (func_num_args() > 0) {
36+
$this->setValue($value);
37+
} elseif (!in_array($this->value, $this->constants, true)) {
38+
throw new InvalidArgumentException("No value given and no default value defined");
39+
}
40+
}
41+
42+
/**
43+
* Get all available constants
44+
* @return array
45+
*/
46+
final public function getConstants()
47+
{
48+
return $this->constants;
49+
}
50+
51+
/**
52+
* Select a new value
53+
* @param mixed $value
54+
* @throws InvalidArgumentException
55+
*/
56+
final public function setValue($value)
57+
{
58+
if (!in_array($value, $this->constants, true)) {
59+
throw new InvalidArgumentException("Unknown value '{$value}'");
60+
}
61+
$this->value = $value;
62+
}
63+
64+
/**
65+
* Get the current selected value
66+
* @return mixed
67+
*/
68+
final public function getValue()
69+
{
70+
return $this->value;
71+
}
72+
73+
/**
74+
* Select a new value by constant name
75+
* @param string $name
76+
* @throws InvalidArgumentException
77+
*/
78+
final public function setName($name)
79+
{
80+
if (!array_key_exists($name, $this->constants)) {
81+
throw new InvalidArgumentException("Unknown name '{$name}'");
82+
}
83+
$this->value = $this->constants[$name];
84+
}
85+
86+
/**
87+
* Get the current selected constant name
88+
* @return string
89+
*/
90+
final public function getName()
91+
{
92+
return array_search($this->value, $this->constants, true);
93+
}
94+
95+
/**
96+
* Get the current selected constant name
97+
* @return string
98+
* @see getName()
99+
*/
100+
final public function __toString()
101+
{
102+
return $this->getName();
103+
}
104+
105+
/**
106+
* Get the current selected value
107+
* @return mixed
108+
* @see getValue()
109+
*/
110+
final public function __invoke()
111+
{
112+
return $this->getValue();
113+
}
114+
}

tests/EnumTest.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* TODO: description
4+
*
5+
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
6+
* @copyright Copyright (c) 2012 Marc Bennewitz
7+
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
8+
*/
9+
10+
error_reporting(E_ALL | E_STRICT);
11+
12+
// init PHPUnit
13+
require_once 'PHPUnit/Framework/TestCase.php';
14+
if ('@package_version@' !== PHPUnit_Runner_Version::id() && version_compare(PHPUnit_Runner_Version::id(), '3.6.0', '<')) {
15+
echo 'This version of PHPUnit (' . PHPUnit_Runner_Version::id() . ') is not supported.' . PHP_EOL;
16+
exit(1);
17+
}
18+
19+
require_once dirname(__FILE__) . '/../library/Enum.php';
20+
21+
class EnumTest extends PHPUnit_Framework_TestCase
22+
{
23+
24+
public function testEnumWithDefaultValue()
25+
{
26+
$enum = new EnumWithDefaultValue();
27+
28+
$this->assertSame(array(
29+
'ONE' => 1,
30+
'TWO' => 2,
31+
), $enum->getConstants());
32+
33+
$this->assertSame(1, $enum->getValue());
34+
$this->assertSame(1, $enum->__invoke());
35+
36+
$this->assertSame('ONE', $enum->getName());
37+
$this->assertSame('ONE', $enum->__toString());
38+
}
39+
40+
public function testEnumWithNullAsDefaultValue()
41+
{
42+
$enum = new EnumWithNullAsDefaultValue();
43+
44+
$this->assertSame(array(
45+
'NONE' => null,
46+
'ONE' => 1,
47+
'TWO' => 2,
48+
), $enum->getConstants());
49+
50+
$this->assertNull($enum->getValue());
51+
$this->assertNull($enum->__invoke());
52+
53+
$this->assertSame('NONE', $enum->getName());
54+
$this->assertSame('NONE', $enum->__toString());
55+
}
56+
57+
public function testEnumWithoutDefaultValue()
58+
{
59+
$this->setExpectedException('InvalidArgumentException');
60+
new EnumWithoutDefaultValue();
61+
}
62+
63+
public function testChangeValueOnConstructor()
64+
{
65+
$enum = new EnumWithoutDefaultValue(1);
66+
67+
$this->assertSame(1, $enum->getValue());
68+
$this->assertSame(1, $enum->__invoke());
69+
70+
$this->assertSame('ONE', $enum->getName());
71+
$this->assertSame('ONE', $enum->__toString());
72+
}
73+
74+
public function testChangeValueOnConstructorThrowsInvalidArgumentExceptionOnStrictComparison()
75+
{
76+
$this->setExpectedException('InvalidArgumentException');
77+
$enum = new EnumWithoutDefaultValue('1');
78+
}
79+
80+
public function testSetValue()
81+
{
82+
$enum = new EnumWithDefaultValue();
83+
$enum->setValue(2);
84+
85+
$this->assertSame(2, $enum->getValue());
86+
$this->assertSame(2, $enum->__invoke());
87+
88+
$this->assertSame('TWO', $enum->getName());
89+
$this->assertSame('TWO', $enum->__toString());
90+
}
91+
92+
public function testSetValueThrowsInvalidArgumentExceptionOnStrictComparison()
93+
{
94+
$this->setExpectedException('InvalidArgumentException');
95+
$enum = new EnumWithDefaultValue();
96+
$enum->setValue('2');
97+
}
98+
99+
}
100+
101+
class EnumWithDefaultValue extends Enum
102+
{
103+
const ONE = 1;
104+
const TWO = 2;
105+
protected $value = 1;
106+
}
107+
108+
class EnumWithNullAsDefaultValue extends Enum
109+
{
110+
const NONE = null;
111+
const ONE = 1;
112+
const TWO = 2;
113+
}
114+
115+
116+
class EnumWithoutDefaultValue extends Enum
117+
{
118+
const ONE = 1;
119+
const TWO = 2;
120+
}
121+
122+
class EmptyEnum extends Enum
123+
{}

0 commit comments

Comments
 (0)