Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[Form] Allowed native framework errors to be mapped as well

  • Loading branch information...
commit 02e02b71490b34bb07311b330c8738c0fd0f6a50 1 parent f333cf6
Bernhard Schussek webmozart authored
16 ConstraintViolation.php
@@ -24,8 +24,9 @@ class ConstraintViolation
24 24 protected $root;
25 25 protected $propertyPath;
26 26 protected $invalidValue;
  27 + protected $code;
27 28
28   - public function __construct($messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue, $messagePluralization = null)
  29 + public function __construct($messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue, $messagePluralization = null, $code = null)
29 30 {
30 31 $this->messageTemplate = $messageTemplate;
31 32 $this->messageParameters = $messageParameters;
@@ -33,6 +34,7 @@ public function __construct($messageTemplate, array $messageParameters, $root, $
33 34 $this->root = $root;
34 35 $this->propertyPath = $propertyPath;
35 36 $this->invalidValue = $invalidValue;
  37 + $this->code = $code;
36 38 }
37 39
38 40 /**
@@ -42,12 +44,17 @@ public function __toString()
42 44 {
43 45 $class = (string) (is_object($this->root) ? get_class($this->root) : $this->root);
44 46 $propertyPath = (string) $this->propertyPath;
  47 + $code = $this->code;
45 48
46 49 if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
47 50 $class .= '.';
48 51 }
49 52
50   - return $class . $propertyPath . ":\n " . $this->getMessage();
  53 + if (!empty($code)) {
  54 + $code = ' (code ' . $code . ')';
  55 + }
  56 +
  57 + return $class . $propertyPath . ":\n " . $this->getMessage() . $code;
51 58 }
52 59
53 60 /**
@@ -112,4 +119,9 @@ public function getInvalidValue()
112 119 {
113 120 return $this->invalidValue;
114 121 }
  122 +
  123 + public function getCode()
  124 + {
  125 + return $this->code;
  126 + }
115 127 }
63 ConstraintViolationList.php
@@ -80,6 +80,57 @@ public function addAll(ConstraintViolationList $otherList)
80 80 }
81 81
82 82 /**
  83 + * Returns the violation at a given offset.
  84 + *
  85 + * @param integer $offset The offset of the violation.
  86 + *
  87 + * @return ConstraintViolation The violation.
  88 + *
  89 + * @throws \OutOfBoundsException If the offset does not exist.
  90 + */
  91 + public function get($offset)
  92 + {
  93 + if (!isset($this->violations[$offset])) {
  94 + throw new \OutOfBoundsException(sprintf('The offset "%s" does not exist.', $offset));
  95 + }
  96 +
  97 + return $this->violations[$offset];
  98 + }
  99 +
  100 + /**
  101 + * Returns whether the given offset exists.
  102 + *
  103 + * @param integer $offset The violation offset.
  104 + *
  105 + * @return Boolean Whether the offset exists.
  106 + */
  107 + public function has($offset)
  108 + {
  109 + return isset($this->violations[$offset]);
  110 + }
  111 +
  112 + /**
  113 + * Sets a violation at a given offset.
  114 + *
  115 + * @param integer $offset The violation offset.
  116 + * @param ConstraintViolation $violation The violation.
  117 + */
  118 + public function set($offset, ConstraintViolation $violation)
  119 + {
  120 + $this->violations[$offset] = $violation;
  121 + }
  122 +
  123 + /**
  124 + * Removes a violation at a given offset.
  125 + *
  126 + * @param integer $offset The offset to remove.
  127 + */
  128 + public function remove($offset)
  129 + {
  130 + unset($this->violations[$offset]);
  131 + }
  132 +
  133 + /**
83 134 * @see IteratorAggregate
84 135 *
85 136 * @api
@@ -106,7 +157,7 @@ public function count()
106 157 */
107 158 public function offsetExists($offset)
108 159 {
109   - return isset($this->violations[$offset]);
  160 + return $this->has($offset);
110 161 }
111 162
112 163 /**
@@ -116,7 +167,7 @@ public function offsetExists($offset)
116 167 */
117 168 public function offsetGet($offset)
118 169 {
119   - return isset($this->violations[$offset]) ? $this->violations[$offset] : null;
  170 + return $this->get($offset);
120 171 }
121 172
122 173 /**
@@ -124,12 +175,12 @@ public function offsetGet($offset)
124 175 *
125 176 * @api
126 177 */
127   - public function offsetSet($offset, $value)
  178 + public function offsetSet($offset, $violation)
128 179 {
129 180 if (null === $offset) {
130   - $this->violations[] = $value;
  181 + $this->add($violation);
131 182 } else {
132   - $this->violations[$offset] = $value;
  183 + $this->set($offset, $violation);
133 184 }
134 185 }
135 186
@@ -140,7 +191,7 @@ public function offsetSet($offset, $value)
140 191 */
141 192 public function offsetUnset($offset)
142 193 {
143   - unset($this->violations[$offset]);
  194 + $this->remove($offset);
144 195 }
145 196
146 197 }
18 ExecutionContext.php
@@ -57,10 +57,11 @@ public function __clone()
57 57 * @param array $params The parameters parsed into the error message.
58 58 * @param mixed $invalidValue The invalid, validated value.
59 59 * @param integer|null $pluralization The number to use to pluralize of the message.
  60 + * @param integer|null $code The violation code.
