Completed
Pull Request — master (#6)
by Dan
01:33
created

AbstractIdNameMap::getId()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Nopolabs\Yabot\Slack;
4
5
6
abstract class AbstractIdNameMap
7
{
8
    private $things = [];
9
    private $thingsById = [];
10
    private $thingsByName = [];
11
12
    abstract protected function getId($thing) : string;
13
14
    abstract protected function getName($thing) : string;
15
16
    public function update(array $things)
17
    {
18
        $this->things = $things;
19
        foreach ($things as $index => $thing) {
20
            $this->thingsById[$this->getId($thing)] = $index;
21
            $this->thingsByName[$this->getName($thing)] = $index;
22
        }
23
    }
24
25
    public function byId($id)
26
    {
27
        if ($id && isset($this->thingsById[$id])) {
28
            return $this->things[$this->thingsById[$id]];
29
        }
30
31
        return null;
32
    }
33
34
    public function byName($name)
35
    {
36
        if ($name && isset($this->thingsByName[$name])) {
37
            return $this->things[$this->thingsByName[$name]];
38
        }
39
40
        return null;
41
    }
42
43
    public function getMap() : array
44
    {
45
        $map = [];
46
47
        foreach ($this->things as $thing) {
48
            $map[$this->getId($thing)] = $this->getName($thing);
49
        }
50
51
        return $map;
52
    }
53
}