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: lstrojny/functional-php
base: f9679f657d
...
head fork: lstrojny/functional-php
compare: a4526d8afc
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Apr 12, 2012
@lstrojny __invoke() handling a4526d8
Showing with 60 additions and 2 deletions.
  1. +10 −0 src/Functional/Access.php
  2. +50 −2 tests/unit/Functional/AccessTest.php
View
10 src/Functional/Access.php
@@ -102,4 +102,14 @@ public function __get($property)
return new static($this->object->{$property});
}
+
+ public function __invoke()
+ {
+ if (!$this->reflected->hasMethod('__invoke')) {
+ throw new Exceptions\AccessException('Invalid functor access');
+ }
+
+ $callable = $this->object;
+ return new static(call_user_func_array($callable, func_get_args()));
+ }
}
View
52 tests/unit/Functional/AccessTest.php
@@ -37,9 +37,39 @@ public function testTryCallingMethods()
$this->assertNull(access(new TestClass(), function($object) {
return $object->undefinedMethod();
}));
- $this->assertSame('virtualMethod', access(new TestClassWithCall(), function($object) {
+ $this->assertNull(access(new TestClass(), function($object) {
+ return $object->undefinedMethod()->undefinedMethod();
+ }));
+ $this->assertSame('virtualMethod:', access(new TestClassWithCall(), function($object) {
return $object->virtualMethod();
}));
+ $this->assertSame('virtualMethod:foo/bar', access(new TestClassWithCall(), function($object) {
+ return $object->virtualMethod('foo', 'bar');
+ }));
+ }
+
+ public function testTryInvoke()
+ {
+ $this->assertSame('INVOKED:', access(new TestClass(), function($object) {
+ $callable = $object->getInvocable();
+ return $callable();
+ }));
+ $this->assertSame('INVOKED:foo/bar', access(new TestClass(), function($object) {
+ $callable = $object->getInvocable();
+ return $callable('foo', 'bar');
+ }));
+ $this->assertSame('CLOSURE:', access(new TestClass(), function($object) {
+ $callable = $object->getClosure();
+ return $callable();
+ }));
+ $this->assertSame('CLOSURE:foo/bar', access(new TestClass(), function($object) {
+ $callable = $object->getClosure();
+ return $callable('foo', 'bar');
+ }));
+ $this->assertNull(access(new TestClass(), function($object) {
+ $callable = $object->getThis();
+ return $callable();
+ }));
}
public function testTryAccessingProperties()
@@ -133,6 +163,24 @@ public function getThis()
{
return $this;
}
+
+ public function getInvocable()
+ {
+ return new TestInvocable();
+ }
+
+ public function getClosure()
+ {
+ return function() { return 'CLOSURE:' . join(func_get_args(), '/'); };
+ }
+}
+
+class TestInvocable
+{
+ public function __invoke()
+ {
+ return 'INVOKED:' . join(func_get_args(), '/');
+ }
}
class TestClassWithGet extends TestClass
@@ -155,7 +203,7 @@ class TestClassWithCall extends TestClass
{
public function __call($method, $arguments)
{
- return $method;
+ return $method . ':' . join($arguments, '/');
}
}

No commit comments for this range

Something went wrong with that request. Please try again.