IntMatrix::typeToEnforce()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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