A simple utility to create payloads in an easy object oriented construct and transform it into different structures viz. array, objects, string(json).
Create a new PayloadStruct instance
$payload = new PayloadStruct;
Set Items:
$payload->setName('Jane Doe');
$payload->setEmail('[email protected]');
$payload->setPhone('1234567890');
$payload->setWebsite('example.com');
You can also chain the setters:
$payload->setName('Jane Doe')
->setEmail('[email protected]')
->setPhone('1234567890')
->setWebsite('example.com');
Or use assignment notation:
$payload->language = 'English';
And get the final payload in different structures:
$payload->getArray();
Output:
Array
(
[name] => Jane Doe
[email] => [email protected]
[phone] => 1234567890
[website] => example.com
[language] => English
)
$payload->getJson();
Output:
{"name":"Jane Doe","email":"[email protected]","phone":"1234567890","website":"example.com","language":"English"}
$payload->getObject();
Output:
stdClass Object
(
[name] => Jane Doe
[email] => [email protected]
[phone] => 1234567890
[website] => example.com
[language] => English
)
You can also access individual items with getter notation or direct property access notation as follows:
$payload->getName(); //Outputs Jane Doe
$payload->name; //Outputs Jane Doe
Reset payload data:
$payload->flush();
You can chain on to add new data items:
$payload->flush()
->setNewName('John Doe');