Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix additionalProperties check #64

Merged
merged 2 commits into from
Dec 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type": "library",
"require": {
"php": ">=5.4",
"ext-json": "*",
"phplang/scope-exit": "^1.0",
"swaggest/json-diff": "^3.4.2"
},
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ parameters:
- '#PHPDoc tag @param references unknown parameter \$schema#'
- '#Access to an undefined property static\(Swaggest\\JsonSchema\\JsonSchema\)\|Swaggest\\JsonSchema\\Constraint\\Properties::#'
- '#Accessing property \$skipValidation on possibly null value of type Swaggest\\JsonSchema\\Context\|null#'
- '#Accessing property \$__propertyToData on possibly null value of type Swaggest\\JsonSchema\\Constraint\\Properties\|null#'
- '#Accessing property \$__dataToProperty on possibly null value of type Swaggest\\JsonSchema\\Constraint\\Properties\|null#'
84 changes: 69 additions & 15 deletions src/Constraint/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

/**
* @method SchemaContract __get($key)
* @method Schema[] toArray()
*/
class Properties extends ObjectItem implements Constraint
{
Expand All @@ -23,18 +22,84 @@ class Properties extends ObjectItem implements Constraint
/** @var Schema */
protected $__schema;

/**
* @var Schema[]
*/
private $__mappedProperties;

/**
* @var array
*/
private $__dataKeyMaps = array();

/**
* Data to property mapping, example ["$ref" => "ref"]
* @var array
*/
public $__dataToProperty = array();

/**
* Property to data mapping, example ["ref" => "$ref"]
* @var array
*/
public $__defaultMapping = array();
public $__propertyToData = array();

/**
* Returns a map of properties by default data name
* @return Schema[]
*/
public function &toArray()
{
if (!isset($this->__propertyToData[Schema::DEFAULT_MAPPING])) {
return $this->__arrayOfData;
}
if (null === $this->__mappedProperties) {
$properties = array();
foreach ($this->__arrayOfData as $propertyName => $property) {
if (isset($this->__propertyToData[Schema::DEFAULT_MAPPING][$propertyName])) {
$propertyName = $this->__propertyToData[Schema::DEFAULT_MAPPING][$propertyName];
}
$properties[$propertyName] = $property;
}
$this->__mappedProperties = $properties;
}
return $this->__mappedProperties;
}

/**
* @param string $mapping
* @return string[] a map of propertyName to dataName
*/
public function getDataKeyMap($mapping = Schema::DEFAULT_MAPPING)
{
if (!isset($this->__dataKeyMaps[$mapping])) {
$map = array();
foreach ($this->__arrayOfData as $propertyName => $property) {
if (isset($this->__propertyToData[$mapping][$propertyName])) {
$map[$propertyName] = $this->__propertyToData[$mapping][$propertyName];
} else {
$map[$propertyName] = $propertyName;
}
}
$this->__dataKeyMaps[$mapping] = $map;
}

return $this->__dataKeyMaps[$mapping];
}

public function lock()
{
$this->__isReadOnly = true;
return $this;
}

public function addPropertyMapping($dataName, $propertyName, $mapping = Schema::DEFAULT_MAPPING)
{
$this->__dataToProperty[$mapping][$dataName] = $propertyName;
$this->__propertyToData[$mapping][$propertyName] = $dataName;
}


/**
* @param string $name
* @param mixed $column
Expand Down Expand Up @@ -101,7 +166,8 @@ public function isEmpty()

public function jsonSerialize()
{
$result = $this->__arrayOfData;
$result = $this->toArray();

if ($this->__nestedObjects) {
foreach ($this->__nestedObjects as $object) {
foreach ($object->toArray() as $key => $value) {
Expand All @@ -110,18 +176,6 @@ public function jsonSerialize()
}
}

if (isset($this->__defaultMapping)) {
$mappedResult = new \stdClass();
foreach ($result as $key => $value) {
if (isset($this->__defaultMapping[$key])) {
$mappedResult->{$this->__defaultMapping[$key]} = $value;
} else {
$mappedResult->$key = $value;
}
}
return $mappedResult;
}

return (object)$result;
}

Expand Down
Loading