Download the PHP package domagoj03/object-creator without Composer
On this page you can find all versions of the php package domagoj03/object-creator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download domagoj03/object-creator
More information about domagoj03/object-creator
Files in domagoj03/object-creator
Package object-creator
Short Description PHP library that helps developers create objects from other data types such as arrays, serialized strings and alike.
License MIT
Informations about the package object-creator
Object Creator
Intro
This simple library was first and foremost created because I noticed that I use the funcionallity it provides across several projects and instead of re-writing the logic again and again, I've decided to mold this into a library, mostly for my conveniance, but also publish it as an open-source project in order to help anyone else who might find it useful.
Installation
Usage
Intended usage is to extend the abstracts of the library and instantiate objects by passing
data to your object's constructor. For example, when you receive data as a result of an API call,
you might get it as an array or as \stdClass. Then instead of accessing that data as
$data['value']
, you could acess it as $object->getValue()
thus mitigating possible errors
and additional checks like isset($data['value'])
before using it.
You can find some inspiration inside tests/Helpers
This method proved very handy in situations where I had to handle different data structure received, for example, from an API calls or Webhooks. It's also useful when you have to handle multiple external API integrations.
v1.0
- Define object which represents data in your application
- Make it extend desired library abstract
-
Generate getters and setters for your properties (in this case for
number
). Remember that index of an associative array is property of your class and value represents the property/key of the data you wish to create object from. - When you need to use that received
$data
, instantiate your object and pass the$data
to a constructor.
Data as array
Data as stdClass
- This is a fairly simple example but shows what the library does in a nutshell.
v0.1 [not recommended]
- Has only
ArrayObjectCreator
ArrayObjectCreator
will not setnull
values
Tests
vendor/bin/phpunit
Contributing
Contributing is always welcome.
If you have suggestions or problems with usage, you can open up an issue.
If you want to help with the issues, fork the repository, make sure you have latest version
of the code and that the current tests pass, create new branch, work on the feature or bug (and add test cases), push the code to your repository and create a PR
to this repository master
branch mentioning the issue it's resolving.