Would it make sense for me to use a builder pattern to return data from an API? Currently I'm just creating the array structure and returning it. In more detail, I have 3 exit places for my class that should be accounted for (one of them being empty results).
Basically it is just a multidimensional array in PHP like below. I'm not sure if I should create a global array, a struct like class, or builder. I would love some input on this for my future work.
$arr['pagination']['total']
$arr['pagination']['page']
$arr['inventory_data']
$arr['errors']
UPDATE: I'm trying it out like so :
public function getMeSomething($input)
{
// -- return 'empty' objects if problems etc. --
if (!$input) {
return (array) (new myBuilderStruct())
->error('the input is missing')
->build();
}
// <method code here>
// -- Return results --
return (array) (new myBuilderStruct())
->total($totalRecords)
->current_page($page)
->limit($limit)
->error((!empty($error)) ? $error : "")
->build();
}
Just to give some more detail I'm using it just to return a data structure from a method, but I want all of my methods that return this data to be the same .