eppak / value
Value Object Helper
1.0.0.0
2018-01-02 11:06 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: ^6.4
This package is auto-updated.
Last update: 2024-10-16 23:07:02 UTC
README
This is a smart dto object, is useful to transport and manipulate data. Usually a value object have getter and setter for a bunch of properties that represent data.
Installation
# composer install eppak/value
Constructor
Can be created in various modes, it accept usually an array of fields with values; the second optional parameter represent if property can be written or is readonly.
- As new.
- From an array.
- From JSON
If the object need to be readonly you can istantiate a roValue object istead ov Value.
Example of Initialization
<?php use eppak\value; //... $data = [ 'testRead' => 'read', 'testWrite' => 'write', 'testReadWrite' => 'readwrite', 'undisciplined' => 'readwrite' ]; $value = new Value( $data ); $value = new Value( $data, [ 'testRead' => static::R, 'testWrite' => static::W, 'testReadWrite' => static::RW ] ); $value = Value::fromArray( $data ); $value = Value::fromJson( '{"test": "read"}' );
Example of reading/writing
<?php $testRead = $value->getTestRead(); $testReadWrite = $value->getTestReadWrite(); $value->setTestReadWrite('some value'); $value->setTestRead('some value'); // Thrown an error, is read only
Testing property existence
Properties existence can be probed as simple level or multi level, useful when value object is istanced from JSON.
<?php $json = Json::fromJson('{ "test": { "test1" : { "test2": { "test3" : 1} } }}'); $testRead_present = $json->has('test'); $testChin_present = $json->hasChain('test', 'test1>test2>test3');
Cheat sheet
License
MIT