1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
use TypedArrays\AbstractTypedArray; |
6
|
|
|
use TypedArrays\Exceptions\InvalidSetupException; |
7
|
|
|
use TypedArrays\Exceptions\InvalidTypeException; |
8
|
|
|
use TypedArrays\Exceptions\ListException; |
9
|
|
|
use TypedArrays\Exceptions\MapException; |
10
|
|
|
use TypedArrays\Scalars\ImmutableIntegerList; |
11
|
|
|
|
12
|
|
|
require getcwd() . '/vendor/autoload.php'; |
13
|
|
|
|
14
|
|
|
final class IntMatrix extends AbstractTypedArray |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @throws InvalidSetupException |
18
|
|
|
* @throws InvalidTypeException |
19
|
|
|
* @throws ListException |
20
|
|
|
* @throws MapException |
21
|
|
|
*/ |
22
|
|
|
public static function fromArrayMatrix(array $input = []): self |
23
|
|
|
{ |
24
|
|
|
return new self(array_map( |
25
|
|
|
static fn (array $row) => new ImmutableIntegerList($row), |
26
|
|
|
$input |
27
|
|
|
)); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function typeToEnforce(): string |
31
|
|
|
{ |
32
|
|
|
return ImmutableIntegerList::class; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function isMutable(): bool |
36
|
|
|
{ |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function collectionType(): string |
41
|
|
|
{ |
42
|
|
|
return self::COLLECTION_TYPE_LIST; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Example 1: using the normal constructor |
47
|
|
|
$intRow1 = new ImmutableIntegerList([1, 2, 3]); |
48
|
|
|
$intRow2 = new ImmutableIntegerList([4, 5, 6]); |
49
|
|
|
$intRow3 = new ImmutableIntegerList([7, 8, 9]); |
50
|
|
|
$intMatrix1 = new IntMatrix([$intRow1, $intRow2, $intRow3]); |
51
|
|
|
|
52
|
|
|
print 'Matrix 1' . PHP_EOL; |
53
|
|
|
printMatrix($intMatrix1); |
54
|
|
|
|
55
|
|
|
// Example 1: using the named constructor |
56
|
|
|
$intMatrix2 = IntMatrix::fromArrayMatrix([ |
57
|
|
|
[1, 2, 3], |
58
|
|
|
[4, 5, 6], |
59
|
|
|
[7, 8, 9], |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
print 'Matrix 2' . PHP_EOL; |
63
|
|
|
printMatrix($intMatrix2); |
64
|
|
|
|
65
|
|
|
function printMatrix(IntMatrix $matrix): void |
66
|
|
|
{ |
67
|
|
|
foreach ($matrix as $row) { |
68
|
|
|
foreach ($row as $column => $value) { |
69
|
|
|
print $value . ' '; |
70
|
|
|
} |
71
|
|
|
print PHP_EOL; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|