Skip to content

Commit

Permalink
new features & phpstan level 9
Browse files Browse the repository at this point in the history
  • Loading branch information
rafageist committed Jan 23, 2024
1 parent 01cbd3e commit f2ff1cc
Show file tree
Hide file tree
Showing 16 changed files with 853 additions and 82 deletions.
38 changes: 17 additions & 21 deletions examples/calculations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,50 @@

use divengine\matrix;

// Create a matrix
$F_AMOUNT = fn($r, $c, matrix $m) => $m->{$r + .1} * $m->{$r + .2};
$F_TOTAL = fn ($r, $c, matrix $m) => array_sum($m->vertical($c, 1, $r - 1));
$F_CUMUL = fn ($r, $c, matrix $m) => $r == 1 ? $m->get($r, $c - 1)
: $m->get($r - 1, $c) + $m->get($r, $c - 1);
$table = new matrix([
["Product", "Price", "Count"],
["Apple", 10, 2],
["Banana", 35, 3],
["Orange", 6, 10],
]);

// Show the matrix
echo $table->format('txt', true);
echo $table . "\n";

$table->addColumn();
$table->set(0, 3, "Amount");
$table->{0.3} = "Amount";

// Fill the column with the product of the previous two columns
$table->fillVertical(3, 1, 3,
fn ($r, $c, matrix $m)
=> $m->get($r, $c - 1) * $m->get($r, $c - 2));
$table->fillVertical(3, 1, 3, $F_AMOUNT);

// Add a row with the sum of the previous rows
$table->addRow(["Total", "", "",
fn ($r, $c, matrix $m)
=> array_sum($m->vertical($c, 1, $r - 1))]);
echo $table->format('txt', true);
$table->addRow(["Total", "", "", $F_TOTAL]);
echo $table . "\n";

// Add a column with the sum of the previous columns
$table->addColumn();
$table->set(0, 4, "Cumul");
$table->{0.4} = "Cumul";

// Fill the column with the sum of the previous column
$table->fillVertical(4, 1, 3,
fn ($r, $c, matrix $m)
=> $r == 1 ? $m->get($r, $c - 1)
: $m->get($r - 1, $c) + $m->get($r, $c - 1));

echo $table->format('txt', true);
$table->fillVertical(4, 1, 3, $F_CUMUL);
echo $table . "\n";

// Change a value of the second row
$table->set(1, 1, 20);
echo $table->format('txt', true);
$table->{1.1} = 20;
echo $table . "\n";

// Remove the second row
$table->removeRow(1);
echo $table->format('txt', true);
echo $table . "\n";

// Show ranges
print_r($table->vertical(1, 1, 2));
print_r($table->horizontal(1, 1, 2));
print_r($table->range(1, 1, 2, 2));
echo "\n";

// Serialize
echo $table->format('serialize');
42 changes: 42 additions & 0 deletions examples/cells.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

include __DIR__ . '/../src/matrix.php';

use divengine\matrix;

$m = new matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);

// echo $m->get(1, 1);
echo $m->{1.1};
echo "\n";

// echo $m->{0.2};
echo $m->{.2};
echo "\n";

// echo $m->{2.2};
echo $m->{-1.2};
echo "\n";

for ($i = 0; $i < 3; $i++)
for ($j = 0; $j < 3; $j++)
echo $m->{$i + ($j / 10)} . " ";
echo "\n";

for ($i = 0; $i < 3; $i++)
for ($j = 0; $j < 3; $j++)
echo $m->{($i + ($j / 10) + 1) * -1} . " ";
echo "\n";

for ($i = 0; $i < 3; $i++)
for ($j = 0; $j < 3; $j++)
echo $m->{"$i.$j"} . " ";

echo "\n";

$m->{0.0} = 10;
echo $m;
28 changes: 28 additions & 0 deletions examples/create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

include __DIR__ . '/../src/matrix.php';

use divengine\matrix;

// bidimensional array of random numbers
$array = matrix::createArrayFromDims(5, 5, 0);

// Create a new object matrix
$m1 = new matrix($array);
echo $m1->formatTXT(false);
echo PHP_EOL;

// Create a new matrix using the static method create
$m2 = matrix::create($array);
echo $m2->formatTXT(false);
echo PHP_EOL;

// Create an matrix from dimensions
$m3 = matrix::dimension(5, 5, 0);
echo $m3->formatTXT(false);
echo PHP_EOL;

// Create an matrix from a string
$m4 = matrix::fromJSONFile(__DIR__.'/data/matrix.json');
echo $m4->formatTXT(false);
echo PHP_EOL;
34 changes: 34 additions & 0 deletions examples/custom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

include __DIR__ . '/../src/matrix.php';

use divengine\matrix;

$F_AMOUNT = fn($r, $c, matrix $m) => $m->{$r + .1} * $m->{$r +.2};
$F_TOTAL = fn($r, $c, matrix $m) => array_sum($m->vertical($c, 1, $m->rows - 1));
$F_AVG = fn($r, $c, matrix $m) => $F_TOTAL($r, $c, $m) / ($m->rows - 1);

