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