Skip to content

Commit

Permalink
worksheets, new methods and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
rafageist committed Jan 17, 2024
1 parent c6e4b53 commit 08a2327
Show file tree
Hide file tree
Showing 3 changed files with 296 additions and 63 deletions.
54 changes: 54 additions & 0 deletions examples/simple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

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

use divengine\matrix;

// simple list of nums
$nums = new matrix([
["", 1, 2, 3],
["", 4, 5, 6]
]);

// get item
echo $nums->get(1, 3); // 6

// set item
$nums->set(1, 3, 10);
echo $nums->formatTXT();
echo PHP_EOL;

// get row
print_r($nums->getRow(1)); // [4, 5, 10]

// get column
print_r($nums->getColumn(3)); // [3, 10]

// get range
$range = $nums->range(0, 0, 1, 1); // [[1, 2], [4, 5]]

// new matrix from range
$rangeMatrix = new matrix($range);

// show $range
echo $rangeMatrix->formatTXT();
echo PHP_EOL;

// add row
$nums->addRow(["", 0, 0, 0]);
echo $nums->formatTXT();
echo PHP_EOL;

// add column
$nums->addColumn(0);
echo $nums->formatTXT();
echo PHP_EOL;

// fill function
$nums->fillVertical(0, 0, $nums->getTotalRows() - 1, fn() => date("Y-m-d"));
echo $nums->formatTXT();
echo PHP_EOL;

// add row on top
$nums->addRow(["Date\t", "Value1", "Value2", "Value3", "Value4"], onTop: true);
echo $nums->formatTXT(true);
61 changes: 61 additions & 0 deletions examples/worksheet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

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

use divengine\matrix;

$F_AMOUNT = fn($row, $col, $matrix) => $matrix->get($row, $col - 2) * $matrix->get($row, $col - 1);
$F_TOTAL = fn($row, $col, $matrix) => array_sum($matrix->vertical($col, 1, $row - 1));
$F_AVG = fn($row, $col, $matrix) => number_format($F_TOTAL($row, $col, $matrix) / ($row - 1), 1);
$F_BOTTOM_RIGHT = fn(matrix $matrix) => $matrix->get($matrix->getTotalRows() - 1, $matrix->getTotalColumns() - 1);

// sheet of products
$products = new matrix([
/* 0 1 2 3 */
/* 0 */ ["Product", "Price", "Count", "Amount"],
//----------------------------------------
/* 1 */ ["Apple", 5, 2, $F_AMOUNT],
/* 2 */ ["Banana", 6, 3, $F_AMOUNT],
/* 3 */ ["Orange", 6, 10, $F_AMOUNT],
//-----------------------------------------
/* 4 */ ["Totals", $F_AVG, $F_TOTAL, $F_TOTAL]
]);

// sheet of services
$services = new matrix([
/* 0 1 2 3 */
/* 0 */ ["Service", "Price", "Count", "Amount"],
//-----------------------------------------
/* 1 */ ["Clean", 10, 2, $F_AMOUNT],
/* 2 */ ["Paint", 35, 3, $F_AMOUNT],
/* 3 */ ["Repair", 6, 10, $F_AMOUNT],
//------------------------------------------
/* 4 */ ["Totals", $F_AVG, $F_TOTAL, $F_TOTAL]
]);

$T_PRODUCTS = fn() => $F_BOTTOM_RIGHT($products);
$T_SERVICES = fn() => $F_BOTTOM_RIGHT($services);

// sheet of earnings
$earnings = new matrix([
["\t", "Amount"],
//------------------------
["Products", $T_PRODUCTS],
["Services", $T_SERVICES],
//------------------------
["Earnings", $F_TOTAL]
]);

echo $products->formatTXT(true);
echo "\n";
echo $services->formatTXT(true);
echo "\n";
echo $earnings->formatTXT(true);
echo "\n";

$products->set(1, 1, 10); // $earnings will be updated automatically

echo $products->formatTXT(true);
echo "\n";
echo $earnings->formatTXT(true);
echo "\n";
Loading

0 comments on commit 08a2327

Please sign in to comment.