1<?php
2/**
3 * Handles search operations provided by the objects registered to the
4 * server structure.
5 *
6 * PHP version 5
7 *
8 * @category Kolab
9 * @package  Kolab_Server
10 * @author   Gunnar Wrobel <wrobel@pardus.de>
11 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
12 * @link     http://pear.horde.org/index.php?package=Kolab_Server
13 */
14
15/**
16 * Handles search operations provided by the objects registered to the
17 * server structure.
18 *
19 * Copyright 2008-2016 Horde LLC (http://www.horde.org/)
20 *
21 * See the enclosed file COPYING for license information (LGPL). If you
22 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
23 *
24 * @category Kolab
25 * @package  Kolab_Server
26 * @author   Gunnar Wrobel <wrobel@pardus.de>
27 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
28 * @link     http://pear.horde.org/index.php?package=Kolab_Server
29 */
30class Horde_Kolab_Server_Search_Base
31implements Horde_Kolab_Server_Search_Interface
32{
33    /**
34     * A link to the composite server handler.
35     *
36     * @var Horde_Kolab_Server_Composite
37     */
38    private $_composite;
39
40    /**
41     * The search methods offered by the object defined for this server.
42     *
43     * @var array
44     */
45    private $_searches;
46
47    /**
48     * Set the composite server reference for this object.
49     *
50     * @param Horde_Kolab_Server_Composite $composite A link to the composite
51     *                                                server handler.
52     *
53     * @return NULL
54     */
55    public function setComposite(
56        Horde_Kolab_Server_Composite $composite
57    ) {
58        $this->_composite = $composite;
59        $this->_searches = $this->_getSearchOperations();
60    }
61
62    /**
63     * Returns the set of search operations supported by this server type.
64     *
65     * @return array An array of supported search operations.
66     */
67    private function _getSearchOperations()
68    {
69        $server_searches = array();
70        foreach ($this->_composite->structure->getSearchOperations() as $search_class) {
71            if (!class_exists($search_class)) {
72                throw new Horde_Kolab_Server_Exception(
73                    sprintf(
74                        "%s::getSearchOperations specified non-existing class \"%s\"!",
75                        get_class($this->_composite->structure),
76                        $search_class
77                    )
78                );
79            }
80            $methods = get_class_methods($search_class);
81            unset($methods['getComposite']);
82            unset($methods['__construct']);
83            foreach ($methods as $method) {
84                $server_searches[$method] = array('class' => $search_class);
85            }
86        }
87        return $server_searches;
88    }
89
90    /**
91     * Returns the set of search operations supported by this server type.
92     *
93     * @return array An array of supported search operations.
94     */
95    public function getSearchOperations()
96    {
97        return $this->_searches;
98    }
99
100    /**
101     * Capture undefined calls and assume they refer to a search operation.
102     *
103     * @param string $method The name of the called method.
104     * @param array  $args   Arguments of the call.
105     *
106     * @return NULL.
107     *
108     * @throws Horde_Kolab_Server_Exception
109     */
110    public function __call($method, $args)
111    {
112        if (in_array($method, array_keys($this->_searches))) {
113            $class = $this->_searches[$method]['class'];
114            $search = new $class($this->_composite->structure);
115            return call_user_func_array(array($search, $method), $args);
116        }
117        throw new Horde_Kolab_Server_Exception(
118            sprintf(
119                "The server type \"%s\" with structure \"%s\" does not support"
120                . " method \"%s\"!",
121                get_class($this->_composite->server),
122                get_class($this->_composite->structure),
123                $method
124            )
125        );
126    }
127
128}