1 | <?php |
||
2 | |||
3 | /** |
||
4 | * FACTURA ELECTRÓNICA SUNAT |
||
5 | * UBL 2.1 |
||
6 | * Version 1.0 |
||
7 | * |
||
8 | * Copyright 2018, 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, $attribues = null) { |
||
52 | if (property_exists($this, $element)) { |
||
53 | $this->{$element} = $value; |
||
54 | if (is_array($attribues)) { |
||
55 | return $this->setElementAttributes($element, $attributes); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
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 |