2
2
3
3
* An enumeration implementation for PHP.*
4
4
5
- [ ![ Build Status]] ( http://travis-ci.org/eloquent/enumeration )
6
- [ ![ Test Coverage]] ( http://eloquent-software.com/enumeration/artifacts/tests/ coverage/ )
5
+ [ ![ Build Status]] [ Latest build ]
6
+ [ ![ Test Coverage]] [ Test coverage report ]
7
7
8
- ## Installation
8
+ ## Installation and documentation
9
9
10
- Available as [ Composer] ( http://getcomposer.org/ ) package
11
- [ eloquent/enumeration ] ( https://packagist.org/packages/eloquent/enumeration ) .
10
+ * Available as [ Composer] package [ eloquent/enumeration ] .
11
+ * [ API documentation ] available .
12
12
13
13
## What is an Enumeration?
14
14
@@ -22,32 +22,32 @@ is considered invalid.
22
22
23
23
## A basic example
24
24
25
- Enumeration can be used like [ C++ enumerated types] ( http://www.learncpp.com/cpp-tutorial/45-enumerated-types/ ) .
26
- Here is an example, representing a set of HTTP request methods:
25
+ Enumeration can be used like [ C++ enumerated types] . Here is an example,
26
+ representing a set of HTTP request methods:
27
27
28
28
``` php
29
29
use Eloquent\Enumeration\Enumeration;
30
30
31
- final class HTTPRequestMethod extends Enumeration
31
+ final class HttpRequestMethod extends Enumeration
32
32
{
33
- const OPTIONS = 'OPTIONS';
34
- const GET = 'GET';
35
- const HEAD = 'HEAD';
36
- const POST = 'POST';
37
- const PUT = 'PUT';
38
- const DELETE = 'DELETE';
39
- const TRACE = 'TRACE';
40
- const CONNECT = 'CONNECT';
33
+ const OPTIONS = 'OPTIONS';
34
+ const GET = 'GET';
35
+ const HEAD = 'HEAD';
36
+ const POST = 'POST';
37
+ const PUT = 'PUT';
38
+ const DELETE = 'DELETE';
39
+ const TRACE = 'TRACE';
40
+ const CONNECT = 'CONNECT';
41
41
}
42
42
```
43
43
44
44
This class can now be used in a type hint to easily accept any valid HTTP
45
45
request method:
46
46
47
47
``` php
48
- function handleHttpRequest(HTTPRequestMethod $method, $url, $body = NULL )
48
+ function handleHttpRequest(HttpRequestMethod $method, $url, $body = null )
49
49
{
50
- // handle request...
50
+ // handle request...
51
51
}
52
52
```
53
53
@@ -56,39 +56,35 @@ function handleHttpRequest(HTTPRequestMethod $method, $url, $body = NULL)
56
56
Members are accessed by static method calls, like so:
57
57
58
58
``` php
59
- handleHttpRequest(HTTPRequestMethod ::GET(), 'http://example.org/');
60
- handleHttpRequest(HTTPRequestMethod ::POST(), 'http://example.org/', 'foo=bar&baz=qux');
59
+ handleHttpRequest(HttpRequestMethod ::GET(), 'http://example.org/');
60
+ handleHttpRequest(HttpRequestMethod ::POST(), 'http://example.org/', 'foo=bar&baz=qux');
61
61
```
62
62
63
63
For each member of the enumeration, a single instance of the enumeration class
64
- is instantiated (that is, an instance of * HTTPRequestMethod * in the above
64
+ is instantiated (that is, an instance of * HttpRequestMethod * in the above
65
65
example). This means that strict comparison (===) can be used to determine
66
66
which member has been passed to a function:
67
67
68
68
``` php
69
- function handleHttpRequest(HTTPRequestMethod $method, $url, $body = NULL )
69
+ function handleHttpRequest(HttpRequestMethod $method, $url, $body = null )
70
70
{
71
- if ($method === HTTPRequestMethod::POST())
72
- {
73
- // handle POST requests...
74
- }
75
- else
76
- {
77
- // handle other requests...
78
- }
71
+ if ($method === HttpRequestMethod::POST()) {
72
+ // handle POST requests...
73
+ } else {
74
+ // handle other requests...
75
+ }
79
76
}
80
77
```
81
78
82
79
## Java-style enumerations
83
80
84
- [ Java's enum types] ( http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html )
85
- have slightly more functionality than C++ enumerated types. They can have
86
- additional properties and/or methods, and are really just a specialised kind of
87
- class where there are a fixed set of instances.
81
+ [ Java's enum types] have slightly more functionality than C++ enumerated types.
82
+ They can have additional properties and/or methods, and are really just a
83
+ specialised kind of class where there are a fixed set of instances.
88
84
89
- This is sometimes called the [ Multiton] ( http://en.wikipedia.org/wiki/Multiton_pattern )
90
- pattern, and in fact, all enumerations in this implementation are Multitons.
91
- The * Enumeration * class simply defines its members based upon class constants.
85
+ This is sometimes called the [ Multiton] pattern, and in fact, all enumerations
86
+ in this implementation are Multitons. The * Enumeration * class simply defines its
87
+ members based upon class constants.
92
88
93
89
Here is an example borrowed from the Java documentation for its enum types. The
94
90
following multiton describes all of the planets in our solar system, including
@@ -99,83 +95,84 @@ use Eloquent\Enumeration\Multiton;
99
95
100
96
final class Planet extends Multiton
101
97
{
102
- /**
103
- * Universal gravitational constant
104
- *
105
- * @var float
106
- */
107
- const G = 6.67300E-11;
108
-
109
- /**
110
- * @return float
111
- */
112
- public function surfaceGravity()
113
- {
114
- return self::G * $this->mass / ($this->radius * $this->radius);
115
- }
116
-
117
- /**
118
- * @param float $otherMass
119
- *
120
- * @return float
121
- */
122
- public function surfaceWeight($otherMass)
123
- {
124
- return $otherMass * $this->surfaceGravity();
125
- }
126
-
127
- protected static function initializeMultiton ()
128
- {
129
- parent::initializeMultiton ();
130
-
131
- new static('MERCURY', 3.302e23, 2.4397e6);
132
- new static('VENUS', 4.869e24, 6.0518e6);
133
- new static('EARTH', 5.9742e24, 6.37814e6);
134
- new static('MARS', 6.4191e23, 3.3972e6);
135
- new static('JUPITER', 1.8987e27, 7.1492e7);
136
- new static('SATURN', 5.6851e26, 6.0268e7);
137
- new static('URANUS', 8.6849e25, 2.5559e7);
138
- new static('NEPTUNE', 1.0244e26, 2.4764e7);
139
- // new static('PLUTO', 1.31e22, 1.180e6);
140
- }
141
-
142
- /**
143
- * @param string $key
144
- * @param float $mass
145
- * @param float $radius
146
- */
147
- protected function __construct($key, $mass, $radius)
148
- {
149
- parent::__construct($key);
150
-
151
- $this->mass = $mass;
152
- $this->radius = $radius;
153
- }
154
-
155
- /**
156
- * @var float
157
- */
158
- private $mass;
159
-
160
- /**
161
- * @var float
162
- */
163
- private $radius;
98
+ /**
99
+ * Universal gravitational constant
100
+ *
101
+ * @var float
102
+ */
103
+ const G = 6.67300E-11;
104
+
105
+ /**
106
+ * @return float
107
+ */
108
+ public function surfaceGravity()
109
+ {
110
+ return self::G * $this->mass / ($this->radius * $this->radius);
111
+ }
112
+
113
+ /**
114
+ * @param float $otherMass
115
+ *
116
+ * @return float
117
+ */
118
+ public function surfaceWeight($otherMass)
119
+ {
120
+ return $otherMass * $this->surfaceGravity();
121
+ }
122
+
123
+ protected static function initializeMembers ()
124
+ {
125
+ parent::initializeMembers ();
126
+
127
+ new static('MERCURY', 3.302e23, 2.4397e6);
128
+ new static('VENUS', 4.869e24, 6.0518e6);
129
+ new static('EARTH', 5.9742e24, 6.37814e6);
130
+ new static('MARS', 6.4191e23, 3.3972e6);
131
+ new static('JUPITER', 1.8987e27, 7.1492e7);
132
+ new static('SATURN', 5.6851e26, 6.0268e7);
133
+ new static('URANUS', 8.6849e25, 2.5559e7);
134
+ new static('NEPTUNE', 1.0244e26, 2.4764e7);
135
+ // new static('PLUTO', 1.31e22, 1.180e6);
136
+ }
137
+
138
+ /**
139
+ * @param string $key
140
+ * @param float $mass
141
+ * @param float $radius
142
+ */
143
+ protected function __construct($key, $mass, $radius)
144
+ {
145
+ parent::__construct($key);
146
+
147
+ $this->mass = $mass;
148
+ $this->radius = $radius;
149
+ }
150
+
151
+ /**
152
+ * @var float
153
+ */
154
+ private $mass;
155
+
156
+ /**
157
+ * @var float
158
+ */
159
+ private $radius;
164
160
}
165
161
```
166
162
167
163
The above class can be used to take a known weight on earth (in any unit) and
168
164
calculate the weight on all of the planets (in the same unit):
169
165
170
166
``` php
171
- require 'Planet.php';
172
-
173
167
$earthWeight = 175;
174
168
$mass = $earthWeight / Planet::EARTH()->surfaceGravity();
175
169
176
- foreach (Planet::multitonInstances() as $planet)
177
- {
178
- echo sprintf('Your weight on %s is %f' . PHP_EOL, $planet, $planet->surfaceWeight($mass));
170
+ foreach (Planet::members() as $planet) {
171
+ echo sprintf(
172
+ 'Your weight on %s is %f' . PHP_EOL,
173
+ $planet,
174
+ $planet->surfaceWeight($mass)
175
+ );
179
176
}
180
177
```
181
178
@@ -192,6 +189,17 @@ Your weight on URANUS is 158.424919
192
189
Your weight on NEPTUNE is 199.055584
193
190
```
194
191
195
- <!-- references -->
196
- [ Build Status ] : https://raw.github.com/eloquent/enumeration/gh-pages/artifacts/images/icecave/regular/build-status.png
197
- [ Test Coverage ] : https://raw.github.com/eloquent/enumeration/gh-pages/artifacts/images/icecave/regular/coverage.png
192
+ <!-- References -->
193
+
194
+ [ API documentation ] : http://lqnt.co/enumeration/artifacts/documentation/api/
195
+ [ C++ enumerated types ] : http://www.learncpp.com/cpp-tutorial/45-enumerated-types/
196
+ [ Composer ] : http://getcomposer.org/
197
+ [ eloquent/enumeration ] : https://packagist.org/packages/eloquent/enumeration
198
+ [ enumeration ] : https://github.com/eloquent/enumeration
199
+ [ Java's enum types ] : http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
200
+ [ Multiton ] : http://en.wikipedia.org/wiki/Multiton_pattern
201
+
202
+ [ Build Status ] : https://api.travis-ci.org/eloquent/enumeration.png?branch=master
203
+ [ Latest build ] : https://travis-ci.org/eloquent/enumeration
204
+ [ Test coverage report ] : https://coveralls.io/r/eloquent/enumeration
205
+ [ Test Coverage ] : https://coveralls.io/repos/eloquent/enumeration/badge.png?branch=master
0 commit comments