Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/serializer-support' into seriali…
Browse files Browse the repository at this point in the history
…zer-support
  • Loading branch information
barw4 committed Oct 15, 2024
2 parents d7db4db + 46032c9 commit 31fcff8
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 793 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
$configFactory = new InternalConfigFactory();
$configFactory->withRules([
'declare_strict_types' => false,
'phpdoc_no_empty_return' => false,
]);

return $configFactory
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline-7.4.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ parameters:

-
message: "#^Parameter \\#2 \\$str of function explode expects string, string\\|null given\\.$#"
count: 1
count: 4
path: src/lib/Server/Controller/Content.php

-
Expand Down
55 changes: 0 additions & 55 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -695,26 +695,11 @@ parameters:
count: 1
path: src/lib/Output/Generator/Json/FieldTypeHashGenerator.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\FieldTypeHashGenerator\\:\\:generateArrayValue\\(\\) has parameter \\$value with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Output/Generator/Json/FieldTypeHashGenerator.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\FieldTypeHashGenerator\\:\\:generateHashArray\\(\\) has parameter \\$hashArray with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Output/Generator/Json/FieldTypeHashGenerator.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\FieldTypeHashGenerator\\:\\:generateHashValue\\(\\) has no return type specified\\.$#"
count: 1
path: src/lib/Output/Generator/Json/FieldTypeHashGenerator.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\FieldTypeHashGenerator\\:\\:generateListArray\\(\\) has parameter \\$listArray with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Output/Generator/Json/FieldTypeHashGenerator.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Json\\\\FieldTypeHashGenerator\\:\\:isNumericArray\\(\\) has parameter \\$value with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -2250,11 +2235,6 @@ parameters:
count: 1
path: src/lib/Server/Output/ValueObjectVisitor/ContentFieldValidationException.php

-
message: "#^Method Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\ContentFieldValidationException\\:\\:visit\\(\\) has no return type specified\\.$#"
count: 1
path: src/lib/Server/Output/ValueObjectVisitor/ContentFieldValidationException.php

-
message: "#^Method Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\ContentList\\:\\:visit\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -2510,16 +2490,6 @@ parameters:
count: 1
path: src/lib/Server/Output/ValueObjectVisitor/DeletedUserSession.php

-
message: "#^Method Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\Exception\\:\\:visit\\(\\) has no return type specified\\.$#"
count: 1
path: src/lib/Server/Output/ValueObjectVisitor/Exception.php

-
message: "#^Property Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\Exception\\:\\:\\$httpStatusCodes type has no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/Server/Output/ValueObjectVisitor/Exception.php

-
message: "#^Property Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\Exception\\:\\:\\$translator \\(Symfony\\\\Contracts\\\\Translation\\\\TranslatorInterface\\) does not accept Symfony\\\\Contracts\\\\Translation\\\\TranslatorInterface\\|null\\.$#"
count: 1
Expand Down Expand Up @@ -6020,31 +5990,6 @@ parameters:
count: 1
path: tests/lib/Output/Generator/JsonTest.php

-
message: "#^Call to method setFormatOutput\\(\\) on an unknown class Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Xml\\.$#"
count: 1
path: tests/lib/Output/Generator/Xml/FieldTypeHashGeneratorTest.php

-
message: "#^Instantiated class Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Xml not found\\.$#"
count: 1
path: tests/lib/Output/Generator/Xml/FieldTypeHashGeneratorTest.php

-
message: "#^Instantiated class Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Xml\\\\FieldTypeHashGenerator not found\\.$#"
count: 1
path: tests/lib/Output/Generator/Xml/FieldTypeHashGeneratorTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\Generator\\\\Xml\\\\FieldTypeHashGeneratorTest\\:\\:initializeFieldTypeHashGenerator\\(\\) has invalid return type Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Xml\\\\FieldTypeHashGenerator\\.$#"
count: 1
path: tests/lib/Output/Generator/Xml/FieldTypeHashGeneratorTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\Generator\\\\Xml\\\\FieldTypeHashGeneratorTest\\:\\:initializeGenerator\\(\\) has invalid return type Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Xml\\.$#"
count: 1
path: tests/lib/Output/Generator/Xml/FieldTypeHashGeneratorTest.php

