Completed
Pull Request — master (#6)
by Tomasz
01:42
created

FormatContainer::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace Thunder\Serializard\FormatContainer;
3
4
use Thunder\Serializard\Exception\FormatAlreadyExistsException;
5
use Thunder\Serializard\Exception\FormatAliasTypeException;
6
use Thunder\Serializard\Format\FormatInterface;
7
8
/**
9
 * @author Tomasz Kowalczyk <[email protected]>
10
 */
11
final class FormatContainer implements FormatContainerInterface
12
{
13
    private $formats = [];
14
15 16
    public function add($alias, FormatInterface $handler)
16
    {
17 16
        if(false === \is_string($alias)) {
18 1
            throw new FormatAliasTypeException('Format alias must be a string.');
19
        }
20 15
        if(array_key_exists($alias, $this->formats)) {
21
            throw new FormatAlreadyExistsException(sprintf('Format with alias `%s` already exists.', $alias));
22
        }
23
24 15
        $this->formats[$alias] = $handler;
25 15
    }
26
27 15
    public function get($alias)
28
    {
29 15
        return array_key_exists($alias, $this->formats) ? $this->formats[$alias] : null;
30
    }
31
}
32