2018-02-05 18:39:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the NelmioApiDocBundle package.
|
|
|
|
*
|
|
|
|
* (c) Nelmio
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;
|
|
|
|
|
2021-06-16 10:59:06 +03:00
|
|
|
use Nelmio\ApiDocBundle\Tests\ModelDescriber\Annotations\Fixture as CustomAssert;
|
2018-02-05 18:39:58 +01:00
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
|
|
|
|
|
|
|
class SymfonyConstraints
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @Assert\NotBlank()
|
|
|
|
*/
|
|
|
|
private $propertyNotBlank;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @Assert\NotNull()
|
|
|
|
*/
|
|
|
|
private $propertyNotNull;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @Assert\Length(min="0", max="50")
|
|
|
|
*/
|
2018-08-30 00:32:11 +02:00
|
|
|
private $propertyAssertLength;
|
2018-02-05 18:39:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @Assert\Regex(pattern="/[a-z]{2}/")
|
|
|
|
*/
|
|
|
|
private $propertyRegex;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @Assert\Count(min="0", max="10")
|
|
|
|
*/
|
|
|
|
private $propertyCount;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @Assert\Choice(choices={"choice1", "choice2"})
|
|
|
|
*/
|
|
|
|
private $propertyChoice;
|
|
|
|
|
2018-05-20 15:59:52 +02:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @Assert\Choice(callback={SymfonyConstraints::class,"fetchAllowedChoices"})
|
|
|
|
*/
|
|
|
|
private $propertyChoiceWithCallback;
|
|
|
|
|
2018-09-26 16:51:43 +02:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @Assert\Choice(callback="fetchAllowedChoices")
|
|
|
|
*/
|
|
|
|
private $propertyChoiceWithCallbackWithoutClass;
|
|
|
|
|
Apply `enum` from Choice Constraints to Items When Choice is Multiple (#1784)
* Apply `enum` from Choice Constraints to Items When Choice is Multiple
Otherwise JSON schema like this is generated:
```
"property": {
"type": "array",
"enum": ["one", "two", "three"],
"items": {
"type": "string"
}
}
```
With this change, however, this schema is generated:
```
"property": {
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
```
A possible downside here is that the symfony constraint stuff happens
before types are figured out from PHPDoc. So it's _possible_ to end up
with something that won't validated. Take something like this:
```
/**
* @Assert\Choice(multiple=true, choices={"..."})
* @var string
*/
```
This would generate:
```
"property": {
"type": "string",
"items": {
"enum": ["..."]
}
}
```
* Fix CS
* cs
* more cs
* fix tests
Co-authored-by: Guilhem Niot <guilhem@gniot.fr>
2021-02-19 02:41:32 -06:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*
|
|
|
|
* @Assert\Choice(multiple=true, choices={"choice1", "choice2"})
|
|
|
|
*/
|
|
|
|
private $propertyChoiceWithMultiple;
|
|
|
|
|
2018-02-05 18:39:58 +01:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @Assert\Expression(
|
|
|
|
* "this.getCategory() in ['php', 'symfony'] or !this.isTechnicalPost()",
|
|
|
|
* message="If this is a tech post, the category should be either php or symfony!"
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
private $propertyExpression;
|
|
|
|
|
2018-12-19 15:41:27 +01:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @Assert\Range(min=1, max=5)
|
|
|
|
*/
|
|
|
|
private $propertyRange;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @Assert\LessThan(42)
|
|
|
|
*/
|
|
|
|
private $propertyLessThan;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @Assert\LessThanOrEqual(23)
|
|
|
|
*/
|
|
|
|
private $propertyLessThanOrEqual;
|
|
|
|
|
2021-06-16 10:59:06 +03:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @CustomAssert\CompoundValidationRule()
|
|
|
|
*/
|
|
|
|
private $propertyWithCompoundValidationRule;
|
|
|
|
|
|
|
|
public function setPropertyWithCompoundValidationRule(int $propertyWithCompoundValidationRule): void
|
|
|
|
{
|
|
|
|
$this->propertyWithCompoundValidationRule = $propertyWithCompoundValidationRule;
|
|
|
|
}
|
|
|
|
|
2018-02-05 18:39:58 +01:00
|
|
|
/**
|
2020-07-12 14:54:39 +02:00
|
|
|
* @Assert\Count(min="0", max="10")
|
2018-02-05 18:39:58 +01:00
|
|
|
*/
|
|
|
|
public function setPropertyNotBlank(int $propertyNotBlank): void
|
|
|
|
{
|
|
|
|
$this->propertyNotBlank = $propertyNotBlank;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPropertyNotNull(int $propertyNotNull): void
|
|
|
|
{
|
|
|
|
$this->propertyNotNull = $propertyNotNull;
|
|
|
|
}
|
|
|
|
|
2018-08-30 00:32:11 +02:00
|
|
|
public function setPropertyAssertLength(int $propertyAssertLength): void
|
2018-02-05 18:39:58 +01:00
|
|
|
{
|
2018-08-30 00:32:11 +02:00
|
|
|
$this->propertyAssertLength = $propertyAssertLength;
|
2018-02-05 18:39:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setPropertyRegex(int $propertyRegex): void
|
|
|
|
{
|
|
|
|
$this->propertyRegex = $propertyRegex;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPropertyCount(int $propertyCount): void
|
|
|
|
{
|
|
|
|
$this->propertyCount = $propertyCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPropertyChoice(int $propertyChoice): void
|
|
|
|
{
|
|
|
|
$this->propertyChoice = $propertyChoice;
|
|
|
|
}
|
|
|
|
|
2018-05-20 15:59:52 +02:00
|
|
|
public function setPropertyChoiceWithCallback(int $propertyChoiceWithCallback): void
|
|
|
|
{
|
|
|
|
$this->propertyChoiceWithCallback = $propertyChoiceWithCallback;
|
|
|
|
}
|
|
|
|
|
2018-09-26 16:51:43 +02:00
|
|
|
public function setPropertyChoiceWithCallbackWithoutClass(int $propertyChoiceWithCallbackWithoutClass): void
|
|
|
|
{
|
|
|
|
$this->propertyChoiceWithCallbackWithoutClass = $propertyChoiceWithCallbackWithoutClass;
|
|
|
|
}
|
|
|
|
|
Apply `enum` from Choice Constraints to Items When Choice is Multiple (#1784)
* Apply `enum` from Choice Constraints to Items When Choice is Multiple
Otherwise JSON schema like this is generated:
```
"property": {
"type": "array",
"enum": ["one", "two", "three"],
"items": {
"type": "string"
}
}
```
With this change, however, this schema is generated:
```
"property": {
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
```
A possible downside here is that the symfony constraint stuff happens
before types are figured out from PHPDoc. So it's _possible_ to end up
with something that won't validated. Take something like this:
```
/**
* @Assert\Choice(multiple=true, choices={"..."})
* @var string
*/
```
This would generate:
```
"property": {
"type": "string",
"items": {
"enum": ["..."]
}
}
```
* Fix CS
* cs
* more cs
* fix tests
Co-authored-by: Guilhem Niot <guilhem@gniot.fr>
2021-02-19 02:41:32 -06:00
|
|
|
public function setPropertyChoiceWithMultiple(array $propertyChoiceWithMultiple): void
|
|
|
|
{
|
|
|
|
$this->propertyChoiceWithMultiple = $propertyChoiceWithMultiple;
|
|
|
|
}
|
|
|
|
|
2018-02-05 18:39:58 +01:00
|
|
|
public function setPropertyExpression(int $propertyExpression): void
|
|
|
|
{
|
|
|
|
$this->propertyExpression = $propertyExpression;
|
|
|
|
}
|
2018-05-20 15:59:52 +02:00
|
|
|
|
2018-12-19 15:41:27 +01:00
|
|
|
public function setPropertyRange(int $propertyRange): void
|
|
|
|
{
|
|
|
|
$this->propertyRange = $propertyRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPropertyLessThan(int $propertyLessThan): void
|
|
|
|
{
|
|
|
|
$this->propertyLessThan = $propertyLessThan;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPropertyLessThanOrEqual(int $propertyLessThanOrEqual): void
|
|
|
|
{
|
|
|
|
$this->propertyLessThanOrEqual = $propertyLessThanOrEqual;
|
|
|
|
}
|
|
|
|
|
2018-05-20 15:59:52 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function fetchAllowedChoices()
|
|
|
|
{
|
|
|
|
return ['choice1', 'choice2'];
|
|
|
|
}
|
2018-02-05 18:39:58 +01:00
|
|
|
}
|