1<?php
2/**
3 * Ensures that a search yields only a single return value.
4 *
5 * PHP version 5
6 *
7 * @category Kolab
8 * @package  Kolab_Server
9 * @author   Gunnar Wrobel <wrobel@pardus.de>
10 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 * @link     http://pear.horde.org/index.php?package=Kolab_Server
12 */
13
14/**
15 * Ensures that a search yields only a single return value.
16 *
17 * Copyright 2008-2016 Horde LLC (http://www.horde.org/)
18 *
19 * See the enclosed file COPYING for license information (LGPL). If you
20 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21 *
22 * @category Kolab
23 * @package  Kolab_Server
24 * @author   Gunnar Wrobel <wrobel@pardus.de>
25 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
26 * @link     http://pear.horde.org/index.php?package=Kolab_Server
27 */
28class Horde_Kolab_Server_Search_Operation_Constraint_Strict
29implements Horde_Kolab_Server_Search_Operation_Interface
30{
31    /**
32     * A link to the search.
33     *
34     * @var Horde_Kolab_Server_Search
35     */
36    private $_search;
37
38    /**
39     * Constructor
40     *
41     * @param Horde_Kolab_Server_Search $search The search being restricted.
42     */
43    public function __construct(
44        Horde_Kolab_Server_Search_Operation_Interface $search
45    ) {
46        $this->_search = $search;
47    }
48
49    /**
50     * Return the reference to the server structure.
51     *
52     * @return Horde_Kolab_Server_Structure_Interface
53     */
54    public function getStructure()
55    {
56        return $this->_search->getStructure();
57    }
58
59    /**
60     * Delegate to the actual search operation.
61     *
62     * @param string $method The name of the called method.
63     * @param array  $args   Arguments of the call.
64     *
65     * @return array The search result.
66     */
67    public function __call($method, $args)
68    {
69        $result = call_user_func_array(array($this->_search, $method), $args);
70        if (count($result) > 1) {
71            throw new Horde_Kolab_Server_Exception(
72                sprintf(
73                    "Found %s results when expecting only one!",
74                    count($result)
75                ),
76                Horde_Kolab_Server_Exception::SEARCH_CONSTRAINT_TOO_MANY
77            );
78        }
79        return array_shift($result);
80    }
81}