Completed
Push — master ( cb8a61...2e8229 )
by Maxime
02:52 queued 01:04
created

MediaInfoContainerBuilder::sanitizeAttributes()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 22
rs 8.6738
1
<?php
2
3
namespace Mhor\MediaInfo\Builder;
4
5
use Mhor\MediaInfo\Container\MediaInfoContainer;
6
use Mhor\MediaInfo\Factory\AttributeFactory;
7
use Mhor\MediaInfo\Factory\TypeFactory;
8
use Mhor\MediaInfo\Type\AbstractType;
9
10
class MediaInfoContainerBuilder
11
{
12
    /**
13
     * @var MediaInfoContainer
14
     */
15
    private $mediaInfoContainer;
16
17
    /**
18
     * @var TypeFactory
19
     */
20
    private $typeFactory;
21
22
    public function __construct()
23
    {
24
        $this->mediaInfoContainer = new MediaInfoContainer();
25
        $this->typeFactory = new TypeFactory();
26
    }
27
28
    /**
29
     * @return MediaInfoContainer
30
     */
31
    public function build()
32
    {
33
        return $this->mediaInfoContainer;
34
    }
35
36
    /**
37
     * @param string $version
38
     */
39
    public function setVersion($version)
40
    {
41
        $this->mediaInfoContainer->setVersion($version);
42
    }
43
44
    /**
45
     * @param $typeName
46
     * @param array $attributes
47
     *
48
     * @throws \Mhor\MediaInfo\Exception\UnknownTrackTypeException
49
     */
50
    public function addTrackType($typeName, array $attributes)
51
    {
52
        $trackType = $this->typeFactory->create($typeName);
53
        $this->addAttributes($trackType, $attributes);
54
55
        $this->mediaInfoContainer->add($trackType);
56
    }
57
58
    /**
59
     * @param AbstractType $trackType
60
     * @param array        $attributes
61
     */
62
    private function addAttributes(AbstractType $trackType, $attributes)
63
    {
64
        foreach ($this->sanitizeAttributes($attributes) as $attribute => $value) {
65
            if ($attribute[0] === '@') {
66
                continue;
67
            }
68
69
            $trackType->set(
70
                $attribute,
71
                AttributeFactory::create($attribute, $value)
72
            );
73
        }
74
    }
75
76
    /**
77
     * @param array $attributes
78
     *
79
     * @return array
80
     */
81
    private function sanitizeAttributes(array $attributes)
82
    {
83
        $sanitizeAttributes = array();
84
        foreach ($attributes as $key => $value) {
85
            $key = $this->formatAttribute($key);
86
            if (isset($sanitizeAttributes[$key])) {
87
                if (!is_array($sanitizeAttributes[$key])) {
88
                    $sanitizeAttributes[$key] = array($sanitizeAttributes[$key]);
89
                }
90
91
                if (!is_array($value)) {
92
                    $value = array($value);
93
                }
94
95
                $value = $sanitizeAttributes[$key] + $value;
96
            }
97
98
            $sanitizeAttributes[$key] = $value;
99
        }
100
101
        return $sanitizeAttributes;
102
    }
103
104
    /**
105
     * @param string $attribute
106
     *
107
     * @return string
108
     */
109
    private function formatAttribute($attribute)
110
    {
111
        return trim(str_replace('__', '_', str_replace(' ', '_', strtolower($attribute))), '_');
112
    }
113
}
114