-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zakhar Shokel
committed
Apr 22, 2024
1 parent
b47dd88
commit 5168da7
Showing
18 changed files
with
493 additions
and
595 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
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 |
---|---|---|
@@ -1,60 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Paysera\Bundle\ApiBundle\Annotation; | ||
|
||
use Paysera\Bundle\ApiBundle\Entity\RestRequestOptions; | ||
use Paysera\Bundle\ApiBundle\Service\Annotation\ReflectionMethodWrapper; | ||
use Paysera\Bundle\ApiBundle\Attribute\BodyContentType as BodyContentTypeAttribute; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"METHOD"}) | ||
*/ | ||
class BodyContentType implements RestAnnotationInterface | ||
class BodyContentType extends BodyContentTypeAttribute implements RestAnnotationInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $supportedContentTypes; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $jsonEncodedBody; | ||
|
||
public function __construct(array $options) | ||
{ | ||
$this->setSupportedContentTypes($options['supportedContentTypes']); | ||
$this->setJsonEncodedBody($options['jsonEncodedBody'] ?? false); | ||
} | ||
|
||
/** | ||
* @param array $supportedContentTypes | ||
* @return $this | ||
*/ | ||
private function setSupportedContentTypes(array $supportedContentTypes): self | ||
{ | ||
$this->supportedContentTypes = $supportedContentTypes; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param bool $jsonEncodedBody | ||
* @return $this | ||
*/ | ||
private function setJsonEncodedBody(bool $jsonEncodedBody): self | ||
{ | ||
$this->jsonEncodedBody = $jsonEncodedBody; | ||
return $this; | ||
} | ||
|
||
public function isSeveralSupported(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function apply(RestRequestOptions $options, ReflectionMethodWrapper $reflectionMethod) | ||
{ | ||
$options->setSupportedContentTypes($this->supportedContentTypes, $this->jsonEncodedBody); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,128 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Paysera\Bundle\ApiBundle\Annotation; | ||
|
||
use Paysera\Bundle\ApiBundle\Entity\PathAttributeResolverOptions; | ||
use Paysera\Bundle\ApiBundle\Entity\RestRequestOptions; | ||
use Paysera\Bundle\ApiBundle\Exception\ConfigurationException; | ||
use Paysera\Bundle\ApiBundle\Service\Annotation\ReflectionMethodWrapper; | ||
use Paysera\Bundle\ApiBundle\Attribute\PathAttribute as PathAttributeAttribute; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"METHOD"}) | ||
*/ | ||
class PathAttribute implements RestAnnotationInterface | ||
class PathAttribute extends PathAttributeAttribute implements RestAnnotationInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $parameterName; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $pathPartName; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $resolverType; | ||
|
||
/** | ||
* @var bool|null | ||
*/ | ||
private $resolutionMandatory; | ||
|
||
public function __construct(array $options) | ||
{ | ||
$this->setParameterName($options['parameterName']); | ||
$this->setPathPartName($options['pathPartName']); | ||
$this->setResolverType($options['resolverType'] ?? null); | ||
$this->setResolutionMandatory($options['resolutionMandatory'] ?? null); | ||
} | ||
|
||
/** | ||
* @param string $parameterName | ||
* @return $this | ||
*/ | ||
private function setParameterName(string $parameterName): self | ||
{ | ||
$this->parameterName = $parameterName; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $pathPartName | ||
* @return $this | ||
*/ | ||
private function setPathPartName(string $pathPartName): self | ||
{ | ||
$this->pathPartName = $pathPartName; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string|null $resolverType | ||
* @return $this | ||
*/ | ||
private function setResolverType($resolverType): self | ||
{ | ||
$this->resolverType = $resolverType; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param bool|null $resolutionMandatory | ||
* @return $this | ||
*/ | ||
private function setResolutionMandatory($resolutionMandatory): self | ||
{ | ||
$this->resolutionMandatory = $resolutionMandatory; | ||
return $this; | ||
} | ||
|
||
public function isSeveralSupported(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function apply(RestRequestOptions $options, ReflectionMethodWrapper $reflectionMethod) | ||
{ | ||
$options->addPathAttributeResolverOptions( | ||
(new PathAttributeResolverOptions()) | ||
->setParameterName($this->parameterName) | ||
->setPathPartName($this->pathPartName) | ||
->setPathAttributeResolverType($this->resolvePathAttributeResolverType($reflectionMethod)) | ||
->setResolutionMandatory($this->resolveIfResolutionIsMandatory($reflectionMethod)) | ||
); | ||
} | ||
|
||
private function resolvePathAttributeResolverType(ReflectionMethodWrapper $reflectionMethod): string | ||
{ | ||
if ($this->resolverType !== null) { | ||
return $this->resolverType; | ||
} | ||
|
||
try { | ||
return $reflectionMethod->getNonBuiltInTypeForParameter($this->parameterName); | ||
} catch (ConfigurationException $exception) { | ||
throw new ConfigurationException(sprintf( | ||
'Denormalization type could not be guessed for %s in %s', | ||
'$' . $this->parameterName, | ||
$reflectionMethod->getFriendlyName() | ||
)); | ||
} | ||
} | ||
|
||
private function resolveIfResolutionIsMandatory(ReflectionMethodWrapper $reflectionMethod): bool | ||
{ | ||
if ($this->resolutionMandatory !== null) { | ||
return $this->resolutionMandatory; | ||
} | ||
|
||
$parameter = $reflectionMethod->getParameterByName($this->parameterName); | ||
|
||
return !$parameter->isDefaultValueAvailable(); | ||
} | ||
} |
Oops, something went wrong.