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

Add functions to remove elements #194

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/node_modules

/.php_cs.cache
/.php-cs-fixer.cache
/.phpunit.result.cache

php-cs-fixer.phar
2 changes: 1 addition & 1 deletion .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

return PhpCsFixer\Config::create()
return ( new PhpCsFixer\Config())
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ check-style: php-cs-fixer.phar

fix-style: php-cs-fixer.phar
$(DOCKER_PHP) vendor/bin/indent --tabs composer.json
$(DOCKER_PHP) vendor/bin/indent --spaces .php_cs.dist
$(DOCKER_PHP) vendor/bin/indent --spaces .php-cs-fixer.dist.php
$(DOCKER_PHP) ./php-cs-fixer.phar fix src/ --diff

install: composer.lock yarn.lock
Expand Down
6 changes: 3 additions & 3 deletions src/ReferenceContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class ReferenceContext
* The result will be a single API description file with references
* inside of the file structure.
*/
const RESOLVE_MODE_INLINE = 'inline';
public const RESOLVE_MODE_INLINE = 'inline';
/**
* resolve all references, except recursive ones.
*/
const RESOLVE_MODE_ALL = 'all';
public const RESOLVE_MODE_ALL = 'all';

/**
* @var bool whether to throw UnresolvableReferenceException in case a reference can not
Expand Down Expand Up @@ -103,7 +103,7 @@ private function buildUri($parts)
$host = $parts['host'] ?? '';
$port = !empty($parts['port']) ? ':' . $parts['port'] : '';
$user = $parts['user'] ?? '';
$pass = !empty($parts['pass']) ? ':' . $parts['pass'] : '';
$pass = !empty($parts['pass']) ? ':' . $parts['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = $parts['path'] ?? '';
$query = !empty($parts['query']) ? '?' . $parts['query'] : '';
Expand Down
38 changes: 38 additions & 0 deletions src/SpecBaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,35 @@ protected function validateUrl(string $property)
}
}


protected function deleteProperty($key, $name = null, $value = null): void
{
if(is_array($this->$key) && $name) {
$this->_properties[$key] = $this->_removeKey($this->$key, $name, $value);
} else {
$this->_properties = $this->_removeKey($this->_properties, $key);
}
}

private function _removeKey(array $arrayData, String $name, $value = null): array
{
if(!empty($arrayData[$name])) {
$properties = $arrayData;
unset($properties[$name]);
return $properties;
}
foreach($arrayData as $key => $val) {
if($name === $val) {
unset($arrayData[$key]);
} elseif($val instanceof self && !empty($val->$name) && $val->$name === $value) {
unset($arrayData[$key]);
} elseif($val instanceof self && !empty($val->$name) && is_array($val->$name)) {
unset($arrayData[$key]);
}
}
return $arrayData;
}

public function __get($name)
{
if (isset($this->_properties[$name])) {
Expand Down Expand Up @@ -525,4 +554,13 @@ public function getExtensions(): array
}
return $extensions;
}

/**
* Remove a property or an attribute
* @param string $name name of property to be removed
*/
public function removeProperty($name)
{
$this->deleteProperty($name);
}
}
10 changes: 5 additions & 5 deletions src/json/JsonPointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ public function evaluate($jsonDocument)

