Skip to content

Commit 1d7c1c7

Browse files
committed
Logical XOr
1 parent 50571fc commit 1d7c1c7

File tree

9 files changed

+379
-2
lines changed

9 files changed

+379
-2
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Symfony Validator
4+
*
5+
* Type sensitive validating Software for Symfony Applications.
6+
*
7+
* Copyright (c) 2020 Fabian Fröhlich <[email protected]> https://f-froehlich.de
8+
*
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*
23+
* For all license terms see README.md and LICENSE Files in root directory of this Project.
24+
*
25+
*/
26+
27+
namespace FabianFroehlich\Validator\Tests\Constraints\Unit;
28+
29+
30+
use FabianFroehlich\Validator\Constraints\IsBool;
31+
use FabianFroehlich\Validator\Constraints\LogicalXOr;
32+
use FabianFroehlich\Validator\Tests\AbstractLogicalConstraintTestCase;
33+
use FabianFroehlich\Validator\Validator\LogicalXOrValidator;
34+
35+
/**
36+
* Class LogicalXOrTest
37+
*
38+
* @author Fabian Fröhlich <[email protected]>
39+
* @package FabianFroehlich\Validator\Tests\Constraints\Unit
40+
*/
41+
class LogicalXOrTest
42+
extends AbstractLogicalConstraintTestCase {
43+
44+
/**
45+
* {@inheritDoc}
46+
*/
47+
public function getRequiredValidatorClass(): string {
48+
49+
return LogicalXOrValidator::class;
50+
}
51+
52+
53+
/**
54+
* {@inheritDoc}
55+
*/
56+
protected function initConstraint(): void {
57+
58+
$constraintClass = $this->getConstraintClass();
59+
$this->constraint = new $constraintClass(['left' => new IsBool(), 'right' => new IsBool()]);
60+
}
61+
62+
/**
63+
* {@inheritDoc}
64+
*/
65+
public function getConstraintClass(): string {
66+
67+
return LogicalXOr::class;
68+
}
69+
70+
/**
71+
* @inheritDoc
72+
*/
73+
protected function getTranslationDomain(): string {
74+
return 'LogicalXOr';
75+
}
76+
77+
78+
}