class ProductsTable extends matrix
{
public function __construct(array $products)
{
global $F_AMOUNT, $F_AVG, $F_TOTAL;

$data = [["Name", "Price", "Qty", "Total"]];
foreach ($products as $product)
{
$data[] = [$product->name, $product->price, $product->quantity, $F_AMOUNT];
}

$data[] = ["Total", $F_AVG, $F_TOTAL, $F_TOTAL];

parent::__construct($data);
}
}

echo new ProductsTable([
(object) ["name" => "Apple", "price" => 1.5, "quantity" => 10],
(object) ["name" => "Banana", "price" => 2.5, "quantity" => 5],
(object) ["name" => "Orange", "price" => 3.5, "quantity" => 3],
(object) ["name" => "Kiwi", "price" => 4.5, "quantity" => 1],
]);
5 changes: 5 additions & 0 deletions examples/data/matrix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
[1,2,3],
[4,5,6],
[7,8,9]
]
75 changes: 75 additions & 0 deletions examples/edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

include __DIR__ . '/../src/matrix.php';

use divengine\matrix;

$m = new matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);

$m->insertAfterRow(1, [10, 11, 12]);

echo $m;
echo "\n";

$m->insertBeforeRow(1, [13, 14, 15]);

echo $m;
echo "\n";

$m->insertAfterColumn(1, 0);
echo $m;
echo "\n";

$m->insertBeforeColumn(1, 0);
echo $m;
echo "\n";

$m->removeColumn(1);
echo $m;
echo "\n";

$m->removeRow(2);
echo $m;
echo "\n";

$m->removeColumn(2);
echo $m;
echo "\n";

$m->addColumn(5);
echo $m;
echo "\n";

$m->removeColumnRange(1, 3);
echo $m;
echo "\n";

$m->addColumn(6);
$m->addRow([2, 2]);
echo $m;
echo "\n";

$m->removeRowRange(1, 3);
echo $m;
echo "\n";

$m->addColumn(7);
$m->addRow([3, 3, 3]);
echo $m;
echo "\n";

$m->fillRange(1, 1, 2, 2, 0);
echo $m;
echo "\n";

$m->insertAfterColumn(1, [1, 2, 3]);
echo $m;
echo "\n";

$m->insertBeforeColumn(1, [4, 5, 6]);
echo $m;
echo "\n";
49 changes: 49 additions & 0 deletions examples/extends.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

include __DIR__ . '/../src/matrix.php';

use divengine\matrix;

class MathTable extends matrix
{
public function __construct($number, $size, $operation, $operator)
{
$data = [];
for ($i = 1; $i <= $size; $i++)
{
$data[] = [$number, $operator, $i, "=", $operation];
}

parent::__construct($data);
}
}

class MultiplicationTable extends MathTable
{
public function __construct($number = 1, $size = 10)
{
parent::__construct($number, $size, fn ($r, $c, $m) => $m->get($r, 0) * $m->get($r, 2), "x");
}
}

class AdditionTable extends MathTable
{
public function __construct($number = 1, $size = 10)
{
parent::__construct($number, $size, fn ($r, $c, $m) => $m->get($r, 0) + $m->get($r, 2), "+");
}
}

for ($i = 1; $i <= 10; $i++)
{
echo new MultiplicationTable($i);
echo PHP_EOL;
}

echo PHP_EOL;

for ($i = 1; $i <= 10; $i++)
{
echo new AdditionTable($i);
echo PHP_EOL;
}
4 changes: 2 additions & 2 deletions examples/grouping.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
]);

// Show the matrix
echo $table->formatTXT(true);
echo $table;

// Group by
$result = $table->groupBy([0], function($key, $group){
Expand All @@ -33,5 +33,5 @@
echo "\n";
$groupBy = new matrix(array_values($result));
$groupBy->addRow(["Product", "Total"], onTop: true);
echo $groupBy->formatTXT(true);
echo $groupBy;

8 changes: 8 additions & 0 deletions examples/handlers/404.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return function ()
{
http_response_code(404);
echo "Page not found";
return true;
};
7 changes: 7 additions & 0 deletions examples/handlers/about.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return function ()
{
echo "About";
return true;
};
7 changes: 7 additions & 0 deletions examples/handlers/admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return function ()
{
echo "Admin";
return true;
};
7 changes: 7 additions & 0 deletions examples/handlers/home.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return function ()
{
echo "Home";
return true;
};
11 changes: 11 additions & 0 deletions examples/handlers/log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return function ()
{
$url = $_SERVER['REQUEST_URI'] ?? "";
$moment = date("Y-m-d H:i:s");

// show in console log
echo "<script>console.log('{$moment} - {$url}');</script>";
return true;
};
7 changes: 7 additions & 0 deletions examples/handlers/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return function ()
{
echo "Login";
return true;
};
Loading

0 comments on commit f2ff1cc

Please sign in to comment.