Skip to content

Commit f14a7ae

Browse files
authored
Merge pull request #5 from swaggest/properties-accessor
Properties accessor
2 parents 2d8b8d3 + f2990bc commit f14a7ae

File tree

6 files changed

+52
-54
lines changed

6 files changed

+52
-54
lines changed

src/Constraint/Properties.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function setAdditionalProperties(Schema $additionalProperties = null)
4444
}
4545

4646

47-
/** @var Egg[] */
47+
/** @var Egg[][] */
4848
private $nestedProperties = array();
4949

5050
/** @var Schema[] */
@@ -57,7 +57,7 @@ protected function addNested(Schema $nested, $name)
5757
}
5858
$this->nestedPropertyNames[$name] = $name;
5959
foreach ($nested->properties->toArray() as $propertyName => $property) {
60-
$this->nestedProperties[$propertyName] = new Egg($nested, $name, $property);
60+
$this->nestedProperties[$propertyName][] = new Egg($nested, $name, $property);
6161
}
6262
return $this;
6363
}

src/MagicMap.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,37 +56,4 @@ public function jsonSerialize()
5656
{
5757
return (object)$this->__arrayOfData;
5858
}
59-
60-
private $__propertyToDataMap;
61-
public function setPropertyToDataMap($map)
62-
{
63-
$this->__propertyToDataMap = $map;
64-
return $this;
65-
}
66-
67-
public function getPropertyToDataMap()
68-
{
69-
return $this->__propertyToDataMap;
70-
}
71-
72-
private $__dataToPropertyMap;
73-
74-
/**
75-
* @return mixed
76-
*/
77-
public function getDataToPropertyMap()
78-
{
79-
return $this->__dataToPropertyMap;
80-
}
81-
82-
/**
83-
* @param mixed $_dataToPropertyMap
84-
* @return MagicMap
85-
*/
86-
public function setDataToPropertyMap($_dataToPropertyMap)
87-
{
88-
$this->__dataToPropertyMap = $_dataToPropertyMap;
89-
return $this;
90-
}
91-
9259
}

src/Schema.php

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,9 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
281281
}
282282
}
283283

284-
$propertyMap = null;
285284
if ($this->properties !== null) {
286285
/** @var Schema[] $properties */
287286
$properties = &$this->properties->toArray(); // TODO check performance of pointer
288-
if ($import) {
289-
$propertyMap = $this->properties->getDataToPropertyMap(); // TODO check performance of pointer
290-
} else {
291-
$propertyMap = $this->properties->getPropertyToDataMap(); // TODO check performance of pointer
292-
}
293-
294287
$nestedProperties = $this->properties->getNestedProperties();
295288
}
296289

@@ -317,22 +310,20 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
317310
}
318311
}
319312

313+
$propertyFound = false;
320314
if (isset($properties[$key])) {
321-
if (null !== $propertyMap) {
322-
323-
324-
}
325-
315+
$propertyFound = true;
326316
$found = true;
327317
$value = $properties[$key]->process($value, $import, $preProcessor, $path . '->properties:' . $key);
328318
}
329319

330-
/** @var Egg $nestedEgg */
331-
$nestedEgg = null;
332-
if (!$found && isset($nestedProperties[$key])) {
320+
/** @var Egg[] $nestedEggs */
321+
$nestedEggs = null;
322+
if (isset($nestedProperties[$key])) {
333323
$found = true;
334-
$nestedEgg = $nestedProperties[$key];
335-
$value = $nestedEgg->propertySchema->process($value, $import, $preProcessor, $path . '->nestedProperties:' . $key);
324+
$nestedEggs = $nestedProperties[$key];
325+
// todo iterate all nested props?
326+
$value = $nestedEggs[0]->propertySchema->process($value, $import, $preProcessor, $path . '->nestedProperties:' . $key);
336327
}
337328

338329
if ($this->patternProperties !== null) {
@@ -358,8 +349,13 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
358349
}
359350
}
360351

361-
if ($nestedEgg && $import) {
362-
$result->setNestedProperty($key, $value, $nestedEgg);
352+
if ($nestedEggs && $import) {
353+
foreach ($nestedEggs as $nestedEgg) {
354+
$result->setNestedProperty($key, $value, $nestedEgg);
355+
}
356+
if ($propertyFound) {
357+
$result->$key = $value;
358+
}
363359
} else {
364360
$result->$key = $value;
365361
}
@@ -422,6 +418,26 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
422418
return $result;
423419
}
424420

421+
/**
422+
* @param bool|Schema $additionalProperties
423+
* @return Schema
424+
*/
425+
public function setAdditionalProperties($additionalProperties)
426+
{
427+
$this->additionalProperties = $additionalProperties;
428+
return $this;
429+
}
430+
431+
/**
432+
* @param Schema|Schema[] $items
433+
* @return Schema
434+
*/
435+
public function setItems($items)
436+
{
437+
$this->items = $items;
438+
return $this;
439+
}
440+
425441

426442
private function fail(InvalidValue $exception, $path)
427443
{

src/Structure/ClassStructure.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public static function schema()
3030
return $schema;
3131
}
3232

33+
/**
34+
* @return Properties|static
35+
*/
36+
public static function properties()
37+
{
38+
return static::schema()->properties;
39+
}
40+
3341
/**
3442
* @param $data
3543
* @param DataPreProcessor $preProcessor

tests/src/Helper/UserInfo.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
class UserInfo extends ClassStructure
1111
{
12+
public $id;
1213
public $firstName;
1314
public $lastName;
1415
public $birthDay;
@@ -19,9 +20,12 @@ class UserInfo extends ClassStructure
1920
*/
2021
public static function setUpProperties($properties, Schema $ownerSchema)
2122
{
23+
$properties->id = User::properties()->id;
2224
$properties->firstName = Schema::string();
2325
$properties->lastName = Schema::string();
2426
$properties->birthDay = Schema::string();
2527
$properties->birthDay->format = Schema::FORMAT_DATE_TIME;
28+
29+
$ownerSchema->required[] = self::names()->id;
2630
}
2731
}

tests/src/PHPUnit/Example/ExampleTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ public function testNestedStructure()
155155
$this->assertSame($json, json_encode($exported, JSON_PRETTY_PRINT));
156156

157157
$imported = User::import(json_decode($json));
158+
$this->assertSame(1, $imported->id);
159+
160+
$this->assertSame(1, $imported->info->id);
158161
$this->assertSame('John', $imported->info->firstName);
159162
$this->assertSame('Doe', $imported->info->lastName);
160163
}

0 commit comments

Comments
 (0)