Ajde_Component::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
abstract class Ajde_Component extends Ajde_Object_Standard
4
{
5
    const AC_XMLNS = 'ac';
6
    const AV_XMLNS = 'av';
7
8
    public $attributes = [];
9
    public $innerXml = null;
10
11
    protected $_parseRules = [];
12
13
    /**
14
     * @var Ajde_Template_Parser
15
     */
16
    protected $_parser = null;
17
18
    /**
19
     * @param Ajde_Template_Parser $parser
20
     * @param array                $attributes
21
     */
22
    public function __construct(Ajde_Template_Parser $parser, $attributes, $innerXml = null)
23
    {
24
        $this->attributes = $attributes;
25
        $this->innerXml = $innerXml;
26
        $this->_parser = $parser;
27
        $this->_parseRules = $this->_init();
28
    }
29
30
    /**
31
     * @return Ajde_Template_Parser
32
     */
33
    public function getParser()
34
    {
35
        return $this->_parser;
36
    }
37
38
    /**
39
     * @param DOMNode $node
40
     *
41
     * @return Ajde_Component
42
     */
43
    public static function fromNode(Ajde_Template_Parser $parser, DOMNode $node)
44
    {
45
        $componentName = ucfirst(str_replace(self::AC_XMLNS.':', '', $node->nodeName));
46
        $className = 'Ajde_Component_'.$componentName;
47
        $nodeAttributes = $node->attributes;
48
        $innerXml = $parser->innerXml($node);
0 ignored issues
show
Documentation Bug introduced by
The method innerXml does not exist on object<Ajde_Template_Parser>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
49
        $attributes = [];
50
        foreach ($nodeAttributes as $attribute) {
51
            $attributes[$attribute->name] = $attribute->value;
52
        }
53
54
        return new $className($parser, $attributes, $innerXml);
55
    }
56
57
    abstract public function process();
58
59
    protected function _init()
60
    {
61
        return [];
62
    }
63
64
    protected function _attributeParse()
65
    {
66
        foreach ($this->_parseRules as $attributeSet => $rule) {
67
            if (array_key_exists($attributeSet, $this->attributes)) {
68
                return $rule;
69
            }
70
        }
71
    }
72
}
73