Skip to content

Commit e133826

Browse files
committed
generating swagger schema
1 parent 30fc4c3 commit e133826

File tree

4 files changed

+6375
-6
lines changed

4 files changed

+6375
-6
lines changed

src/Schema.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ class Schema extends MagicMap
9898
public $objectItemClass;
9999
private $useObjectAsArray = false;
100100

101+
private $__dataToProperty = array();
102+
private $__propertyToData = array();
103+
104+
public function addPropertyMapping($dataName, $propertyName)
105+
{
106+
$this->__dataToProperty[$dataName] = $propertyName;
107+
$this->__propertyToData[$propertyName] = $dataName;
108+
return $this;
109+
}
110+
101111
public function import($data, DataPreProcessor $preProcessor = null)
102112
{
103113
return $this->process($data, true, $preProcessor);
@@ -216,7 +226,7 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
216226
}
217227
}
218228
if ($this->pattern !== null) {
219-
if (0 === preg_match($this->pattern, $data)) {
229+
if (0 === preg_match(Helper::toPregPattern($this->pattern), $data)) {
220230
$this->fail(new StringException('Does not match to '
221231
. $this->pattern, StringException::PATTERN_MISMATCH), $path);
222232
}
@@ -298,7 +308,24 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
298308
$nestedProperties = $this->properties->getNestedProperties();
299309
}
300310

301-
$array = (array)$data;
311+
$array = array();
312+
if (!empty($this->__dataToProperty)) {
313+
foreach ((array)$data as $key => $value) {
314+
if ($import) {
315+
if (isset($this->__dataToProperty[$key])) {
316+
$key = $this->__dataToProperty[$key];
317+
}
318+
} else {
319+
if (isset($this->__propertyToData[$key])) {
320+
$key = $this->__propertyToData[$key];
321+
}
322+
}
323+
$array[$key] = $value;
324+
}
325+
} else {
326+
$array = (array)$data;
327+
}
328+
302329
if ($this->minProperties !== null && count($array) < $this->minProperties) {
303330
$this->fail(new ObjectException("Not enough properties", ObjectException::TOO_FEW), $path);
304331
}
@@ -343,7 +370,7 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
343370

344371
if ($this->patternProperties !== null) {
345372
foreach ($this->patternProperties as $pattern => $propertySchema) {
346-
if (preg_match($pattern, $key)) {
373+
if (preg_match(Helper::toPregPattern($pattern), $key)) {
347374
$found = true;
348375
$value = $propertySchema->process($value, $import, $preProcessor, $path . '->patternProperties:' . $pattern);
349376
if ($import) {

src/SchemaLoader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ protected function readSchemaDeeper($schemaArray)
174174

175175
if (isset($schemaArray[self::PATTERN_PROPERTIES])) {
176176
foreach ($schemaArray[self::PATTERN_PROPERTIES] as $name => $data) {
177-
$schema->patternProperties[Helper::toPregPattern($name)] = $this->readSchemaDeeper($data);
177+
$schema->patternProperties[$name] = $this->readSchemaDeeper($data);
178178
}
179179
}
180180

@@ -267,7 +267,8 @@ protected function readSchemaDeeper($schemaArray)
267267

268268
// String
269269
if (isset($schemaArray[self::PATTERN])) {
270-
$schema->pattern = Helper::toPregPattern($schemaArray[self::PATTERN]);
270+
$schema->pattern = $schemaArray[self::PATTERN];
271+
271272
}
272273
if (isset($schemaArray[self::MIN_LENGTH])) {
273274
$schema->minLength = $schemaArray[self::MIN_LENGTH];

src/Structure/ClassStructure.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public static function schema()
2020

2121
if (null === $schema) {
2222
$schema = new ClassSchema();
23-
$schema->type = Type::OBJECT;
2423
$properties = new Properties();
2524
$schema->properties = $properties;
2625
$schema->objectItemClass = get_called_class();

0 commit comments

Comments
 (0)