Permalink
Browse files

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

  • Loading branch information...
1 parent f333cf6 commit 02e02b71490b34bb07311b330c8738c0fd0f6a50 @webmozart webmozart committed May 21, 2012
Showing with 83 additions and 14 deletions.
  1. +14 −2 ConstraintViolation.php
  2. +57 −6 ConstraintViolationList.php
  3. +12 −6 ExecutionContext.php
View
16 ConstraintViolation.php
@@ -24,15 +24,17 @@ class ConstraintViolation
protected $root;
protected $propertyPath;
protected $invalidValue;
+ protected $code;
- public function __construct($messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue, $messagePluralization = null)
+ public function __construct($messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue, $messagePluralization = null, $code = null)
{
$this->messageTemplate = $messageTemplate;
$this->messageParameters = $messageParameters;
$this->messagePluralization = $messagePluralization;
$this->root = $root;
$this->propertyPath = $propertyPath;
$this->invalidValue = $invalidValue;
+ $this->code = $code;
}
/**
@@ -42,12 +44,17 @@ public function __toString()
{
$class = (string) (is_object($this->root) ? get_class($this->root) : $this->root);
$propertyPath = (string) $this->propertyPath;
+ $code = $this->code;
if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
$class .= '.';
}
- return $class . $propertyPath . ":\n " . $this->getMessage();
+ if (!empty($code)) {
+ $code = ' (code ' . $code . ')';
+ }
+
+ return $class . $propertyPath . ":\n " . $this->getMessage() . $code;
}
/**
@@ -112,4 +119,9 @@ public function getInvalidValue()
{
return $this->invalidValue;
}
+
+ public function getCode()
+ {
+ return $this->code;
+ }
}
View
63 ConstraintViolationList.php
@@ -80,6 +80,57 @@ public function addAll(ConstraintViolationList $otherList)
}
/**
+ * Returns the violation at a given offset.
+ *
+ * @param integer $offset The offset of the violation.
+ *
+ * @return ConstraintViolation The violation.
+ *
+ * @throws \OutOfBoundsException If the offset does not exist.
+ */
+ public function get($offset)
+ {
+ if (!isset($this->violations[$offset])) {
+ throw new \OutOfBoundsException(sprintf('The offset "%s" does not exist.', $offset));
+ }
+
+ return $this->violations[$offset];
+ }
+
+ /**
+ * Returns whether the given offset exists.
+ *
+ * @param integer $offset The violation offset.
+ *
+ * @return Boolean Whether the offset exists.
+ */
+ public function has($offset)
+ {
+ return isset($this->violations[$offset]);
+ }
+
+ /**
+ * Sets a violation at a given offset.
+ *
+ * @param integer $offset The violation offset.
+ * @param ConstraintViolation $violation The violation.
+ */
+ public function set($offset, ConstraintViolation $violation)
+ {
+ $this->violations[$offset] = $violation;
+ }
+
+ /**
+ * Removes a violation at a given offset.
+ *
+ * @param integer $offset The offset to remove.
+ */
+ public function remove($offset)
+ {
+ unset($this->violations[$offset]);
+ }
+
+ /**
* @see IteratorAggregate
*
* @api
@@ -106,7 +157,7 @@ public function count()
*/
public function offsetExists($offset)
{
- return isset($this->violations[$offset]);
+ return $this->has($offset);
}
/**
@@ -116,20 +167,20 @@ public function offsetExists($offset)
*/
public function offsetGet($offset)
{
- return isset($this->violations[$offset]) ? $this->violations[$offset] : null;
+ return $this->get($offset);
}
/**
* @see ArrayAccess
*
* @api
*/
- public function offsetSet($offset, $value)
+ public function offsetSet($offset, $violation)
{
if (null === $offset) {
- $this->violations[] = $value;
+ $this->add($violation);
} else {
- $this->violations[$offset] = $value;
+ $this->set($offset, $violation);
}
}
@@ -140,7 +191,7 @@ public function offsetSet($offset, $value)
*/
public function offsetUnset($offset)
{
- unset($this->violations[$offset]);
+ $this->remove($offset);
}
}
View
18 ExecutionContext.php
@@ -57,10 +57,11 @@ public function __clone()
* @param array $params The parameters parsed into the error message.
* @param mixed $invalidValue The invalid, validated value.
* @param integer|null $pluralization The number to use to pluralize of the message.
+ * @param integer|null $code The violation code.
*
* @api
*/
- public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null)
+ public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null)
{
$this->globalContext->addViolation(new ConstraintViolation(
$message,
@@ -69,7 +70,8 @@ public function addViolation($message, array $params = array(), $invalidValue =
$this->propertyPath,
// check using func_num_args() to allow passing null values
func_num_args() >= 3 ? $invalidValue : $this->value,
- $pluralization
+ $pluralization,
+ $code
));
}
@@ -82,8 +84,9 @@ public function addViolation($message, array $params = array(), $invalidValue =
* @param array $params The parameters parsed into the error message.
* @param mixed $invalidValue The invalid, validated value.
* @param integer|null $pluralization The number to use to pluralize of the message.
+ * @param integer|null $code The violation code.
*/
- public function addViolationAtPath($propertyPath, $message, array $params = array(), $invalidValue = null, $pluralization = null)
+ public function addViolationAtPath($propertyPath, $message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null)
{
$this->globalContext->addViolation(new ConstraintViolation(
$message,
@@ -92,7 +95,8 @@ public function addViolationAtPath($propertyPath, $message, array $params = arra
$propertyPath,
// check using func_num_args() to allow passing null values
func_num_args() >= 4 ? $invalidValue : $this->value,
- $pluralization
+ $pluralization,
+ $code
));
}
@@ -105,8 +109,9 @@ public function addViolationAtPath($propertyPath, $message, array $params = arra
* @param array $params The parameters parsed into the error message.
* @param mixed $invalidValue The invalid, validated value.
* @param integer|null $pluralization The number to use to pluralize of the message.
+ * @param integer|null $code The violation code.
*/
- public function addViolationAtSubPath($subPath, $message, array $params = array(), $invalidValue = null, $pluralization = null)
+ public function addViolationAtSubPath($subPath, $message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null)
{
$this->globalContext->addViolation(new ConstraintViolation(
$message,
@@ -115,7 +120,8 @@ public function addViolationAtSubPath($subPath, $message, array $params = array(
$this->getPropertyPath($subPath),
// check using func_num_args() to allow passing null values
func_num_args() >= 4 ? $invalidValue : $this->value,
- $pluralization
+ $pluralization,
+ $code
));
}

0 comments on commit 02e02b7

Please sign in to comment.