2017 © Pedro Peláez
 

library value

A base class for immutable single-value-wrapper classes

image

mle86/value

A base class for immutable single-value-wrapper classes

  • Monday, May 7, 2018
  • by mle86
  • Repository
  • 1 Watchers
  • 0 Stars
  • 238 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 42 % Grown

The README.md

php-value

Build Status Coverage Status Latest Stable Version PHP 7.0 License, (*1)

This PHP library provides a simple base class for Immutable Value Objects. 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., (*2)

It is released under the MIT License., (*3)

Simple use case:

class OddNumber extends \mle86\Value\AbstractValue
{

    // The base class requires this boolean test method:
    public static function isValid($input): bool
    {
        return (is_int($input) && ($input % 2) === 1);
    }

    // Nothing else is needed.
}

function myFunction(OddNumber $oddArgument)
{
    /* No further validation of $oddArgument is necessary in this function,
     * it's guaranteed to contain an odd number. */
    print "Got an odd number here: " . $oddArgument->value();
}

$odd1 = new OddNumber(61);       // works as expected, $odd1->value() will return 61
$odd2 = new OddNumber(40);       // throws an InvalidArgumentException
$odd3 = new OddNumber("string"); // throws an InvalidArgumentException
$odd4 = new OddNumber(null);     // throws an InvalidArgumentException

$odd5   = OddNumber::optional(33);   // works as expected, $odd5->value() will return 33
$nonodd = OddNumber::optional(null); // $nonodd is now null
$odd6   = OddNumber::optional(40);   // throws an InvalidArgumentException

Installation:

Via Composer: composer require mle86/value, (*4)

Or insert this into your project's composer.json file:, (*5)

"require": {
    "mle86/value": "^2"
}

Minimum PHP version:

PHP 7.0, (*6)

Classes and interfaces:

  1. Value (interface)
  2. AbstractValue (abstract class)
  3. AbstractSerializableValue (abstract class)
  4. InvalidArgumentException (exception)
  5. NotImplementedException (exception)

Value

This interface specifies that all Value classes should have * a constructor which takes exactly one argument, * a value() method without arguments., (*7)

AbstractValue

This immutable class wraps a single value per instance. The constructor enforces validity checks on the input value. Therefore, every class instance's wrapped value can be considered valid., (*8)

The validity checks are located in the isValid class method which all subclasses must implement. It is a class method to allow validity checks of external values without wrapping them in an instance., (*9)

  • public function __construct($rawValue), (*10)

    The constructor uses the isValid class method to test its input argument. Valid values are stored in the new instance, invalid values cause an InvalidArgumentException to be thrown. Other instances of the same class are always considered valid (re-wrapping)., (*11)

  • public static function optional($rawValue): ?static, (*12)

    Same as the default constructor, but also accepts null values (which will be returned unchanged)., (*13)

  • abstract public static function isValid($testValue): bool, (*14)

    Checks the validity of a raw value. If it returns true, a new object can be instantiated with that value. Implement this in every subclass!, (*15)

  • final public function value(): mixed, (*16)

    Returns the object's wrapped initializer value., (*17)

  • final public function equals($testValue): bool, (*18)

    Equality test. This method performs an equality check on other instances or raw values. Objects are considered equal if and only if they are instances of the same subclass and carry the same value(). All other values are considered equal if and only if they are identical (===) to the current objects's value()., (*19)

  • final public static function wrap(&$value), (*20)

    Replaces a value (by-reference) with instance wrapping that value. This means of course that the call will fail with an InvalidArgumentException if the input value fails the subclass' isValid check. If the value already is an instance, it won't be replaced., (*21)

  • final public static function wrapOptional(&$value), (*22)

    Like wrap(), but won't change null values., (*23)

  • final public static function wrapArray(array &$array): array, (*24)

    Will replace all values in an array with instances. The array will only be altered (by-reference) if all its values are valid. Array keys will be preserved., (*25)

  • final public static function wrapOptionalsArray(array &$array): array, (*26)

    Will replace all non-null values in an array with instances. The array will only be changed (by-reference) if all its values are valid (or null). Array keys will be preserved., (*27)

AbstractSerializableValue

This extension of AbstractValue provides easy serializability for the Value objects. It implements the JsonSerializable interface., (*28)

Standard PHP serialization via serialize/unserialize is always supported. This class contains an extra __wakeup() implementation to make sure that unserialized instances always contain a valid value., (*29)

  • public function __toString(): string, (*30)

    Returns the wrapped value like value(), but with an explicit string typecast. This allows string concatenation of Value objects., (*31)

  • public function jsonSerialize(): mixed, (*32)

    Returns the wrapped value – like value(). This enables json_encode() to encode the object., (*33)

InvalidArgumentException

An empty extension of PHP's InvalidArgumentException., (*34)

The Versions

07/05 2018

dev-master

9999999-dev

A base class for immutable single-value-wrapper classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

07/05 2018

dev-develop

dev-develop

A base class for immutable single-value-wrapper classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

07/05 2018

v1.1.0

1.1.0.0

A base class for immutable single-value-wrapper classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

24/09 2017

v1.0.4

1.0.4.0

A base class for immutable single-value-wrapper classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

11/04 2017

v1.0.3

1.0.3.0

A base class for immutable single-value-wrapper classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

11/04 2017

v1.0.2

1.0.2.0

A base class for immutable single-value-wrapper classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

12/01 2017

v1.0.1

1.0.1.0

A base class for immutable single-value-wrapper classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

24/05 2016

v1.0

1.0.0.0

A base class for immutable single-value-wrapper classes

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires