Completed
Push — master ( 8004f9...06b653 )
by Thibaud
04:58 queued 02:39
created

DefinitionMap::getMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Alchemy\Phraseanet\Mapping;
4
5
/**
6
 * Simple class to map friendly names to Phraseanet media sub-definitions.
7
 *
8
 * @package Alchemy\Phraseanet
9
 */
10
class DefinitionMap
11
{
12
    /**
13
     * @var array
14
     */
15
    private $map;
16
17
    /**
18
     * @param array $map Initial mapping using friendly names as keys and matching sub-definitions as values
19
     */
20 13
    public function __construct(array $map = array())
21
    {
22 13
        foreach ($map as $definition => $subdefinition) {
23 7
            $this->addMapping($definition, $subdefinition);
24 13
        }
25 13
    }
26
27
    /**
28
     * @return string[]
29
     */
30 1
    public function toArray()
31
    {
32 1
        return $this->map;
33
    }
34
35
    /**
36
     * @param string $definition
37
     * @param string|array $subDefinition
38
     */
39 8
    public function addMapping($definition, $subDefinition)
40
    {
41 8
        if (!  is_array($subDefinition)) {
42 7
            $subDefinition = [ 'default' => $subDefinition ];
43 7
        }
44
45 8
        $this->map[$definition] = $subDefinition;
46 8
    }
47
48
    /**
49
     * @param string $definition
50
     * @param string|null $type
51
     * @return bool
52
     */
53 10
    public function hasSubDefinition($definition, $type = null)
54
    {
55 10
        if ($type !== null && isset($this->map[$definition][$type])) {
56 4
            return true;
57
        }
58
59 9
        return isset($this->map[$definition]['default']);
60
    }
61
62
    /**
63
     * @param string $definition
64
     * @param string|null $type
65
     * @return string
66
     */
67 7
    public function getSubDefinition($definition, $type = null)
68
    {
69 7
        if (! $this->hasSubDefinition($definition, $type)) {
70 2
            throw new \OutOfBoundsException(sprintf('No subdef configured for definition "%s".', $definition));
71
        }
72
73 5
        if ($type !== null && isset($this->map[$definition][$type])) {
74 3
            return $this->map[$definition][$type];
75
        }
76
77 4
        return $this->map[$definition]['default'];
78
    }
79
}
80