2017 © Pedro Peláez
 

library valueobject

Value objects

image

yomy/valueobject

Value objects

  • Friday, June 22, 2018
  • by yomy
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

valueobject

Value Objects and Enums, (*1)

Build Status, (*2)

Installation and documentation

  • Available as [Composer] package [yomy/valueobject].

What is this library about

This library adds a ValueObject and EnumValueObject classes., (*3)

Examples of value object

Creating an object:, (*4)

use YomY\ValueObject\ValueObject;
$object = ValueObject::instance(1);

Getting value from the object, (*5)

$value = $object->getValue();

Objects with the same value are going to be the same object, (*6)

$object1 = ValueObject::instance(1);
$object2 = ValueObject::instance(1);
//These two are the same objects ($object1 === $object2)

You can use type hinting in methods, (*7)

public function doSomething(ValueObject $valueObject) {
    $value = $valueObject->getValue();
    ...
}

You can extend the object for more detailed type hinting, (*8)

class UserId extends ValueObject {}
class DataId extends ValueObject {}
...
public function doSomething(UserId $userId, DataId $dataId) {
    ...
}

Objects of different class or variable type are different, (*9)

$object1 = ValueObject::instance('');
$object2 = ValueObject::instance(null);
$object3 = ValueObject::instance(false);
$object4 = ExtendedValueObject::instance('');
$object5 = ExtendedValueObject::instance(null);
$object6 = ExtendedValueObject::instance(false);
//All of the above are different

Instead of a strong (===) comparison operator, you could also use the equals() method, (*10)

$object1 = ValueObject::instance(1);
$object2 = ValueObject::instance(1);
$same = $object1->equals($object2); //true

Generally, unserialize of a value object is prohibited, as this would break the ability to compare objects by reference. However, you might have a case you don't care about strict comparison, and need to unserialize the object. You can add a WeakValueObjectTrait usage to your custom object, which will allow unserializing it, and also compare objects by the values instead of reference when using equals() method, (*11)

class MyWeakObject extends ValueObject {
    use WeakValueObjectTrait;
}
...
$weakObject1 = MyWeakObject::instance(1);
$serializedWeakObject = serialize($weakObject1);
$weakObject2 = unserialize($serializedWeakObject);
//These two objects are "equal" but not the same
$weakObject1->equals($weakObject2); //true
//On regular value objects this would be false

Examples of Enum value object

Creating an enum object, (*12)

use YomY\ValueObject\EnumValueObject;
class Category extends EnumValueObject {
    const FIRST = 1;
    const SECOND = 2;
    const THIRD = 3;
}

Creating enum objects, (*13)

$category = Category::instance(Category::FIRST);

or by referring referring to key, (*14)

$category = Category::FIRST();

You will get an error if you try to instantiate invalid value, (*15)

$category = Category::instance('missing_value');
$category = Category::MISSING();

Examples of Positive Integer value object

As value objects are commonly used as identifiers for database entities with an integer key, positive int value object ensures a valid key object for this purpose, (*16)

Creating an object:, (*17)

use YomY\ValueObject\PositiveIntValueObject;
$object1 = PositiveIntValueObject::instance(1);
$object2 = PositiveIntValueObject::instance('1');
//These two are the same objects ($object1 === $object2)

Usually, the id key in the db cannot be a 0, so these objects are invalid:, (*18)

$object = PositiveIntValueObject::instance(0);
$object = PositiveIntValueObject::instance('0');

Of course, as with a basic value object, it is intended to use these in extended classes., (*19)

class UserId extends PositiveIntValueObject {}
class DataId extends PositiveIntValueObject {}
$user = UserId::instance(42);
$data = DataId::instance(42);
//these two are not the same

The Versions

22/06 2018

dev-master

9999999-dev https://github.com/yomy/valueobject

Value objects

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0

 

The Development Requires

enum ddd value-object valueobject

22/06 2018

2.0

2.0.0.0 https://github.com/yomy/valueobject

Value objects

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0

 

The Development Requires

enum ddd value-object valueobject

21/09 2017

1.0

1.0.0.0 https://github.com/yomy/valueobject

Value objects

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

enum ddd value-object valueobject

20/09 2017

dev-cleaning

dev-cleaning https://github.com/yomy/valueobject

Value objects

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

enum ddd value-object valueobject