Download the PHP package nuad/graph-objects without Composer
On this page you can find all versions of the php package nuad/graph-objects. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nuad/graph-objects
More information about nuad/graph-objects
Files in nuad/graph-objects
Package graph-objects
Short Description An array to object map php library. Useful when mapping from incoming json or database results
License MIT
Informations about the package graph-objects
PHP assoc array to class properties
Graph objects is a php5 hydrator which maps associative arrays to class instances using metadata defined in their classes, useful when working with database result sets or formats that can be easily formatted to assoc arrays (such as json).
The library has no extra requirements (except phpunit for testing). It does not require custom annotation syntax or makes use of doc comments in order to limit reflection usage and to keep things as simple and light as possible.
Installation
Installing with composer:
Usage
All the project files are under the Nuad/Graph namespace. The main interface that all graph objects must implement is the Graphable interface and all the functionality is in the GraphAdapter trait.
The two main methods that the library uses are the create() and graph() methods. The first one will just output a clean instance of an object and the second one will provide the metadata to map the assoc array to the object. Though the GraphAdapter provides a version of the create() method (that uses reflection to create the instance) the method may be to have an object created with whatever default values.
So assuming there is a dto,entity etc class called Person which may work as a base class for a User object. A simple definition of the Person class would be:
Now to put the library in use the above class becomes:
Note: The create method can be ignored as we can use the traits create() method but this way we can create an instance without the use of Reflection.
Now to use the class to map a $data assoc array lets say from a JSON format:
Now the $person var should be a new Person instance with the data from the json file.
Syntax
The graph method must return an Entity object. The Entity's constructor requires an array of names which will identify the class. The Entity contains a list of properties (defined with the properties() method) which is an assoc array with keys the names of the properties of the main class and values the type of each property.
Types
The types of the properties are divided in three main categories
- flat types (Integer,Double,Boolean,FlatArray)
- objects (refers to another graph object. The constructor requires a clean instance of that object)
- collection (refers to an array of graph objects. The constructor also requires a clean instance of that object)
Inheritance and more complex types
Using the example above we will define a User class which will extend the Person and contain a Location object which will contain a Point object.
The extra Location and Point classes:
Note: The Location contains an object named point which is of type Point and is defined as such in the graph() method. The Point graph defines expected arrays to both of its properties. In the expected attribute you may put variations of the property name which may be encountered in the data.
And now to define the User child class:
Note: The GraphAdapter trait must be defined again and in the graph() method we extend the graph() method of the parent class as seen above.
Features
-
Inject:
Using the inject method instead of the map method will force the GraphAdapter trait to use the constructor of the object instead. Also the injectWithInstance Method accepts a graph object instance as parameter which will use to map the data.
-
Map empty:
The mapEmpty method of the trait will map only values of the object which are null. Useful when creating object properties in separate steps.
-
Expected & bind:
The Type's expected() method accepts an array of indexes on where the values of the property may be found in the incoming data. The property names will be preferred over the expected values. To override this behaviour use the bind method (also accepts an array of indexes which will be preferred over expected values and property names). Syntax:
-
Callbacks:
The Entity takes a finalize callback (through the finalize() method) which gives access to the mapped object and the raw data as soon as the mapping is complete, can be used to validate object properties before continuing.
The Type takes a handler callback (through the handler() method) which will provide access to the data found in the payload for that property and the index where the data were found. If defined the returned value of the handler will be used instead, if the handler callback returns null the adapter will continue to map the property normally.
Note: Both callbacks give access to a variable named 'scenario'. This variable can be passed through the inject/map methods and can be useful when a property is used different scenarios, for example different locale data.