Skip to content

Commit 30340dc

Browse files
committed
#4 object to array and back
1 parent f14a7ae commit 30340dc

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

src/Schema.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class Schema extends MagicMap
9696
public $oneOf;
9797

9898
public $objectItemClass;
99+
private $useObjectAsArray = false;
99100

100101
public function import($data, DataPreProcessor $preProcessor = null)
101102
{
@@ -112,6 +113,10 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
112113
if (!$import && $data instanceof ObjectItem) {
113114
$data = $data->jsonSerialize();
114115
}
116+
if (!$import && is_array($data) && $this->useObjectAsArray) {
117+
$data = (object)$data;
118+
}
119+
115120
if (null !== $preProcessor) {
116121
$data = $preProcessor->process($data, $this, $import);
117122
}
@@ -254,8 +259,6 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
254259
}
255260
}
256261
}
257-
258-
259262
}
260263

261264
if ($data instanceof \stdClass) {
@@ -267,16 +270,21 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
267270
}
268271
}
269272

270-
if ($import && !$result instanceof ObjectItem) {
271-
$result = $this->makeObjectItem();
272273

273-
if ($result instanceof ClassStructure) {
274-
if ($result->__validateOnSet) {
275-
$result->__validateOnSet = false;
276-
/** @noinspection PhpUnusedLocalVariableInspection */
277-
$validateOnSetHandler = new ScopeExit(function () use ($result) {
278-
$result->__validateOnSet = true;
279-
});
274+
if ($import) {
275+
if ($this->useObjectAsArray) {
276+
$result = array();
277+
} elseif (!$result instanceof ObjectItem) {
278+
$result = $this->makeObjectItem();
279+
280+
if ($result instanceof ClassStructure) {
281+
if ($result->__validateOnSet) {
282+
$result->__validateOnSet = false;
283+
/** @noinspection PhpUnusedLocalVariableInspection */
284+
$validateOnSetHandler = new ScopeExit(function () use ($result) {
285+
$result->__validateOnSet = true;
286+
});
287+
}
280288
}
281289
}
282290
}
@@ -344,7 +352,7 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
344352
}
345353

346354
$value = $this->additionalProperties->process($value, $import, $preProcessor, $path . '->additionalProperties');
347-
if ($import) {
355+
if ($import && !$this->useObjectAsArray) {
348356
$result->addAdditionalPropertyName($key);
349357
}
350358
}
@@ -357,7 +365,11 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
357365
$result->$key = $value;
358366
}
359367
} else {
360-
$result->$key = $value;
368+
if ($this->useObjectAsArray && $import) {
369+
$result[$key] = $value;
370+
} else {
371+
$result->$key = $value;
372+
}
361373
}
362374

363375
}
@@ -418,6 +430,16 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
418430
return $result;
419431
}
420432

433+
/**
434+
* @param boolean $useObjectAsArray
435+
* @return Schema
436+
*/
437+
public function setUseObjectAsArray($useObjectAsArray)
438+
{
439+
$this->useObjectAsArray = $useObjectAsArray;
440+
return $this;
441+
}
442+
421443
/**
422444
* @param bool|Schema $additionalProperties
423445
* @return Schema
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\PHPUnit\Schema;
4+
5+
6+
use Swaggest\JsonSchema\Schema;
7+
8+
class ObjectStructure extends \PHPUnit_Framework_TestCase
9+
{
10+
11+
public function testObjectAsArray()
12+
{
13+
$schema = new Schema();
14+
$schema->setUseObjectAsArray(true);
15+
16+
$data = json_decode('{"one":1,"two":2,"three":3}');
17+
18+
$imported = $schema->import($data);
19+
$this->assertSame(array("one" => 1, "two" => 2, "three" => 3), $imported);
20+
$exported = $schema->export($imported);
21+
$this->assertTrue($exported instanceof \stdClass, '\stdClass expected on export');
22+
$this->assertSame(array("one" => 1, "two" => 2, "three" => 3), (array)$exported);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)