8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 711788b + ec42844 commit caabd41Copy full SHA for caabd41
src/Symfony/Component/Validator/Constraints/Isbn.php
@@ -12,34 +12,38 @@
12
namespace Symfony\Component\Validator\Constraints;
13
14
use Symfony\Component\Validator\Constraint;
15
-use Symfony\Component\Validator\Exception\MissingOptionsException;
16
17
/**
18
* @Annotation
19
*
20
* @author The Whole Life To Learn <thewholelifetolearn@gmail.com>
+ * @author Manuel Reinhard <manu@sprain.ch>
21
*/
22
class Isbn extends Constraint
23
{
24
public $isbn10Message = 'This value is not a valid ISBN-10.';
25
public $isbn13Message = 'This value is not a valid ISBN-13.';
26
public $bothIsbnMessage = 'This value is neither a valid ISBN-10 nor a valid ISBN-13.';
27
- public $isbn10;
28
- public $isbn13;
+ public $type;
+ public $message;
29
30
- public function __construct($options = null)
31
- {
32
- if (null !== $options && !is_array($options)) {
33
- $options = array(
34
- 'isbn10' => $options,
35
- 'isbn13' => $options,
36
- );
37
- }
+ /**
+ * @deprecated Deprecated since version 2.5, to be removed in 3.0. Use option "type" instead.
+ * @var Boolean
+ */
+ public $isbn10 = false;
38
39
- parent::__construct($options);
40
+ public $isbn13 = false;
41
- if (null === $this->isbn10 && null === $this->isbn13) {
42
- throw new MissingOptionsException(sprintf('Either option "isbn10" or "isbn13" must be given for constraint "%s".', __CLASS__), array('isbn10', 'isbn13'));
43
+ * {@inheritDoc}
44
45
+ public function getDefaultOption()
46
+ {
47
+ return 'type';
48
}
49
src/Symfony/Component/Validator/Constraints/IsbnValidator.php
@@ -16,10 +16,10 @@
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
- * Validates whether the value is a valid ISBN-10 or ISBN-13.
+ * Validates whether the value is a valid ISBN-10 or ISBN-13
- *
* @see https://en.wikipedia.org/wiki/Isbn
class IsbnValidator extends ConstraintValidator
@@ -45,11 +45,43 @@ public function validate($value, Constraint $constraint)
$value = str_replace('-', '', $value);
- $validation = 0;
- $value = strtoupper($value);
+ if (null == $constraint->type) {
+ if ($constraint->isbn10 && !$constraint->isbn13) {
50
+ $constraint->type = 'isbn10';
51
+ $value = strtoupper($value);
52
+ } elseif ($constraint->isbn13 && !$constraint->isbn10) {
53
+ $constraint->type = 'isbn13';
54
55
+ }
56
57
+
58
+ if ('isbn10' === $constraint->type) {
59
+ if (!$this->validateIsbn10($value)) {
60
+ $this->context->addViolation($this->getMessage($constraint, 'isbn10'));
61
62
+ return;
63
64
+ } elseif ('isbn13' === $constraint->type) {
65
+ if (!$this->validateIsbn13($value)) {
66
+ $this->context->addViolation($this->getMessage($constraint, 'isbn13'));
67
68
69
70
+ } else {
71
+ if (!$this->validateIsbn10($value) && !$this->validateIsbn13($value)) {
72
+ $this->context->addViolation($this->getMessage($constraint));
73
74
75
76
77
78
79
+ protected function validateIsbn10($value)
80
81
+ $validation = 0;
82
$valueLength = strlen($value);
83
- if (10 === $valueLength && null !== $constraint->isbn10) {
84
+ if (10 === $valueLength) {
85
for ($i = 0; $i < 10; $i++) {
86
if ($value[$i] == 'X') {
87
$validation += 10 * intval(10 - $i);
@@ -59,13 +91,21 @@ public function validate($value, Constraint $constraint)
91
92
93
if ($validation % 11 != 0) {
- if (null !== $constraint->isbn13) {
- $this->context->addViolation($constraint->bothIsbnMessage);
- } else {
- $this->context->addViolation($constraint->isbn10Message);
- } 9E81
94
+ return false;
95
96
+ return true;
97
- } elseif (13 === $valueLength && null !== $constraint->isbn13) {
98
99
100
101
102
103
+ protected function validateIsbn13($value)
104
105
106
+ $valueLength = strlen($value);
107
108
+ if (13 === $valueLength) {
109
for ($i = 0; $i < 13; $i += 2) {
110
$validation += intval($value[$i]);
111
@@ -74,20 +114,25 @@ public function validate($value, Constraint $constraint)
114
115
116
if ($validation % 10 != 0) {
- if (null !== $constraint->isbn10) {
- $this->context->addViolation($constraint->isbn13Message);
- if (null !== $constraint->isbn10 && null !== $constraint->isbn13) {
- } elseif (null !== $constraint->isbn10) {
117
88
118
} else {
89
119
90
120
121
122
123
124
125
126
+ protected function getMessage($constraint, $type=null)
127
128
+ if (null !== $constraint->message) {
129
+ return $constraint->message;
130
+ } elseif ($type == 'isbn10') {
131
+ return $constraint->isbn10Message;
132
+ } elseif ($type == 'isbn13') {
133
+ return $constraint->isbn13Message;
134
135
+ return $constraint->bothIsbnMessage;
136
137
138
src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php
@@ -44,6 +44,7 @@ public function getValidIsbn10()
array('0321812700'),
array('0-45122-5244'),
array('0-4712-92311'),
+ array('0-9752298-0-X')
);
@@ -58,6 +59,7 @@ public function getInvalidIsbn10()
array('0-4X19-92611'),
array('0_45122_5244'),
array('2870#971#648'),
+ array('0-9752298-0-x')
@@ -145,7 +147,7 @@ public function testExpectsStringCompatibleType()
145
147
146
148
public function testValidIsbn10($isbn)
149
- $constraint = new Isbn(array('isbn10' => true));
150
+ $constraint = new Isbn(array('type' => 'isbn10'));
151
$this->context
152
->expects($this->never())
153
->method('addViolation');
@@ -158,7 +160,7 @@ public function testValidIsbn10($isbn)
158
160
159
161
public function testInvalidIsbn10($isbn)
162
163
164
165
->expects($this->once())
166
->method('addViolation')
@@ -172,7 +174,7 @@ public function testInvalidIsbn10($isbn)
172
174
173
175
public function testValidIsbn13($isbn)
176
- $constraint = new Isbn(array('isbn13' => true));
177
+ $constraint = new Isbn(array('type' => 'isbn13'));
178
179
180
@@ -185,7 +187,7 @@ public function testValidIsbn13($isbn)
185
187
186
188
public function testInvalidIsbn13($isbn)
189
190
191
192
193
@@ -199,7 +201,7 @@ public function testInvalidIsbn13($isbn)
199
201
200
202
public function testValidIsbn($isbn)
203
- $constraint = new Isbn(array('isbn10' => true, 'isbn13' => true));
204
+ $constraint = new Isbn();
205
206
207
@@ -212,7 +214,7 @@ public function testValidIsbn($isbn)
212
214
213
215
public function testInvalidIsbn($isbn)
216
217
218
219
220