Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/esler/smart-dto
Simple trait for creating "smart" data-transfer-objects in PHP
https://github.com/esler/smart-dto
dto php
Last synced: 2 days ago
JSON representation
Simple trait for creating "smart" data-transfer-objects in PHP
- Host: GitHub
- URL: https://github.com/esler/smart-dto
- Owner: esler
- License: mit
- Created: 2019-04-30T10:25:34.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-09-11T09:46:35.000Z (over 5 years ago)
- Last Synced: 2024-11-09T05:23:06.733Z (about 2 months ago)
- Topics: dto, php
- Language: PHP
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# smart-dto
Simple trait for creating \"smart\" data-transfer-objects in PHP## Installation
```
composer require intraworlds/smart-dto
```## Usage
```php
_config = json_decode($config, true); // loading from DB
} else {
$this->_config = $config;
}
}
}$user = new UserDTO;
$user->id = 123;
$user->username = 'joe.doe';
$user->config = '{"foo":"bar"}';// undefined properties will fail
$user->surname = 'doe';
echo $user->surname; // throws an exception// you can extract an array from DTO
print_r($user);
// > Array(id => 123, username => joe.doe, config => Array(foo => bar))// you can hydrate object from an array
$user->hydrate(['id' => 456, 'config => ['foo' => 'baz']]);// the MAIN usage will be probably when loading from DB
$stmt = $pdo->query('SELECT * FROM user', \PDO::FETCH_CLASS, UserDTO::class);
$user = $stmt->fetch();```
## License
MIT