-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add responses and new way for default responses
- Loading branch information
Showing
7 changed files
with
201 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioBackendBundle\Note\Attributes\Response\Property; | ||
|
||
use OpenApi\Attributes\Items; | ||
use OpenApi\Attributes\Property; | ||
use Pimcore\Bundle\StudioBackendBundle\Note\Schema\Note; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class NoteCollection extends Property | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct( | ||
'items', | ||
title: 'items', | ||
type: 'array', | ||
items: new Items(ref: Note::class) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response; | ||
|
||
use Attribute; | ||
use OpenApi\Attributes\JsonContent; | ||
use OpenApi\Attributes\Response; | ||
use OpenApi\Attributes\Schema; | ||
use Pimcore\Bundle\StudioBackendBundle\Response\Schemas; | ||
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[Attribute(Attribute::TARGET_METHOD)] | ||
final class DefaultResponses extends Response | ||
{ | ||
private array $defaultErrorCodes = [ | ||
HttpResponseCodes::BAD_REQUEST, | ||
HttpResponseCodes::METHOD_NOT_ALLOWED, | ||
HttpResponseCodes::UNSUPPORTED_MEDIA_TYPE, | ||
HttpResponseCodes::UNPROCESSABLE_CONTENT, | ||
]; | ||
|
||
public function __construct(array $codes = []) | ||
{ | ||
$description = $this->generateDescription($codes); | ||
|
||
parent::__construct( | ||
response: 'default', | ||
description: $description, | ||
content: new JsonContent( | ||
oneOf: array_map(static function ($class) { | ||
return new Schema(ref: $class); | ||
}, Schemas::ERRORS), | ||
) | ||
); | ||
} | ||
|
||
private function generateDescription(array $errorCodes): string | ||
{ | ||
// merge the default error codes with the provided ones | ||
$errorCodes = array_merge($this->defaultErrorCodes, $errorCodes); | ||
|
||
// Sort the array of enums by http status code | ||
usort($errorCodes, static function($a, $b) { | ||
return $a->value <=> $b->value; | ||
}); | ||
|
||
// Generate description block of http codes | ||
$errorCodes = array_map(function ($code) { | ||
return sprintf('%s - %s', $code->value, $this->generateNiceName($code->name)); | ||
}, $errorCodes); | ||
|
||
return implode('<br>', $errorCodes); | ||
} | ||
|
||
private function generateNiceName(string $name): string { | ||
return ucwords(str_replace('_', ' ', strtolower($name))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioBackendBundle\Util\Constants; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
enum HttpResponseCodes: int | ||
{ | ||
case SUCCESS = 200; | ||
case NOT_COMPLETED = 202; | ||
case BAD_REQUEST = 400; | ||
case UNAUTHORIZED = 401; | ||
case FORBIDDEN = 403; | ||
case NOT_FOUND = 404; | ||
case METHOD_NOT_ALLOWED = 405; | ||
case UNSUPPORTED_MEDIA_TYPE = 415; | ||
case UNPROCESSABLE_CONTENT = 422; | ||
} |