BaseComponent   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 57
rs 10
c 1
b 0
f 0
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setElementAttributes() 0 8 2
A xmlSerialize() 0 1 1
A setElementAttribute() 0 2 1
A writeLineJump() 0 4 1
A validate() 0 6 6
A set() 0 10 3
A get() 0 5 2
1
<?php
2
3
/**
4
 * MÓDULO DE EMISIÓN ELECTRÓNICA F72X
5
 * UBL 2.1
6
 * Version 1.0
7
 * 
8
 * Copyright 2019, Jaime Cruz
9
 */
10
11
namespace F72X\UblComponent;
12
13
use Exception;
14
use InvalidArgumentException;
15
use Sabre\Xml\XmlSerializable;
16
use Sabre\Xml\Writer;
17
18
class BaseComponent implements XmlSerializable {
19
20
    protected $validations = [];
21
22
    public function xmlSerialize(Writer $writer) {
23
        
24
    }
25
26
    public function validate() {
27
        foreach ($this->validations as $field => $validation) {
28
            if (is_numeric($field)) {
29
                // Null or empty array
30
                if (is_null($this->{$validation}) || (is_array($this->{$validation}) && count($this->{$validation}) === 0)) {
31
                    throw new InvalidArgumentException(get_class($this) . " $validation is required");
32
                }
33
            }
34
        }
35
    }
36
37
    public function setElementAttribute($element, $attribute, $value) {
38
        $this->{$element}[$attribute] = $value;
39
    }
40
41
    public function setElementAttributes($element, $attributes) {
42
        $attProperty = $element . 'Attributes';
43
        if (property_exists($this, $attProperty)) {
44
            $this->{$attProperty} = $attributes;
45
        } else {
46
            throw new Exception("The property $attProperty, doesn't exist!");
47
        }
48
        return $this;
49
    }
50
51
    public function set($element, $value, $attributes = null) {
52
        if (property_exists($this, $element)) {
53
            $this->{$element} = $value;
54
            if (is_array($attributes)) {
55
                return $this->setElementAttributes($element, $attributes);
56
            }
57
        } else {
58
            throw new Exception("The element $element, doesn't exist!");
59
        }
60
        return $this;
61
    }
62
63
    public function get($element) {
64
        if (property_exists($this, $element)) {
65
            return $this->{$element};
66
        } else {
67
            throw new Exception("The element $element, doesn't exist!");
68
        }
69
    }
70
71
    public function writeLineJump(Writer $writer) {
72
        // Line jump
73
        $ENTER = chr(13) . chr(10);
74
        $writer->writeRaw($ENTER);
75
    }
76
77
}
78