Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: weierophinney/zf2
...
head fork: weierophinney/zf2
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 14, 2012
@weierophinney [ZF2-299] allow combining callback options and context
- If we have $context, it should appear immediately following the $value
  when passed to the callback.
199b5ba
View
15 library/Zend/Validator/Callback.php
@@ -138,11 +138,20 @@ public function isValid($value, $context = null)
throw new Exception\InvalidArgumentException('No callback given');
}
- $args = func_get_args();
- $options = array_merge($args, $options);
+ $args = array($value);
+ if (empty($options) && !empty($context)) {
+ $args[] = $context;
+ }
+ if (!empty($options) && empty($context)) {
+ $args = array_merge($args, $options);
+ }
+ if (!empty($options) && !empty($context)) {
+ $args[] = $context;
+ $args = array_merge($args, $options);
+ }
try {
- if (!call_user_func_array($callback, $options)) {
+ if (!call_user_func_array($callback, $args)) {
$this->error(self::INVALID_VALUE);
return false;
}
View
22 tests/Zend/Validator/CallbackTest.php
@@ -124,6 +124,28 @@ public function testEqualsMessageVariables()
);
}
+ public function testCanAcceptContextWithoutOptions()
+ {
+ $value = 'bar';
+ $context = array('foo' => 'bar', 'bar' => 'baz');
+ $validator = new Validator\Callback(function($v, $c) use ($value, $context) {
+ return (($value == $v) && ($context == $c));
+ });
+ $this->assertTrue($validator->isValid($value, $context));
+ }
+
+ public function testCanAcceptContextWithOptions()
+ {
+ $value = 'bar';
+ $context = array('foo' => 'bar', 'bar' => 'baz');
+ $options = array('baz' => 'bat');
+ $validator = new Validator\Callback(function($v, $c, $baz) use ($value, $context, $options) {
+ return (($value == $v) && ($context == $c) && ($options['baz'] == $baz));
+ });
+ $validator->setCallbackOptions($options);
+ $this->assertTrue($validator->isValid($value, $context));
+ }
+
public function objectCallback($value)
{
return true;

No commit comments for this range

Something went wrong with that request. Please try again.