MethodMatcher.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * Identify a static/dynamic method callable, even if contains an object to which you don't have a reference.
  5. *
  6. * @access private
  7. * @since 1.11.0
  8. */
  9. class MethodMatcher {
  10. /**
  11. * @var string
  12. */
  13. private $type;
  14. /**
  15. * @var string
  16. */
  17. private $method;
  18. /**
  19. * Constructor
  20. *
  21. * @param string $type Class to match
  22. * @param string $method Method name to match
  23. */
  24. public function __construct($type, $method) {
  25. $this->type = strtolower(ltrim($type, '\\'));
  26. $this->method = strtolower($method);
  27. }
  28. /**
  29. * Does the given callable match the specification?
  30. *
  31. * @param callable $subject Callable to test
  32. * @return bool
  33. */
  34. public function matches($subject) {
  35. // We don't use the callable type-hint because it unnecessarily autoloads for static methods.
  36. if (is_string($subject)) {
  37. if (false === strpos($subject, '::')) {
  38. return false;
  39. }
  40. $subject = explode('::', $subject, 2);
  41. }
  42. if (!is_array($subject) || empty($subject[0]) || empty($subject[1]) || !is_string($subject[1])) {
  43. return false;
  44. }
  45. if (strtolower($subject[1]) !== $this->method) {
  46. return false;
  47. }
  48. if (is_object($subject[0])) {
  49. $subject[0] = get_class($subject[0]);
  50. }
  51. if (!is_string($subject[0])) {
  52. return false;
  53. }
  54. return (strtolower(ltrim($subject[0], '\\')) === $this->type);
  55. }
  56. }