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\Tools;
|
12
|
|
|
|
13
|
|
|
use InvalidArgumentException;
|
14
|
|
|
|
15
|
|
|
class XMatrix {
|
16
|
|
|
|
17
|
|
|
const PRINT_STYLE = '<style>
|
18
|
|
|
.xm-table{border:#a7a7a7 dashed 1px; border-collapse: collapse; margin: 5px;}
|
19
|
|
|
.xm-table td, .xm-table th{border: dashed 1px #1976D2;padding: 2px 4px;}
|
20
|
|
|
.xm-table th{border-bottom: solid 2px #1976D2;}
|
21
|
|
|
.align-r{text-align: right;}
|
22
|
|
|
</style>';
|
23
|
|
|
|
24
|
|
|
private $_MATRIX = [];
|
25
|
|
|
private $_ROW = [];
|
26
|
|
|
|
27
|
|
|
/**
|
28
|
|
|
* @var int|string[]
|
29
|
|
|
*/
|
30
|
|
|
protected $columns = 0;
|
31
|
|
|
private $totalColumns = 0;
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
*
|
35
|
|
|
* @param int|string[] $columns
|
36
|
|
|
*/
|
37
|
|
|
public function __construct($columns = 0) {
|
38
|
|
|
$this->columns = $columns ? $columns : $this->columns;
|
39
|
|
|
$this->totalColumns = is_numeric($this->columns) ? $this->columns : count($this->columns);
|
40
|
|
|
for ($i = 0; $i < $this->totalColumns; $i++) {
|
41
|
|
|
$this->_ROW[$i] = null;
|
42
|
|
|
}
|
43
|
|
|
}
|
44
|
|
|
|
45
|
|
|
public function set($columnIndex, $rowIndex, $value) {
|
46
|
|
|
if (!isset($this->_MATRIX[$rowIndex])) {
|
47
|
|
|
$this->completeMatrix($rowIndex);
|
48
|
|
|
}
|
49
|
|
|
if ($columnIndex >= $this->totalColumns) {
|
50
|
|
|
throw new InvalidArgumentException(printf('The index %s exceeds the number of defined for this matrix!', $columnIndex));
|
51
|
|
|
}
|
52
|
|
|
$this->_MATRIX[$rowIndex][$columnIndex] = $value;
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
public function get($columnIndex, $rowIndex) {
|
56
|
|
|
return $this->_MATRIX[$rowIndex][$columnIndex];
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
public function getRow($rowIndex) {
|
60
|
|
|
return $this->_MATRIX[$rowIndex];
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
private function completeMatrix($y) {
|
64
|
|
|
for ($i = 0; $i <= $y; $i++) {
|
65
|
|
|
if (!isset($this->_MATRIX[$i])) {
|
66
|
|
|
$this->_MATRIX[$i] = $this->_ROW;
|
67
|
|
|
}
|
68
|
|
|
}
|
69
|
|
|
}
|
70
|
|
|
|
71
|
|
|
public function getMatrix() {
|
72
|
|
|
return $this->_MATRIX;
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
public function countRows() {
|
76
|
|
|
return count($this->_MATRIX);
|
77
|
|
|
}
|
78
|
|
|
|
79
|
|
|
public function each(callable $fn) {
|
80
|
|
|
foreach ($this->_MATRIX as &$row) {
|
81
|
|
|
$fn($row);
|
82
|
|
|
}
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
public function getHtml() {
|
86
|
|
|
$matrix = $this->_MATRIX;
|
87
|
|
|
$html = [];
|
88
|
|
|
$html[] = self::PRINT_STYLE;
|
89
|
|
|
$html[] = '<table class="xm-table">';
|
90
|
|
|
if (is_array($this->columns)) {
|
91
|
|
|
$html[] = '<tr><th>' . implode('</th><th>', $this->columns) . '</th></tr>';
|
92
|
|
|
} else {
|
93
|
|
|
$html[] = $this->getHtmlTableHead($matrix);
|
94
|
|
|
}
|
95
|
|
|
$html[] = $this->getHtmlTableBody($matrix);
|
96
|
|
|
$html[] = '</table>';
|
97
|
|
|
return implode('', $html);
|
98
|
|
|
}
|
99
|
|
|
|
100
|
|
|
public function getHtmlTable(array $table) {
|
101
|
|
|
if (empty($table)) {
|
102
|
|
|
return '';
|
103
|
|
|
}
|
104
|
|
|
return '<table>' . $this->getHtmlTableHead($table) . $this->getHtmlTableBody($table) . '</table>';
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
private function getHtmlTableHead(array $table) {
|
108
|
|
|
$html = [];
|
109
|
|
|
$keys = array_keys($table[0]);
|
110
|
|
|
$html[] = '<tr>';
|
111
|
|
|
foreach ($keys as $key) {
|
112
|
|
|
$html[] = '<th>' . $key . '</th>';
|
113
|
|
|
}
|
114
|
|
|
$html[] = '</tr>';
|
115
|
|
|
return implode('', $html);
|
116
|
|
|
}
|
117
|
|
|
|
118
|
|
|
private function getHtmlTableBody(array $table) {
|
119
|
|
|
$html = [];
|
120
|
|
|
foreach ($table as $row) {
|
121
|
|
|
$html[] = '<tr>';
|
122
|
|
|
foreach ($row as $value) {
|
123
|
|
|
// if array recursive
|
124
|
|
|
if (is_array($value)) {
|
125
|
|
|
$html[] = '<td>';
|
126
|
|
|
$html[] = $this->getHtmlTable($value);
|
127
|
|
|
$html[] = '</td>';
|
128
|
|
|
} else {
|
129
|
|
|
$class = is_float($value) || is_int($value) ? 'class="align-r"' : '';
|
130
|
|
|
$html[] = "<td $class >$value</td>";
|
131
|
|
|
}
|
132
|
|
|
}
|
133
|
|
|
$html[] = '</tr>';
|
134
|
|
|
}
|
135
|
|
|
return implode('', $html);
|
136
|
|
|
}
|
137
|
|
|
|
138
|
|
|
public function sum($columnIndex) {
|
139
|
|
|
$column = array_column($this->_MATRIX, $columnIndex);
|
140
|
|
|
return array_sum($column);
|
141
|
|
|
}
|
142
|
|
|
|
143
|
|
|
}
|
144
|
|
|
|