zingle-com / array-model
Model primitive arrays.
Installs: 15 988
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ~7.2
- ext-json: *
- illuminate/support: ^5.4
- phootwork/collection: >=1.5
- zingle-com/php-enum: ^1.1
Requires (Dev)
This package is auto-updated.
Last update: 2024-12-10 17:13:22 UTC
README
What and why?
Model associative array's as objects. This is useful when you would like some description of what is in associative array for future reference. Often times, in code you're also need to model certain kinds of behavior around data from various sources (api responses, arbitrary database queries, etc.).
Usage
Extend the ZingleCom\ArrayModel\AbstractModel
class and add annotations to describe the
kinds of data it models. Example:
use ZingleCom\ArrayModel\AbstractModel; /** * @method int getId() * @method string getUsername() * @method bool isActive() * @method array getFriends() */ class SomeApiReponse extends AbstractModel {} // later in code // @var array $response $response = $httpClient->get('/user/123'); // $response suppose arr now contains an associative array like: // [ // 'id' => 123, // 'username' => 'bob', // 'active' => true, // 'friends' => [234, 345], // ] $model = new SomeApiResponse($response); echo $model->getId(); // prints 123 echo $model->getUsername(); // prints bob echo $model->isActive() ? 'YES' : 'NO'; // prints YES echo json_encode($model->getFriends()) // prints [234, 345]
Contributing
Open a PR against master. If you see any serious concerns email me at zach dot quintana at gmail dot com
.
Happy coding!