60 61 *
61 62 * @api
62 63 */
63   - public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null)
  64 + public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null)
64 65 {
65 66 $this->globalContext->addViolation(new ConstraintViolation(
66 67 $message,
@@ -69,7 +70,8 @@ public function addViolation($message, array $params = array(), $invalidValue =
69 70 $this->propertyPath,
70 71 // check using func_num_args() to allow passing null values
71 72 func_num_args() >= 3 ? $invalidValue : $this->value,
72   - $pluralization
  73 + $pluralization,
  74 + $code
73 75 ));
74 76 }
75 77
@@ -82,8 +84,9 @@ public function addViolation($message, array $params = array(), $invalidValue =
82 84 * @param array $params The parameters parsed into the error message.
83 85 * @param mixed $invalidValue The invalid, validated value.
84 86 * @param integer|null $pluralization The number to use to pluralize of the message.
  87 + * @param integer|null $code The violation code.
85 88 */
86   - public function addViolationAtPath($propertyPath, $message, array $params = array(), $invalidValue = null, $pluralization = null)
  89 + public function addViolationAtPath($propertyPath, $message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null)
87 90 {
88 91 $this->globalContext->addViolation(new ConstraintViolation(
89 92 $message,
@@ -92,7 +95,8 @@ public function addViolationAtPath($propertyPath, $message, array $params = arra
92 95 $propertyPath,
93 96 // check using func_num_args() to allow passing null values
94 97 func_num_args() >= 4 ? $invalidValue : $this->value,
95   - $pluralization
  98 + $pluralization,
  99 + $code
96 100 ));
97 101 }
98 102
@@ -105,8 +109,9 @@ public function addViolationAtPath($propertyPath, $message, array $params = arra
105 109 * @param array $params The parameters parsed into the error message.
106 110 * @param mixed $invalidValue The invalid, validated value.
107 111 * @param integer|null $pluralization The number to use to pluralize of the message.
  112 + * @param integer|null $code The violation code.
108 113 */
109   - public function addViolationAtSubPath($subPath, $message, array $params = array(), $invalidValue = null, $pluralization = null)
  114 + public function addViolationAtSubPath($subPath, $message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null)
110 115 {
111 116 $this->globalContext->addViolation(new ConstraintViolation(
112 117 $message,
@@ -115,7 +120,8 @@ public function addViolationAtSubPath($subPath, $message, array $params = array(
115 120 $this->getPropertyPath($subPath),
116 121 // check using func_num_args() to allow passing null values
117 122 func_num_args() >= 4 ? $invalidValue : $this->value,
118   - $pluralization
  123 + $pluralization,
  124 + $code
119 125 ));
120 126 }
121 127

0 comments on commit 02e02b7

Please sign in to comment.
Something went wrong with that request. Please try again.