src/Nugget.php

 
Procedures Status
Nugget::__construct Testable
Nugget::meetsSchema Issues 1
Nugget::limitToSchema Testable
 
 
1. <?php
3. namespace Activerules\Nugget;
5. use Activerules\Nugget\Exceptions\NuggetException;
7. /**
8. * The Nugget
9. */
10. class Nugget
11. {
13. /**
14. * Create a new Skeleton Instance
15. */
16. public function __construct()
17. {
18. }
20. /**
21. * Determine if an object is valid
22. *
23. * @param string $object, JSON serialized object
24. * @param type $schema
25. * @return boolean
26. */
27. public function meetsSchema(string $object, $schema)
28. {
29. $data = json_decode($object);
New instance (Activerules\JsonGuard\Validator)  
31. $validator = new \Activerules\JsonGuard\Validator($data, $schema);
33. return $validator->fails() ? false : true ;
34. }
36. /**
37. * Remove all properties not defined in the schema
38. *
39. * @param string $object
40. * @param type $schema
41. * @return boolean
42. */
43. public function limitToSchema(string $object, $schema, $jsonOut=true)
44. {
45. $data = json_decode($object);
47. // We loop through the top level schema objects and only use the data elements defined therein.
48. // We don't loop though data becasue it could be much larger than the schema.
49. // Start with an empty array.
50. $cleanObject = [];
52. foreach($schema->properties as $prop => $val) {
53. if(isset($data->$prop)) {
54. $cleanObject[$prop] = $data->$prop;
55. }
56. }
58. return $jsonOut ? json_encode($cleanObject) : $cleanObject;
59. }
61. }
 
Generated Thu, 11 Jan 2018 21:56:06 -0800 by PHP_Testability