foreach ($this->getPath() as $part) {
if (is_array($currentReference)) {
// if (!preg_match('~^([1-9]*[0-9]|-)$~', $part)) {
// throw new NonexistentJsonPointerReferenceException(
// "Failed to evaluate pointer '$this->_pointer'. Invalid pointer path '$part' for Array at path '$currentPath'."
// );
// }
// if (!preg_match('~^([1-9]*[0-9]|-)$~', $part)) {
// throw new NonexistentJsonPointerReferenceException(
// "Failed to evaluate pointer '$this->_pointer'. Invalid pointer path '$part' for Array at path '$currentPath'."
// );
// }
if ($part === '-' || !array_key_exists($part, $currentReference)) {
throw new NonexistentJsonPointerReferenceException(
"Failed to evaluate pointer '$this->_pointer'. Array has no member $part at path '$currentPath'."
Expand Down
40 changes: 40 additions & 0 deletions src/spec/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,46 @@
*/
class Components extends SpecBaseObject
{
/**
* @param string $name schema name
*/
public function removeSchema(string $name): void
{
$this->deleteProperty('schemas', $name);
}

/**
* @param string $name parameter name
*/
public function removeParameter(string $name): void
{
$this->deleteProperty('parameters', $name);
}

/**
* @param string $name response name
*/
public function removeResponse(string $name): void
{
$this->deleteProperty('responses', $name);
}

/**
* @param string $name securityScheme name
*/
public function removeSecuityScheme(string $name): void
{
$this->deleteProperty('securitySchemes', $name);
}

/**
* @param string $name request body name
*/
public function removeRequestBody(string $name): void
{
$this->deleteProperty('requestBodies', $name);
}

/**
* @return array array of attributes available in this object.
*/
Expand Down
1 change: 0 additions & 1 deletion src/spec/Encoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
class Encoding extends SpecBaseObject
{

/**
* @return array array of attributes available in this object.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/spec/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@ protected function performValidation()
$this->addError('Link: operationId and operationRef are mutually exclusive.');
}
}

/**
* @param string $name parameter name
*/
public function removeParameter(string $name): void
{
$this->deleteProperty('parameters', $name);
}
}
16 changes: 16 additions & 0 deletions src/spec/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ public function __construct(array $data)
}
}

/**
* @param string $name example name
*/
public function removeExample(string $name): void
{
$this->deleteProperty('examples', $name);
}

/**
* @param string $name example name
*/
public function removeEncoding(string $name): void
{
$this->deleteProperty('encoding', $name);
}

/**
* Perform validation on this object, check data against OpenAPI Specification rules.
*/
Expand Down
9 changes: 8 additions & 1 deletion src/spec/OAuthFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
class OAuthFlow extends SpecBaseObject
{

/**
* @return array array of attributes available in this object.
*/
Expand All @@ -35,6 +34,14 @@ protected function attributes(): array
];
}

/**
* @param string $name scope's name
*/
public function removeScope(string $name): void
{
$this->deleteProperty('scopes', $name);
}

/**
* Perform validation on this object, check data against OpenAPI Specification rules.
*
Expand Down
24 changes: 24 additions & 0 deletions src/spec/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ protected function attributes(): array
];
}

/**
* @param string $name name of security attribute requirement
*/
public function removeSecurity(string $name): void
{
$this->deleteProperty('security', $name);
}

/**
* @param string $name tag name
*/
public function removeTag(string $name): void
{
$this->deleteProperty('tags', $name);
}

/**
* @param string $url server's url value
*/
public function removeServer(string $url): void
{
$this->deleteProperty('servers', 'url', $url);
}

/**
* @return array array of attributes default values.
*/
Expand Down
34 changes: 33 additions & 1 deletion src/spec/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
*/
class Operation extends SpecBaseObject
{

/**
* @return array array of attributes available in this object.
*/
Expand Down Expand Up @@ -58,6 +57,39 @@ protected function attributeDefaults(): array
];
}

/**
* @param string $name tag name
*/
public function removeTag(string $name): void
{
$this->deleteProperty('tags', $name);
}

/**
* @param string $name parameter's property key
*/
public function removeParameter(string $name): void
{
$this->deleteProperty('parameters', 'name', $name);
}

/**
* @param string $name name of security requirement
*/
public function removeSecurity(string $name): void
{
$this->deleteProperty('security', $name);
}

/**
* @param string $name server's property key
*/
public function removeServer(string $name): void
{
$this->deleteProperty('servers', 'url', $name);
}


/**
* Perform validation on this object, check data against OpenAPI Specification rules.
*
Expand Down
26 changes: 26 additions & 0 deletions src/spec/PathItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,30 @@ public function setDocumentContext(SpecObjectInterface $baseDocument, JsonPointe
$this->_ref->setDocumentContext($baseDocument, $jsonPointer->append('$ref'));
}
}

/**
* @param string $name parameter's property key
*/
public function removeParameter(string $name): void
{
$this->deleteProperty('parameters', 'name', $name);
}

/**
* @param string $name server's url
*/
public function removeServer(string $name): void
{
$this->deleteProperty('servers', 'url', $name);
}

/**
* @param string $httpVerb is http verb you wish to delete. eg: get, post, etc
* @param string $operationId is unique identify of Operation
*/
public function removeOperation(string $httpVerb, $operationId): void
{
$this->deleteProperty($httpVerb, 'operationId', $operationId);
}

}
2 changes: 1 addition & 1 deletion src/spec/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function setContext(ReferenceContext $context)
/**
* @return ReferenceContext
*/
public function getContext() : ?ReferenceContext
public function getContext(): ?ReferenceContext
{
return $this->_context;
}
Expand Down
24 changes: 24 additions & 0 deletions src/spec/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ protected function attributes(): array
];
}

/**
* @param string $name headers' name
*/
public function removeHeader(string $name): void
{
$this->deleteProperty('headers', $name);
}

/**
* @param string $operationId headers' operationId
*/
public function removeLinkByOperationId(string $operationId): void
{
$this->deleteProperty('links', 'operationId', $operationId);
}

/**
* @param string $operationRef link's operationRef
*/
public function removeLinkByoperationRef(string $operationRef): void
{
$this->deleteProperty('links', 'operationRef', $operationRef);
}

/**
* Perform validation on this object, check data against OpenAPI Specification rules.
*/
Expand Down
14 changes: 7 additions & 7 deletions src/spec/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
*/
class Type
{
const ANY = 'any';
const INTEGER = 'integer';
const NUMBER = 'number';
const STRING = 'string';
const BOOLEAN = 'boolean';
const OBJECT = 'object';
const ARRAY = 'array';
public const ANY = 'any';
public const INTEGER = 'integer';
public const NUMBER = 'number';
public const STRING = 'string';
public const BOOLEAN = 'boolean';
public const OBJECT = 'object';
public const ARRAY = 'array';

/**
* Indicate whether a type is a scalar type, i.e. not an array or object.
Expand Down
Loading