-1

My first question at stackoverflow:

I have a class that loads JSON data.

Here is the class:

class example {
public $url = array(
    "location" => "http://www.example.com/location/?{city}.json",
);
public $location = array();
public function load($url_template = NULL, $params = array(), $overwrite = false) {
    $this->$url_template = $this->load_json(
        $this->build_url($url_template, $params)
    );
}
    public function load_json($url) {
        return json_decode($url);
    }
}

Outside I use:

    $example = new example();
    $example->load("location", array(
    'city' => 'Boston',
    ));

When I do

echo $example->country->name;
echo $example->blabla->blub;

I want an information like

$example->country->name was called and the returned value is "Boston"

$example->blabla->blub was called but property "blabla" is not existing and property "blub" is also not existing so the returned value is "n/a"

within the class.

Thank you, harry_bo

harry_bo
  • 1
  • 3
  • A little unclear, if you're asking how to implement request validation, it's a bit broad, you'll need to narrow down the topic or do a little more research to figure out exactly what you need help with. – Devon Bessemer Dec 18 '18 at 15:34
  • https://github.com/Respect/Validation is a nice lib – Felippe Duarte Dec 18 '18 at 15:37
  • @Devon when I use get_object_vars I see all properties of the current object. What I want to see is which property of the object was requested from outside the function by usign echo $class->property – harry_bo Dec 18 '18 at 15:47

1 Answers1

0

First thing you can do is to validate the JSON (if you have a schema) input.

More about a JSON schema: https://json-schema.org/

Some more info here: JSON schema validation with PHP

Of course you can do all in PHP and validate every possible entry.

if (isset($class->country)) {
  if (isset($class->country->name) ) {
      // validate this is really a country
  } else {
   // no country name set ...
  }
  // .. more validation ..
} else {
 // no country given
}
michael - mlc
  • 376
  • 5
  • 5