Skip to content

Commit 629faf3

Browse files
authored
Merge pull request #10 from wol-soft/PatternProperties
Pattern properties
2 parents 6d1bb95 + 5909dba commit 629faf3

File tree

4 files changed

+100
-60
lines changed

4 files changed

+100
-60
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Exception\Object;
6+
7+
use PHPModelGenerator\Exception\ValidationException;
8+
9+
/**
10+
* Class InvalidPatternPropertiesException
11+
*
12+
* @package PHPModelGenerator\Exception\Object
13+
*/
14+
class InvalidPatternPropertiesException extends ValidationException
15+
{
16+
/** @var ValidationException[][] */
17+
protected $nestedExceptions;
18+
/** @var string */
19+
protected $pattern;
20+
21+
/**
22+
* InvalidAdditionalPropertiesException constructor.
23+
*
24+
* @param $providedValue
25+
* @param string $propertyName
26+
* @param ValidationException[][] $nestedExceptions
27+
*/
28+
public function __construct($providedValue, string $propertyName, string $pattern, $nestedExceptions)
29+
{
30+
$this->nestedExceptions = $nestedExceptions;
31+
$this->pattern = $pattern;
32+
33+
parent::__construct($this->getErrorMessage($propertyName), $propertyName, $providedValue);
34+
}
35+
36+
/**
37+
* Get a list of nested exceptions containing all failed validations indexed by the required pattern
38+
*
39+
* @return ValidationException[][]
40+
*/
41+
public function getNestedExceptions(): array
42+
{
43+
return $this->nestedExceptions;
44+
}
45+
46+
/**
47+
* @return string
48+
*/
49+
public function getPattern(): string
50+
{
51+
return $this->pattern;
52+
}
53+
54+
protected function getErrorMessage(string $propertyName): string
55+
{
56+
$output = '';
57+
foreach ($this->nestedExceptions as $nestedPropertyName => $exceptions) {
58+
$output .= sprintf(
59+
"\n - invalid property '%s' matching pattern '%s'\n * %s",
60+
$nestedPropertyName,
61+
$this->pattern,
62+
implode(
63+
"\n * ",
64+
str_replace(
65+
"\n",
66+
"\n ",
67+
array_map(function (ValidationException $exception): string {
68+
return $exception->getMessage();
69+
}, $exceptions)
70+
)
71+
)
72+
);
73+
}
74+
75+
return "Provided JSON for $propertyName contains invalid pattern properties." . $output;
76+
}
77+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Exception\Object;
6+
7+
use Exception;
8+
9+
/**
10+
* Class UnknownPatternPropertyException
11+
*
12+
* @package PHPModelGenerator\Exception\Object
13+
*/
14+
class UnknownPatternPropertyException extends Exception
15+
{
16+
}

src/Model/SerializedValue.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/Traits/SerializableTrait.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace PHPModelGenerator\Traits;
66

7-
use PHPModelGenerator\Model\SerializedValue;
8-
97
/**
108
* Provide methods to serialize generated models
119
*
@@ -68,27 +66,22 @@ public function toArray(array $except = [], int $depth = 512)
6866
}
6967

7068
if ($customSerializer = $this->_getCustomSerializerMethod($key)) {
71-
$this->handleSerializedValue($modelData, $key, $this->{$customSerializer}(), $depth, $except);
69+
$modelData[$key] = $this->_getSerializedValue($this->{$customSerializer}(), $depth, $except);
7270
continue;
7371
}
7472

7573
$modelData[$key] = $this->_getSerializedValue($this->$key, $depth, $except);
7674
}
7775

78-
return $modelData;
76+
return $this->resolveSerializationHook($modelData, $depth, $except);
7977
}
8078

81-
private function handleSerializedValue(array &$data, $key, $serializedValue, int $depth, array $except): void
79+
/**
80+
* Function can be overwritten by classes using the trait to hook into serialization
81+
*/
82+
protected function resolveSerializationHook(array $data, int $depth, array $except): array
8283
{
83-
if ($serializedValue instanceof SerializedValue &&
84-
$serializedValue->getSerializationStrategy() === SerializedValue::STRATEGY_MERGE_VALUE
85-
) {
86-
$data = array_merge($data, $this->_getSerializedValue($serializedValue->getSerializedValue(), $depth, $except));
87-
88-
return;
89-
}
90-
91-
$data[$key] = $this->_getSerializedValue($serializedValue, $depth, $except);
84+
return $data;
9285
}
9386

9487
private function _getSerializedValue($value, int $depth, array $except) {

0 commit comments

Comments
 (0)