Tests/Enums/Unit/ErrorCodesTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ protected function getEnumKeys(): array {
7676
'UUID_GROUP_COUNT_INVALID',
7777
'UUID_INVALID_HYPHEN_POS',
7878
'UUID_INVALID_LENGTH',
79+
'LOGICAL_XOR_INVALID',
7980
];
8081
}
8182

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* Symfony Validator
4+
*
5+
* Type sensitive validating Software for Symfony Applications.
6+
*
7+
* Copyright (c) 2020 Fabian Fröhlich <[email protected]> https://f-froehlich.de
8+
*
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*
23+
* For all license terms see README.md and LICENSE Files in root directory of this Project.
24+
*
25+
*/
26+
27+
namespace FabianFroehlich\Validator\Tests\Validator\Integration;
28+
29+
30+
use FabianFroehlich\Validator\Constraints\AbstractConstraint;
31+
use FabianFroehlich\Validator\Constraints\IsNotNull;
32+
use FabianFroehlich\Validator\Constraints\IsNull;
33+
use FabianFroehlich\Validator\Constraints\LogicalXOr;
34+
use FabianFroehlich\Validator\Enums\ErrorCodes;
35+
use FabianFroehlich\Validator\Tests\ValidatorTestCase;
36+
use FabianFroehlich\Validator\Validator\AbstractConstraintValidator;
37+
38+
/**
39+
* Class LogicalXOrValidatorTest
40+
*
41+
* @author Fabian Fröhlich <[email protected]>
42+
* @package FabianFroehlich\Validator\Tests\Validator\Integration
43+
*/
44+
class LogicalXOrValidatorTest
45+
extends ValidatorTestCase {
46+
47+
/**
48+
* @test
49+
*/
50+
public function checkBothValuesValid(): void {
51+
$constraint = new LogicalXOr(['left' => new IsNull(), 'right' => new IsNull()]);
52+
53+
$this->assertFalse($this->validator->validate(null, $constraint));
54+
55+
$this->assertCount(1, $this->validatorBuilder->getViolations());
56+
$violation = $this->validatorBuilder->getViolations()[0];
57+
$this->assertEquals(ErrorCodes::LOGICAL_XOR_INVALID()->value(), $violation->getCode());
58+
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function passIfLeftValueIsValid(): void {
65+
$constraint = new LogicalXOr(['left' => new IsNull(), 'right' => new IsNotNull()]);
66+
67+
$this->assertTrue($this->validator->validate(null, $constraint));
68+
}
69+
70+
/**
71+
* @test
72+
*/
73+
public function passIfRightValueIsPassed(): void {
74+
$constraint = new LogicalXOr(['left' => new IsNotNull(), 'right' => new IsNull()]);
75+
76+
$this->assertTrue($this->validator->validate(null, $constraint));
77+
}
78+
79+
/**
80+
* @test
81+
*/
82+
public function failIfBothValueIsInvalid(): void {
83+
$constraint = new LogicalXOr(['left' => new IsNotNull(), 'right' => new IsNotNull()]);
84+
85+
$this->assertFalse($this->validator->validate(null, $constraint));
86+
87+
$this->assertCount(3, $this->validatorBuilder->getViolations());
88+
$violation = $this->validatorBuilder->getViolations()[2];
89+
$this->assertEquals(ErrorCodes::LOGICAL_XOR_INVALID()->value(), $violation->getCode());
90+
}
91+
92+
protected function getRequiredType(): string {
93+
return AbstractConstraintValidator::ALL;
94+
}
95+
96+
/**
97+
* Get the types that are valid for the Validator
98+
*
99+
* @return array
100+
*/
101+
protected function getValidTypes(): array {
102+
103+
return $this->getAllTypes();
104+
}
105+
106+
/**
107+
* {@inheritDoc}
108+
*/
109+
protected function getConstraint(): AbstractConstraint {
110+
111+
$class = $this->getConstraintClass();
112+
113+
return new $class(['left' => new IsNotNull(), 'right' => new IsNotNull()]);
114+
}
115+
116+
/**
117+
* {@inheritDoc}
118+
*/
119+
protected function getConstraintClass(): string {
120+
121+
return LogicalXOr::class;
122+
}
123+
124+
}

src/Constraints/LogicalOr.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
use FabianFroehlich\Validator\Validator\LogicalOrValidator;
3232

3333
/**
34-
* Class LogicalAnd
34+
* Class LogicalOr
3535
*
3636
* @Annotation
3737
* @Target({"PROPERTY", "ANNOTATION"})

src/Constraints/LogicalXOr.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* Symfony Validator
5+
*
6+
* Type sensitive validating Software for Symfony Applications.
7+
*
8+
* Copyright (c) 2020 Fabian Fröhlich <[email protected]> https://f-froehlich.de
9+
*
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
23+
*
24+
* For all license terms see README.md and LICENSE Files in root directory of this Project.
25+
*
26+
*/
27+
28+
namespace FabianFroehlich\Validator\Constraints;
29+
30+
31+
use FabianFroehlich\Validator\Validator\LogicalXOrValidator;
32+
33+
/**
34+
* Class LogicalXOr
35+
*
36+
* @Annotation
37+
* @Target({"PROPERTY", "ANNOTATION"})
38+
*
39+
* @author Fabian Fröhlich <[email protected]>
40+
* @package FabianFroehlich\Validator\Constraints
41+
*/
42+
class LogicalXOr
43+
extends AbstractLogicalConstraint {
44+
45+
46+
/**
47+
* {@inheritDoc}
48+
*/
49+
public function validatedBy(): string {
50+
51+
return LogicalXOrValidator::class;
52+
}
53+
54+
/**
55+
* @inheritDoc
56+
*/
57+
public function getTranslationDomain(): string {
58+
return 'LogicalXOr';
59+
}
60+
61+
}

src/Enums/ErrorCodes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@
7171
* @method static ErrorCodes UUID_GROUP_COUNT_INVALID()
7272
* @method static ErrorCodes UUID_INVALID_HYPHEN_POS()
7373
* @method static ErrorCodes UUID_INVALID_LENGTH()
74+
* @method static ErrorCodes LOGICAL_XOR_INVALID()
7475
*/
7576
class ErrorCodes
7677
extends AbstractEnum {
7778

79+
/** @const string */
80+
private const LOGICAL_XOR_INVALID = 'LOGICAL_XOR_INVALID';
81+
7882
/** @const string */
7983
private const UUID_INVALID_VERSION = 'UUID_INVALID_VERSION';
8084

src/Resources/config/validators.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,9 @@
170170
<argument type="service" id="FabianFroehlich\Validator\Violation\DataConstraintViolationBuilder"/>
171171
</service>
172172

173+
<service id="FabianFroehlich\Validator\Validator\LogicalXOrValidator">
174+
<argument type="service" id="FabianFroehlich\Validator\Violation\DataConstraintViolationBuilder"/>
175+
</service>
176+
173177
</services>
174178
</container>

src/Validator/LogicalOrValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
use FabianFroehlich\Validator\Constraints\LogicalOr;
3232

3333
/**
34-
* Class LogicalAndValidator
34+
* Class LogicalOrValidator
3535
*
3636
* @author Fabian Fröhlich <[email protected]>
3737
* @package FabianFroehlich\Validator\Validator

0 commit comments

Comments
 (0)