dev-master
9999999-dev https://github.com/TriunValue Object
MIT
The Requires
- php >=5.1.0
The Development Requires
object value
v1.0.0
1.0.0.0 https://github.com/TriunValue Object
MIT
The Requires
- php >=5.1.0
The Development Requires
object value
Wallogit.com
2017 © Pedro Peláez
Value Object
![Software License][ico-license], (*1)
Value Object definition., (*2)
PHP Class interface to define and standardise a Value Object format., (*3)
In computer science, a value object is a small object that represents a simple entity whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object (Wikipedia)., (*4)
Require this package with composer using the following command:, (*5)
composer require triun/value-object
The package contains a ValueObject interface:, (*6)
interface ValueObject
{
/**
* Returns a object taking PHP native value(s) as argument(s).
*
* @return \Triun\ValueObject\ValueObject
*/
public static function fromNative();
/**
* Returns PHP native value(s)
*
* @return mixed
*/
public function toNative();
/**
* Compare two ValueObjectInterface and tells whether they can be considered equal.
*
* @param \Triun\ValueObject\ValueObject $object
*
* @return mixed
*/
public function equals(ValueObject $object);
/**
* Returns a string representation of the object.
*
* @return string
*/
public function __toString();
}
And a Invalid Argument Exception:, (*7)
class InvalidNativeArgumentException extends \InvalidArgumentException
{
/**
* InvalidNativeArgumentException constructor.
*
* @param string $value
* @param array $allowed_types
*/
public function __construct($value, array $allowed_types)
{
$this->message = sprintf(
'Argument "%s" is invalid. Allowed types for argument are "%s".',
$value,
implode(', ', $allowed_types)
);
}
}
Example of use for a single field ValueObject:, (*8)
use Triun\ValueObject\ValueObject;
use Triun\ValueObject\Exceptions\InvalidNativeArgumentException;
class StringLiteral implements ValueObject
{
/**
* Native string
*
* @var string
*/
protected $value;
/**
* Returns a StringLiteral object given a PHP native string as parameter.
*
* @internal param string $value
*
* @return StringLiteral
*/
public static function fromNative()
{
$value = func_get_arg(0);
return new static($value);
}
/**
* Returns a StringLiteral object given a PHP native string as parameter.
*
* @param string $value
*/
public function __construct($value)
{
if (false === \is_string($value)) {
throw new InvalidNativeArgumentException($value, array('string'));
}
$this->value = $value;
}
/**
* Returns the value of the string
*
* @return string
*/
public function toNative()
{
return $this->value;
}
/**
* Tells whether two string literals are equal by comparing their values
*
* @param ValueObject $stringLiteral
*
* @return bool
*/
public function equals(ValueObject $stringLiteral)
{
if (static::class !== get_class($stringLiteral)) {
return false;
}
return $this->toNative() === $stringLiteral->toNative();
}
/**
* Tells whether the StringLiteral is empty
*
* @return bool
*/
public function isEmpty()
{
return \strlen($this->toNative()) == 0;
}
/**
* Returns the string value itself
*
* @return string
*/
public function __toString()
{
return $this->toNative();
}
}
Bug reports and feature requests can be submitted on the Github Issue Tracker., (*9)
See CONTRIBUTING.md for information., (*10)
The Laravel Model Base is open-sourced software licensed under the MIT license, (*11)
Value Object
MIT
object value
Value Object
MIT
object value