summaryrefslogtreecommitdiff
path: root/app/Core/Group/GroupManager.php
blob: a05591397b4a53ab86bf447b9e389736f4f8009e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

namespace Kanboard\Core\Group;

/**
 * Group Manager
 *
 * @package  Kanboard\Core\Group
 * @author   Frederic Guillot
 */
class GroupManager
{
    /**
     * List of backend providers
     *
     * @access protected
     * @var array
     */
    protected $providers = array();

    /**
     * Register a new group backend provider
     *
     * @access public
     * @param  GroupBackendProviderInterface $provider
     * @return GroupManager
     */
    public function register(GroupBackendProviderInterface $provider)
    {
        $this->providers[] = $provider;
        return $this;
    }

    /**
     * Find a group from a search query
     *
     * @access public
     * @param  string $input
     * @return GroupProviderInterface[]
     */
    public function find($input)
    {
        $groups = array();

        foreach ($this->providers as $provider) {
            $groups = array_merge($groups, $provider->find($input));
        }

        return $this->removeDuplicates($groups);
    }

    /**
     * Remove duplicated groups
     *
     * @access protected
     * @param  array $groups
     * @return GroupProviderInterface[]
     */
    protected function removeDuplicates(array $groups)
    {
        $result = array();

        foreach ($groups as $group) {
            if (! isset($result[$group->getName()])) {
                $result[$group->getName()] = $group;
            }
        }

        return array_values($result);
    }
}