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     * @param DSL     $dsl
28     * @param Version $version
29     */
30    public function __construct(DSL $dsl, Version $version)
31    {
32        $this->_dsl = $dsl;
33        $this->_version = $version;
34    }
35
36    /**
37     * Executes DSL methods.
38     *
39     * @param string $name
40     * @param array  $arguments
41     *
42     * @throws QueryBuilderException
43     *
44     * @return mixed
45     */
46    public function __call(string $name, array $arguments)
47    {
48        // defined check
49        if (false === \method_exists($this->_dsl, $name)) {
50            throw new QueryBuilderException(
51                'undefined '.$this->_dsl->getType().' "'.$name.'"'
52            );
53        }
54
55        // version support check
56        if (false === $this->_version->supports($name, $this->_dsl->getType())) {
57            $reflection = new \ReflectionClass($this->_version);
58            throw new QueryBuilderException(
59                $this->_dsl->getType().' "'.$name.'" in '.$reflection->getShortName().' not supported'
60            );
61        }
62
63        return \call_user_func_array([$this->_dsl, $name], $arguments);
64    }
65}
66