PartialApplicationHelper.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /**
  3. * Helper class to wrap an object with a modified interface created by
  4. * a partial application of its existing methods.
  5. *
  6. * @category Services
  7. * @package Services_Twilio
  8. * @author Neuman Vong <neuman@twilio.com>
  9. * @license http://creativecommons.org/licenses/MIT/ MIT
  10. * @link http://pear.php.net/package/Services_Twilio
  11. */
  12. class Services_Twilio_PartialApplicationHelper
  13. {
  14. private $callbacks;
  15. public function __construct()
  16. {
  17. $this->callbacks = array();
  18. }
  19. public function set($method, $callback, array $args)
  20. {
  21. if (!is_callable($callback)) {
  22. return FALSE;
  23. }
  24. $this->callbacks[$method] = array($callback, $args);
  25. }
  26. public function __call($method, $args)
  27. {
  28. if (!isset($this->callbacks[$method])) {
  29. throw new Exception("Method not found: $method");
  30. }
  31. list($callback, $cb_args) = $this->callbacks[$method];
  32. return call_user_func_array(
  33. $callback,
  34. array_merge($cb_args, $args)
  35. );
  36. }
  37. }