-
message: "#^Parameter \\#1 \\$boolValue of method Ibexa\\\\Contracts\\\\Rest\\\\Output\\\\Generator\\:\\:serializeBool\\(\\) expects bool, string given\\.$#"
count: 1
Expand Down
161 changes: 161 additions & 0 deletions src/contracts/Output/Exceptions/AbstractExceptionVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace Ibexa\Contracts\Rest\Output\Exceptions;

use Ibexa\Contracts\Rest\Output\Generator;
use Ibexa\Contracts\Rest\Output\ValueObjectVisitor;
use Ibexa\Contracts\Rest\Output\Visitor;
use Ibexa\Core\Base\Translatable;
use JMS\TranslationBundle\Annotation\Desc;
use JMS\TranslationBundle\Annotation\Ignore;
use Symfony\Contracts\Translation\TranslatorInterface;

abstract class AbstractExceptionVisitor extends ValueObjectVisitor
{
/**
* Mapping of HTTP status codes to their respective error messages.
*
* @var array<int, string>
*/
protected static $httpStatusCodes = [
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested range not satisfiable',
417 => 'Expectation Failed',
418 => "I'm a teapot",
421 => 'There are too many connections from your internet address',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
425 => 'Unordered Collection',
426 => 'Upgrade Required',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
509 => 'Bandwidth Limit Exceeded',
510 => 'Not Extended',
];

/**
* Returns HTTP status code.
*
* @return int
*/
protected function getStatus()
{
return 500;
}

/**
* @param \Exception $data
*
* @return void
*/
public function visit(Visitor $visitor, Generator $generator, $data)
{
$generator->startObjectElement('ErrorMessage');

$visitor->setHeader('Content-Type', $generator->getMediaType('ErrorMessage'));

$statusCode = $this->generateErrorCode($generator, $visitor, $data);

$errorMessage = $this->getErrorMessage($data, $statusCode);
$generator->valueElement('errorMessage', $errorMessage);

$errorDescription = $this->getErrorDescription($data, $statusCode);
$generator->valueElement('errorDescription', $errorDescription);

if ($this->canDisplayExceptionTrace()) {
$generator->valueElement('trace', $data->getTraceAsString());
$generator->valueElement('file', $data->getFile());
$generator->valueElement('line', $data->getLine());
}

$previous = $data->getPrevious();
if ($previous !== null && $this->canDisplayPreviousException()) {
$generator->startObjectElement('Previous', 'ErrorMessage');
$visitor->visitValueObject($previous);
$generator->endObjectElement('Previous');
}

$generator->endObjectElement('ErrorMessage');
}

protected function generateErrorCode(Generator $generator, Visitor $visitor, \Exception $e): int
{
$statusCode = $this->getStatus();
$visitor->setStatus($statusCode);

$generator->valueElement('errorCode', $statusCode);

return $statusCode;
}

protected function getErrorMessage(\Exception $data, int $statusCode): string
{
return static::$httpStatusCodes[$statusCode] ?? static::$httpStatusCodes[500];
}

protected function getErrorDescription(\Exception $data, int $statusCode): string
{
$translator = $this->getTranslator();
if ($statusCode < 500 || $this->canDisplayExceptionMessage()) {
$errorDescription = $data instanceof Translatable && $translator
? /** @Ignore */
$translator->trans($data->getMessageTemplate(), $data->getParameters(), 'ibexa_repository_exceptions')
: $data->getMessage();
} else {
// Do not leak any file paths and sensitive data on production environments
$errorDescription = $translator
? /** @Desc("An error has occurred. Please try again later or contact your Administrator.") */
$translator->trans('non_verbose_error', [], 'ibexa_repository_exceptions')
: 'An error has occurred. Please try again later or contact your Administrator.';
}

return $errorDescription;
}

protected function getTranslator(): ?TranslatorInterface
{
return null;
}

protected function canDisplayExceptionTrace(): bool
{
return false;
}

protected function canDisplayPreviousException(): bool
{
return false;
}

protected function canDisplayExceptionMessage(): bool
{
return false;
}
}
29 changes: 11 additions & 18 deletions src/lib/Output/Generator/InMemory/Xml/FieldTypeHashGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Ibexa\Rest\Output\Generator\InMemory\Xml;

