/** * Validates an object * * @param stdClass $element object properties * @param AbstractSpecObject $schema schema this object generated of api specification */ protected function checkObject($element, $schema) { if (isset($schema->sample)) { $this->checkSample($element, $schema->sample); return; } if (isset($schema->discriminator)) { $discriminator = $schema->discriminator; if (!property_exists($element, $discriminator)) { $propName = $schema->getObjectName(); if (isset(static::$ignoreDiscriminatorValues[$propName]) && $discriminator == static::$ignoreDiscriminatorValues[$propName]) { ApiTest::markTestIncomplete(sprintf('%s unexpected discriminator value', $discriminator)); } else { $this->appendError($discriminator, ' unexpected discriminator value'); } return; } $schema = $this->getConcreteTypes($schema, $element->{$discriminator}); if (!$schema) { $this->appendError($element->{$discriminator}, ' unexpected concrete type value'); } unset($schema->discriminator); $this->check($element, $schema); return; } $properties = $schema instanceof ApiEntity ? $schema->getProperties() : (array) $schema; foreach ($element as $p => $value) { if (isset($properties[$p])) { $this->check($value, $properties[$p]); } else { $this->appendError($p, "this property don't exist in Api specifications"); } } return; }
/** * Resolve schema references to object * * @param array $schema segment Api specifications * @param mixed $specObject optional object schema view * @return DetailResponse|ListResponse */ protected function resolveReferences($schema, $specObject = null) { $ref = '$ref'; if (array_key_exists($ref, $schema)) { $refPath = explode('/', ltrim($schema[$ref], '#/')); unset($schema[$ref]); $object = AbstractSpecObject::init($refPath[1]); if (is_null($specObject)) { $specObject = $object; } else { $specObject->entity = $object; foreach ($schema as $key => $value) { $specObject->{$key} = $value; } } $schema = $this->getPath(...$refPath); } else { if (is_null($specObject)) { $specObject = new Property(); } $object = $specObject; } if (array_key_exists('items', $schema)) { $object->items = $this->resolveReferences($schema['items']); unset($schema['items']); } if (array_key_exists('properties', $schema)) { foreach ($schema['properties'] as $nameProp => $prop) { if (!array_key_exists('type', $prop)) { $prop['type'] = $nameProp; } $object->{$nameProp} = $this->resolveReferences($prop, new Property($nameProp)); } unset($schema['properties']); } if (array_key_exists('x-concreteTypes', $schema)) { foreach ($schema['x-concreteTypes'] as $prop) { $enumObject = $this->resolveReferences($prop); $object->concreteTypes[$enumObject->getObjectName()] = $enumObject; } unset($schema['x-concreteTypes']); } foreach ($schema as $key => $value) { $object->{$key} = $value; } return $specObject; }
/** *{@inheritdoc} * @see \PHPUnit_Framework_Constraint::toString() */ public function toString() { return $this->specObject->getObjectName(); }