Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Dynamic {
- const RE_MethodName = '@[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*@';
- static protected $container = array();
- static public function attach ($name, $method) {
- if(!is_string($name))
- throw new \InvalidArgumentException('$name should be a string.');
- if(!preg_match(self::RE_MethodName, $name))
- throw new \InvalidArgumentException('$name is not a valid method name');
- if(!is_callable($method))
- throw new \InvalidArgumentException('$method should be callable.');
- self::$container[$name] = $method;
- }
- static public function __callStatic ($name, $arguments) {
- return isset(self::$container[$name])
- ? call_user_func_array(self::$container[$name], $arguments)
- : null;
- }
- protected function __construct () {}
- }
- Dynamic::attach('urlsafe', function ($value) {
- $value = strtolower($value);
- $value = preg_replace('@[^a-z0-9_#%:]+@', '-', $value);
- return trim($value, '-');
- });
- echo Dynamic::urlsafe('My precious URL + something else.');
- echo Dynamic::does_not_exist('The ABC.');
- // urlsafe : my-precious-url-something-else
- // does_not_exist :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement