A PHP (code Igniter) Library for easily refactoring associative arrays (remove key-vals, rename keys, & inflate a value with respective objects from a mysql table based off provided primary key values). You'll find this library useful if you want to return payloads which should have different keys than what they exist as in the database.
When building RESTful APIs, there are certain structures you want your JSON payloads to take up which do not necessarily reflect the way they were retrieved from a database, etc. or simply perform extra data retrievals before they are sent as response.
This library basically refactor your associative arrays using specified rules. I’ll demonstrate this with examples below in the Usage section.
This library plays a similar role to the Laravel Resources API. Only this is built to suit Code Igniter specifically.
To install, download and install Splint from https://splint.cynobit.com/downloads/splint and then run the below from your Code Igniter project root
splint install francis94c/refactor-ci
From anywhere you can access the CI
instance
$this->load->package("francis94c/refactor-ci");
There are two ways to use this library.
Create a refactor.php
file in your (Code Igniter) application config folder.
Though this is not necessary but it makes sense to save your refactor rules in a separate file other than the Code Igniter (application) config file.
This means you can create config rules also in the application config file, But not the best of practices.
The library when loaded, will always attempt to load the refactor.php
config file. No error will be thrown if this file doesn’t exist.
Below is a sample refactor.php
file with rules defined.
defined('BASEPATH') OR exit('No direct script access allowed');
$config['refactor_email'] = [
'unset' => [
'user_id',
'hit_count',
'last_read_time',
'last_message',
],
'replace' => [
'open_count' => 'times_opened',
'stuck_count' => 'stuck_tx'
],
'bools' => [
'active'
],
'cast' => [
'id' => 'int'
],
'inflate' => [
'users_ids' => [
'table' => 'users',
'refactor' => 'user'
]
]
];
$config['refactor_user'] = [
'unset' => [
'date_registered',
'lua_script'
],
'cast' => [
'service_status' => 'int'
],
];
This methods requires you to create as much PHP classes as the JSON payloads you want to modify.
These PHP classes should be created in the application/libraries/refactor
.
Notices that you have to create a refactor
class in you libraries
folder.
Lets say I have a payload to return as a response from an API end point which is a user data array as below.
$data = [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'address' => 'Utopia'
];
And I want every User
payload like the above to have a status
key a bd a value of true
.
I’ll creates a say User.php
file in the application/libraries/refactor
with a
User
class defined within it and extending/inherit from the RefactorPayload
class. Then override the toArray
method, returning an array as shown below.
defined('BASEPATH') OR exit('No direct script access allowed');
class Email extends RefactorPayload
{
public function toArray():array
{
$this->status = true;
return parent::toArray();
}
}
From the above, it is clear that the Email
object which extebds the RefactorPayload
class is kind of the payload itself.
Proof of the is in the toArray
function where it’s status
field was set to true
and then a call to the parent
toArray
function being made to return itself as an array.
To actual effect the toArray
method on an actual payload, we load the refactor
library and use as below.
$this->load->package('francis94c/refactor-ci');
$data = [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'address' => 'Utopia'
];
$data = $this->refactor->payload(Email::class, $data);
The above payload
function will create an instance of the class name provided as it’s first argument and run it’s toArray
method on the second argument provided.
If the payload is an array (multiple user data), process it as below.
$data = $this->refactor->array(Email::class, $data);
With this, you can directly modify payloads as you wish, programmatically.