This library provides class that lets easily get structured items from JSON-decoded data.
So that it would be easier and quicker to:
- check correct data types. This allows to use PHP7 static type-hints easily;
- check required keys/items;
- give defaults to missing items;
- provide information about exact place in object tree where the data fails your requirements.
$jsonString = json_encode([
// ...
]);
/** @var stdClass $jsonData */
$jsonData = json_decode($jsonString);
$data = new ObjectWrapper($jsonData);
// Use like an associative array if needed
foreach ($data as $key => $value) {
var_dump(isset($data[$value]));
var_dump($data[$value]);
}
var_dump($data['non_existing']); // just null - no notice
var_dump($data->getRequired('required_key')); // throws exception if missing
var_dump($data->getString('key_with_string')); // throws exception if not string, defaults to null
var_dump($data->getString('key_with_string', 'default')); // different default
var_dump($data->getRequiredString('key_with_string')); // throws exception if missing or not string
var_dump(get_class($data->getRequiredObject('inner_data'))); // always another ObjectWrapper instance
var_dump(get_class($data->getObject('inner_data'))); // another ObjectWrapper instance or null
var_dump($data->getArrayOfString('keys')); // array of strings
var_dump($data->getArrayOfOject('children')); // array of ObjectWrapper instances
try {
$data->getRequiredObject('inner_data')->getArrayOfObject('list')[0]->getRequiredString('key');
} catch (MissingItemException $e) {
echo $e->getMessage(); // Missing required key "inner_data.list.0.key"
echo $e->getKey(); // inner_data.list.0.key
}$data[$key]- use array accessor to get any value inside, returnsnullif it's unavailable. If value is object, instance ofObjectWrapperwill be returned;getRequired. Returns mixed value, but checks that it is provided;getRequiredBool. Returnsbool;getBool. Returnsboolornull;getRequiredFloat. Returnsfloat. Acceptsint, but casts it tofloat;getFloat. Returnsfloatornull. Acceptsint, but casts it tofloat;getRequiredInt. Returnsint;getInt. Returnsintornull;getRequiredObject. Returns anotherObjectWrapperinstance;getObject. Returns anotherObjectWrapperinstance ornull;getRequiredString. Returnsstring;getString. Returnsstringornull;getArray. Returnsarray. If no item is provided, returns empty array, so nogetRequiredArrayis available;getArrayOfBool. Returnsarray, all it's items arebool(or it's empty);getArrayOfFloat. Returnsarray, all it's items arefloat(or it's empty);getArrayOfInt. Returnsarray, all it's items areint(or it's empty);getArrayOfString. Returnsarray, all it's items arestring(or it's empty);getArrayOfObject. Returnsarray, all it's items are instances ofObjectWrapper(or it's empty).getDataAsArray. Returnsarray, all it's items arearrayor primitive types.getOriginalData. ReturnsstdClass, the original data passed to constructor.
stdClassis used for objects - this is default forjson_decode. This is to easily check where the data was an object and where it was an array. For example, if empty object or empty array is passed as JSON and you denormalize to an array, there is no way to check what was the type of original data;nullvalues are treated the same as it would not be provided at all:- it will not throw
InvalidItemTypeExceptionif you providenulland some type was expected (even array or object); - it will throw
MissingItemExceptioneven if you provide the value but it isnull;
- it will not throw
- the object cannot be modified - setting or unsetting anything is not supported.