2

I am trying to create a dynamic endpoint for a API I am creating in order to include some data but only if it is required so I can use it in multiple places.

The idea is to have api.domain.com/vehicle to bring back basic vehicle information but if I did api.domain.com/vehicle?with=owners,history then the idea is to have a function which maps the owners and history to a class which will return data but only if it is required.

This is what I currently have.

public static function vehicle()
{
    $with = isset($_GET['with']) ? $_GET['with'] : null;
    $properties = explode(',', $with);
    $result = ['vehicle' => Vehicle::data($id)];

    foreach ($properties as $property) {
        array_push($result, static::getPropertyResponse($property));
    }

    echo json_encode($result);
}

Which will then call this function.

protected static function getPropertyResponse($property)
{
    $propertyMap = [
        'owners' => Vehicle::owner($id),
        'history' => Vehicle::history($id)
    ];

    if (array_key_exists($property, $propertyMap)) {
        return $propertyMap[$property];
    }

    return null;
}

However, the response I'm getting is being nested within a index, which I don't want it to be. The format I want is...

{
    "vehicle": {
        "make": "vehicle make"
    },
    "owners": {
        "name": "owner name"
    },
    "history": {
        "year": "26/01/2018"
    }
}

But the format I am getting is this...

{
    "vehicle": {
        "make": "vehicle make"
    },
    "0": {
        "owners": {
            "name": "owner name"
        }
    },
    "1": {
        "history": {
            "year": "26/01/2018"
        }
    }
}

How would I do this so it doesn't return with the index?

2
  • Instead of array_push, try array_merge. Commented Mar 2, 2018 at 21:57
  • 1
    don't use array push. You want each array entry to be keyed to the property. Use something like : $result[$property] = static::getPropertyResponse($property); Commented Mar 2, 2018 at 21:58

1 Answer 1

2

Vehicle::history($id) seems to return ['history'=>['year' => '26/01/2018']], ...etc.

foreach ($properties as $property) {
    $out = static::getPropertyResponse($property) ;
    $result[$property] = $out[$property] ;
}

Or your methods should returns something like ['year' => '26/01/2018'] and use :

foreach ($properties as $property) {
    $result[$property] = static::getPropertyResponse($property) ;
}
Sign up to request clarification or add additional context in comments.

6 Comments

@IncredibleHat return $propertyMap[$property]; is in getPropertyResponse(), not in Vehicle::history($id).
@IncredibleHat :) Thank you for your comment, that's interesting. I'm currently looking.
@IncredibleHat static::getPropertyResponse($property) returns "owners": {"name": "owner name"}. Agree? So in foreach, $result[$property] = static::getPropertyResponse($property)[$property]. No?
Like... I'm not sure where he gets $id from inside the method of getPropertyResponse. Its not passed in, and its not a class reference. Also as you mentioned it, what is the actual return of Vehicle::history() anyway? I got lost.
I bet you nailed it though. Your answer should definitely be a move to a solution to his problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.