Completed
Push — master ( 3cecaa...777c1d )
by Kacper
03:21
created

XmlElementFactory::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 6
rs 9.4285
1
<?php
2
/**
3
 * XMPP Library
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Xmpp\Xml;
17
18
class XmlElementFactory
19
{
20
    /**
21
     * @var string[string]
22
     */
23
    private $_lookup = [];
24
    
25
    public function lookup($namespace, $tag)
26
    {
27
        if (isset($this->_lookup["$tag@$namespace"])) {
28
            return $this->_lookup["$tag@$namespace"];
29
        } elseif (isset($this->_lookup[$namespace])) {
30
            return $this->_lookup[$namespace];
31
        } else {
32
            return XmlElement::class;
33
        }
34
    }
35
36
    public function register($class, $namespace, $tag = null)
37
    {
38
        if (is_array($namespace)) {
39
            $this->_lookup = array_merge($this->_lookup, $namespace);
40
41
            return;
42
        }
43
44
        if ($tag !== null) {
45
            $namespace = "$tag@$namespace";
46
        }
47
48
        $this->_lookup[$namespace] = $class;
49
    }
50
51
    public function load(array $dictionary) {
52
        foreach($dictionary as $element) {
53
            $this->register($element[0], $element['uri'] ?? null, $element['name'] ?? null);
54
        }
55
    }
56
57
    public function create($namespace, $tag, $arguments = [])
58
    {
59
        $class = $this->lookup($namespace, $tag);
60
        /** @noinspection PhpUndefinedMethodInspection */
61
        return $class::plain(...$arguments);
62
    }
63
}
64