use Ibexa\Rest\Output\Generator\Data\ArrayList;
use Ibexa\Rest\Output\Generator\Json\FieldTypeHashGenerator as JsonFieldTypeHashGenerator;
use Ibexa\Rest\Output\Generator\Json\JsonObject;

Expand All @@ -24,17 +23,13 @@ protected function generateValue($parent, $value): mixed
return sprintf('%F', $value);
} elseif (is_array($value)) {
return $this->generateArrayValue($parent, $value);
} elseif (is_object($value)) {
return $this->generateObjectValue($parent, $value);
} else {
return $value;
}
}

/**
* Generates an array value from $value.
*
* @param array $value
* @param string|null $key
*/
protected function generateArrayValue($parent, $value)
{
if ($this->isNumericArray($value)) {
Expand All @@ -44,22 +39,20 @@ protected function generateArrayValue($parent, $value)
}
}

/**
* Generates a JSON array from the given $hashArray with $parent.
*
* @param \Ibexa\Rest\Output\Generator\Json\ArrayObject|\Ibexa\Rest\Output\Generator\Json\JsonObject $parent
* @param array $listArray
*
* @return \Ibexa\Rest\Output\Generator\Json\ArrayObject
*/
protected function generateListArray($parent, array $listArray)
{
$arrayList = new ArrayList('value', $parent);
$object = new JsonObject($parent);

/** @phpstan-ignore-next-line */
$object->value = [];

foreach ($listArray as $listItem) {
$arrayList->append($this->generateValue($parent, $listItem));
$object->value[] = [
'#' => $this->generateValue($object, $listItem),
];
}

return $arrayList;
return $object;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/lib/Output/Generator/Json/FieldTypeHashGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected function generateValue($parent, $value)
* JSON array, otherwise a JSON object
*
* @param \Ibexa\Rest\Output\Generator\Json\ArrayObject|\Ibexa\Rest\Output\Generator\Json\JsonObject $parent
* @param array $value
* @param array<mixed> $value
*
* @return \Ibexa\Rest\Output\Generator\Json\ArrayObject|\Ibexa\Rest\Output\Generator\Json\JsonObject
*/
Expand All @@ -97,9 +97,9 @@ protected function generateArrayValue($parent, array $value)
* Generates a JSON array from the given $hashArray with $parent.
*
* @param \Ibexa\Rest\Output\Generator\Json\ArrayObject|\Ibexa\Rest\Output\Generator\Json\JsonObject $parent
* @param array $listArray
* @param array<int> $listArray
*
* @return \Ibexa\Rest\Output\Generator\Json\ArrayObject
* @return \Ibexa\Rest\Output\Generator\Json\ArrayObject|JsonObject
*/
protected function generateListArray($parent, array $listArray)
{
Expand All @@ -115,7 +115,7 @@ protected function generateListArray($parent, array $listArray)
* Generates a JSON object from the given $hashArray with $parent.
*
* @param \Ibexa\Rest\Output\Generator\Json\ArrayObject|\Ibexa\Rest\Output\Generator\Json\JsonObject $parent
* @param array $hashArray
* @param array<mixed> $hashArray
*
* @return \Ibexa\Rest\Output\Generator\Json\JsonObject
*/
Expand Down Expand Up @@ -152,7 +152,7 @@ protected function isNumericArray(array $value)
*
* @return mixed
*/
private function generateObjectValue($parent, object $value)
protected function generateObjectValue($parent, object $value)
{
try {
$value = $this->normalizer->normalize($value, 'json', ['parent' => $parent]);
Expand Down
Loading

0 comments on commit 31fcff8

Please sign in to comment.