Facade.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Elastica\QueryBuilder;
  3. use Elastica\Exception\QueryBuilderException;
  4. /**
  5. * Facade for a specific DSL object.
  6. *
  7. * @author Manuel Andreo Garcia <andreo.garcia@googlemail.com>
  8. **/
  9. class Facade
  10. {
  11. /**
  12. * @var DSL
  13. */
  14. private $_dsl;
  15. /**
  16. * @var Version
  17. */
  18. private $_version;
  19. /**
  20. * Constructor.
  21. *
  22. * @param DSL $dsl
  23. * @param Version $version
  24. */
  25. public function __construct(DSL $dsl, Version $version)
  26. {
  27. $this->_dsl = $dsl;
  28. $this->_version = $version;
  29. }
  30. /**
  31. * Executes DSL methods.
  32. *
  33. * @param string $name
  34. * @param array $arguments
  35. *
  36. * @throws QueryBuilderException
  37. *
  38. * @return mixed
  39. */
  40. public function __call($name, array $arguments)
  41. {
  42. // defined check
  43. if (false === method_exists($this->_dsl, $name)) {
  44. throw new QueryBuilderException(
  45. 'undefined '.$this->_dsl->getType().' "'.$name.'"'
  46. );
  47. }
  48. // version support check
  49. if (false === $this->_version->supports($name, $this->_dsl->getType())) {
  50. $reflection = new \ReflectionClass($this->_version);
  51. throw new QueryBuilderException(
  52. $this->_dsl->getType().' "'.$name.'" in '.$reflection->getShortName().' not supported'
  53. );
  54. }
  55. return call_user_func_array([$this->_dsl, $name], $arguments);
  56. }
  57. }