https://github.com/eppak/value
Value Object Helper
https://github.com/eppak/value
object php value-object
Last synced: 11 months ago
JSON representation
Value Object Helper
- Host: GitHub
- URL: https://github.com/eppak/value
- Owner: eppak
- License: mit
- Created: 2017-12-29T15:05:17.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-01-16T13:05:25.000Z (about 6 years ago)
- Last Synced: 2025-01-24T18:08:05.846Z (about 1 year ago)
- Topics: object, php, value-object
- Language: PHP
- Homepage:
- Size: 13.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Intro
====
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
====
```sh
# 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
'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
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
has('test');
$testChin_present = $json->hasChain('test', 'test1>test2>test3');
```
Cheat sheet
====
| Method | Type | Description |
| ------------- |-------------| -----|
| fromArray | static | *Create an object from array* |
| fromJson | static | *Create an object from a json string* |
| has | virtual | *Check if a property is present* |
| hasChain | virtual | *Check if a chain of properties is present* |
| getChainValue | virtual | *Get the value of a chain of properties* |
| duplicate | virtual | *Create a new object identical to the current* |
| getX | virtual | *Get the X property* |
| setX | virtual | *Set the X property* |
License
====
MIT