Created
October 6, 2016 02:41
-
-
Save sergiors/e3be1a14334b73eecd35e6763af5a95f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
function partial(callable $fn, ...$args) | |
{ | |
$numReqParams = (new \ReflectionFunction($fn))->getNumberOfRequiredParameters(); | |
return isset($args[$numReqParams - 1]) | |
? $fn(...$args) | |
: function (...$restArgs) use ($fn, $args) { | |
return partial($fn, ...array_merge($args, $restArgs)); | |
}; | |
} | |
function filter(...$args) | |
{ | |
return partial(function (callable $fn, array $xss, $flag = 0) { | |
return array_filter($xss, $fn, $flag); | |
})(...$args); | |
} | |
function typeof (...$args) | |
{ | |
return partial(function ($type, $value) { | |
return $value instanceof $type; | |
})(...$args); | |
} | |
function head(array $xss) | |
{ | |
return [] === $xss | |
? $xss | |
: array_values($xss)[0]; | |
} | |
interface IdInterface | |
{ | |
} | |
interface EmailInterface | |
{ | |
} | |
interface UserInterface | |
{ | |
} | |
interface OrgInterface | |
{ | |
public function getName(): string; | |
} | |
interface ProfileInterface extends UserInterface | |
{ | |
public function getId(): IdInterface; | |
public function getName(): string; | |
public function getEmail(): EmailInterface; | |
} | |
$sergio = new class implements ProfileInterface { | |
public function getId(): IdInterface { | |
return new class implements IdInterface { | |
}; | |
} | |
public function getName(): string { | |
return 'Sérgio Rafael Siqueira'; | |
} | |
public function getEmail(): EmailInterface { | |
return new class implements EmailInterface { | |
}; | |
} | |
}; | |
$tiago = new class implements UserInterface { | |
}; | |
$inbep = new class implements OrgInterface { | |
public function getName(): string { | |
return 'INBEP'; | |
} | |
}; | |
var_dump($users = [$sergio, $tiago, $inbep]); | |
var_dump($justUsers = filter(typeof(UserInterface::class), $users)); | |
var_dump(head(filter(typeof(ProfileInterface::class), $justUsers))->getName()); | |
var_dump(head(filter(typeof(OrgInterface::class), $users))->getName()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment