Example #1
0
 public function testNested()
 {
     $data = '{
         "prop1": {
             "nested1": {
                 "nested2": {
                     "inner1": ["hidden"],
                     "inner2": {}
                 },
                 "nested3": []
             }
         },
         "prop2": "string",
         "prop3": [],
         "prop4": null
     }';
     $expected = '{
         "prop1": {
             "nested1": {
                 "nested2": {
                     "inner1": ["hidden"]
                 }
             }
         },
         "prop2": "string",
         "prop4": null
     }';
     $data = $this->fromJson($data);
     $expected = $this->fromJson($expected);
     $result = Utils::dataPrune($data);
     $this->assertEquals($expected, $result);
 }
Example #2
0
 public function testUniqueWithObject()
 {
     $obj1 = (object) array('name' => 'name', 'other' => 'other');
     $obj2 = (object) array('other' => 'other', 'name' => 'name');
     $value = array(1, 'str', $obj1, false, $obj2, 2, 1);
     $expected = array(1, 'str', $obj1, false, 2);
     $this->assertEquals($expected, Utils::uniqueArray($value));
 }
Example #3
0
 private function resolve($schema, $parents = array())
 {
     $result = $schema;
     if ($ref = Utils::get($schema, '$ref')) {
         $refSchema = Utils::get($this->references, $ref);
         if (in_array($ref, $parents)) {
             throw new \RuntimeException('Circular reference to ref ' . $ref);
         } elseif (Utils::get($refSchema, '$ref')) {
             $parents[] = $ref;
             $result = $this->resolve($refSchema, $parents);
         } else {
             $result = $refSchema;
         }
     }
     return $result;
 }
Example #4
0
 public function testPretty()
 {
     $data = '{
         "prop1": "",
         "prop2": {
             "inner1": [
                 {"lat": 50, "lng": 120},
                 {"lat": 27, "lng": 3}
             ],
             "inner2": 2
         },
         "prop3": [],
         "prop4": null,
         "prop5": 5,
         "prop6": {}
     }';
     $expected = $this->getFileExpected(__FUNCTION__);
     $data = $this->fromJson($data);
     $result = Utils::dataToJson($data, true);
     $this->assertEquals($expected, $result);
 }
Example #5
0
 public static function validateArchiveData($archiveData)
 {
     if (!self::$schemaUri) {
         self::$schemaUri = __DIR__ . '/archive-schema.json';
     }
     $document = new Document();
     // have to do a encode+decode so that json objects decoded as arrays from Guzzle
     // are re-encoded as objects instead
     $document->loadData(json_decode(json_encode($archiveData)));
     $document->loadSchema(self::$schemaUri);
     // JSON Pointers are supported for the validation using this library, this is a hack
     $document->loadSchema(JsonUtils::get(JsonUtils::get($document->schema->data, 'definitions'), 'archive'));
     if (!$document->validate()) {
         throw new InvalidArgumentException('The archive data provided is not valid. Errors:' . $document->lastError . ' archiveData:' . print_r($archiveData, true));
     }
 }
Example #6
0
 protected function validateUnique($data)
 {
     $count = count($data);
     for ($i = 0; $i < $count; ++$i) {
         for ($j = $i + 1; $j < $count; ++$j) {
             if (Utils::equals($data[$i], $data[$j])) {
                 return false;
             }
         }
     }
     return true;
 }
Example #7
0
 public function testScalarFail()
 {
     $container = 6;
     $this->assertEquals(null, Utils::get($container, 'name'));
 }
Example #8
0
 public function testNested()
 {
     $schema = '{
         "properties": {
             "prop1": {},
             "prop2": {
                 "inner1": {
                     "items": {
                         "$ref": "#/definitions/location"
                     }
                 },
                 "inner2": {}
             },
             "prop3": {},
             "prop4": {}
         },
         "definitions": {
             "location": {
                 "properties": {
                     "lat": {"type": "number"},
                     "lng": {"type": "number"}
                 }
             }
         }
     }';
     $data = '{
         "prop4": null,
         "prop5": 5,
         "prop2": {
             "inner2": 2,
             "inner1": [
                 {"lng": 120, "lat": 50},
                 {"lat": 27, "lng": 3}
             ]
         },
         "prop1": "",
         "prop3": []
     }';
     $expected = '{
         "prop1": "",
         "prop2": {
             "inner1": [
                 {"lat": 50, "lng": 120},
                 {"lat": 27, "lng": 3}
             ],
             "inner2": 2
         },
         "prop3": [],
         "prop4": null,
         "prop5": 5
     }';
     $schema = $this->getSchemaObject($schema);
     $data = $this->fromJson($data);
     $expected = $this->fromJson($expected);
     $result = Utils::dataOrder($data, $schema);
     $this->assertEquals($expected, $result);
 }
Example #9
0
 public function testEncodePathWithZero()
 {
     $value = '0';
     $expected = '/0';
     $this->assertEquals($expected, Utils::pathEncode($value));
 }
Example #10
0
 public function testObjectWithEmptyElements()
 {
     $data = new \stdClass();
     $data->prop1 = new \stdClass();
     $data->prop2 = 'none';
     $data->prop3 = null;
     $data->prop4 = array();
     $data->prop5 = array(7);
     $data->prop6 = array('Bloggs', 'firstName' => 'Fred', 9);
     $result = Utils::dataCopy($data);
     $expected = '{
         "prop1": {},
         "prop2": "none",
         "prop3": null,
         "prop4": [],
         "prop5": [7],
         "prop6": {
             "0": "Bloggs",
             "firstName": "Fred",
             "1": 9
         }
     }';
     $expected = $this->fromJson($expected);
     $this->assertEquals($expected, $result);
 }
Example #11
0
 public function testObjectFalse2()
 {
     $var1 = (object) array('name' => 'value', 'other' => 'other');
     $var2 = 'other';
     $this->assertFalse(Utils::equals($var1, $var2));
 }
Example #12
0
 public function testFail()
 {
     $value = array(1, 2, 3);
     $this->assertFalse(Utils::checkType('object', $value));
 }