Skip to content

Commit 15cd338

Browse files
committed
added readme
1 parent c1e322f commit 15cd338

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# php-value
2+
3+
This PHP library provides a simple base class for Immutable Value Objects.
4+
Those are objects which wrap exactly one value, cannot be changed in any way, have no additional state and carry some validation logic in the constructor.
5+
6+
It is released under the [MIT License](http://opensource.org/licenses/MIT).
7+
8+
9+
# Simple use case:
10+
11+
```php
12+
class OddNumber extends \mle86\Value\AbstractValue {
13+
public static function IsValid ($input) {
14+
// The base class requires this boolean method.
15+
return (is_int($input) && ($input % 2) === 1);
16+
}
17+
// Nothing else is needed.
18+
}
19+
20+
function my_function (OddNumber $odd_argument) {
21+
/* No further validation of $odd_argument is necessary in this function,
22+
* it's guaranteed to be an odd number. */
23+
print "Got an odd number here: " . $odd_argument->value();
24+
}
25+
26+
$odd1 = new OddNumber (61); // works as expected, $odd1->value() will return 61
27+
$odd2 = new OddNumber (40); // throws an InvalidArgumentException
28+
```
29+
30+
31+
# Installation:
32+
33+
Via Composer: `$ ./composer.phar require mle86/value`
34+
35+
Or insert this into your project's `composer.json` file:
36+
37+
```js
38+
"require": {
39+
"mle86/value": "^1.0"
40+
}
41+
```
42+
43+
44+
# Minimum PHP version:
45+
46+
* PHP 5.4 is needed for the `AbstractSerializableValue` class, as it uses the `JsonSerializable` interface.
47+
48+
* PHP 5.3 is sufficient for the rest (the `Value` interface and `AbstractValue` base class).
49+
50+
51+
# Classes and interfaces:
52+
53+
1. [Value](#value) (interface)
54+
1. [AbstractValue](#abstractvalue) (abstract class)
55+
1. [AbstractSerializableValue](#abstractserializablevalue) (abstract class)
56+
1. [InvalidArgumentException](#invalidargumentexception) (exception)
57+
1. [NotImplementedException](#notimplementedexception) (exception)
58+
59+
60+
## Value
61+
62+
This interface specifies that all Value classes should have
63+
* a constructor which takes exactly one argument,
64+
* a value() method without arguments.
65+
66+
67+
## AbstractValue
68+
69+
This immutable class wraps a single value per instance.
70+
The constructor enforces validity checks on the input value.
71+
Therefore, every class instance's wrapped value can be considered valid.
72+
73+
The validity checks are located in the IsValid class method which all
74+
subclasses must implement. It is a class method to allow validity checks
75+
of external values without wrapping them in an instance.
76+
77+
78+
* `public function __construct ($raw_value);`
79+
80+
The constructor uses the subclass' `IsValid` method to test its input argument.
81+
Valid values are stored in the new instance, invalid values cause an `InvalidArgumentException` to be thrown.
82+
Other instance of the same class are always considered valid (*re-wrapping*).
83+
84+
* `public static function IsValid ($test_value);`
85+
86+
Checks the validity of a raw value. If it returns true, a new object can be instantiated with the same value.
87+
Implement this in every subclass!
88+
In the base class implementation, it simply throws a `NotImplementedException`.
89+
90+
* `final public function value ();`
91+
92+
Returns the object's wrapped initializer value.
93+
94+
* `final public function equals ($test_value);`
95+
96+
This method performs an equality check on other instances or raw values.
97+
Objects are considered equal if and only if they are instances of the same subclass and carry the same `value()`.
98+
All other values are considered equal if and only if they are identical (`===`) to the current objects's `value()`.
99+
100+
* `final public static function Wrap (&$value)`
101+
102+
Replaces a value (by-reference) with instance wrapping that value.
103+
This means of course that the call will fail with an `InvalidArgumentException` if the input value fails the subclass' `IsValid` check.
104+
If the value already is an instance, it won't be replaced.
105+
106+
* `final public static function WrapOrNull (&$value)`
107+
108+
Like `Wrap()`, but won't change `NULL` values.
109+
110+
* `final public static function WrapArray (array &$array)`
111+
112+
Will replace all values in an array with instances.
113+
The array will only be altered (by-reference) if all its values are valid.
114+
Array keys will be preserved.
115+
116+
* `final public static function WrapOrNullArray (array &$array)`
117+
118+
Will replace all non-`NULL` values in an array with instances.
119+
The array will only be changed (by-reference) if all its values are valid (or `NULL`).
120+
Array keys will be preserved.
121+
122+
123+
## AbstractSerializableValue
124+
125+
This extension of `AbstractValue` provides easy serializability for the Value objects.
126+
It implements the PHP 5.4 [JsonSerializable](https://php.net/manual/class.jsonserializable.php) interface.
127+
128+
* `public function __toString ();`
129+
130+
Returns the wrapped value -- like `value()`, but with an explicit `(string)` typecast.
131+
This allows string concatenation of Value objects.
132+
133+
* `public function jsonSerialize ();`
134+
135+
Returns the wrapped value -- like `value()`.
136+
This enables [json_encode()](https://secure.php.net/json_encode) to encode the Value object.
137+
138+
139+
## InvalidArgumentException
140+
141+
An empty extension of PHP's `InvalidArgumentException`.
142+
143+
144+
## NotImplementedException
145+
146+
An empy extension of PHP's `ErrorException`.
147+

0 commit comments

Comments
 (0)