Composer library providing core functionalities

1.0.82 2024-04-29 19:01 UTC

README

Composer library to provide PHP core functionalities

The package contains classes for :

  • containers (cache, pool)
  • formatters (export data to CSV of any other format - currently only CSV export is implemented)
  • helpers (to help processing data in requests, to sanitize user data, to encode/decode data, etc.)

Setup instructions

To install net-tools/core package, just require it through composer : require net-tools/core:^1.0.0

How to use ?

The classes provided in the helpers are rather self-explanatory, each class and method dealing with only one purpose. All helper classes are not meant to be instantiated (all methods are static), except PdoHelper which MUST be instantiated. The Containers and Formatters class must also be instantiated.

When the package is mentionned in your composer.json, it will automatically require Includes/Init.php which will perform some initialization stuff, such as the default charset, locale and timezones. Currently, if not specified, the errors are displayed in the standard output, and the mb_xxx functions default encoding is set to UTF_8, as this is the most easy way to deal with foreign characters.

You may set other values, by defining the following constants BEFORE including your vendor/autoload.php :

Samples

For most classes, the function names and their parameters are self-explanatory, please refer to the API reference link below.

Sample : CsvFormatter

The Formatters namespace has some classes to help export tabular data.

Currently, only CSV export is implemented, but we could also implement HTML Table export rather simply by subclassing the Formatter class and providing code to it's abstract methods (those methods define how to print lines, rows, separate columns, etc.). For CsvFormatter subclass, the only implementation needed is how to separate columns (in CSV, this is done with ';' character). New rows are written with a newline character.

// we create a file at $PATH
$fhandle = fopen($path, 'w');

// we create the formatter along with an output strategy, here to a file handle
$csv = new CsvFormatter(new FormatterFileOutputStrategy($fhandle));

// beginning export
$csv->newRow();
$csv->row(array('column1 header', 'column2 header', 'column3 header'));
$csv->closeRow();
$csv->newRow();
$csv->row(array('line2_column1_value', 'line3_column2_value', ''));
$csv->closeRow(true);   // true = this is the last row

// closing file handle
fclose($fhandle);

Sample : PdoHelper

PdoHelper is a subclass of PHP Pdo class (instantiation needed, use the same constructor parameters as the Pdo constructor). So you may use any usual method of Pdo (such as prepare and execute).

There are simple functions such as pdo_query or pdo_query_select which prepare AND then execute the request with a single call.

There is a pdo_dbexists method you may use to test whether a value exists for a SQL Select statement, with only one PHP code line (the value is returned) :

if ( $name = $pdoh->pdo_dbexists('SELECT name FROM Client WHERE id=?', array(123456)) )
    echo "found client ; its name is '$name' !";

The main benefit of PdoHelper is it's foreign key querying system. If you define your relationnal schema (just the tables having foreign keys, other tables are useless), you may ask the question "is there a table which has a line with a column referencing a particular foreign key ?". In other words, may I delete safely a row in a table X without breaking a table Y with a column referencing table X and the row deleted ?

To build the schema of tables/foreign keys, you just call addForeignKey method for all tables whoses rows may be referenced (tables being foreign keys to other). For example, in a Town and Client schema, Town is the table being referenced by a idTown column in the Client table.

// defining a schema with 2 tables referencing the Town table through it's idTown column
$pdoh->addForeignKey('Town', 'idTown', ['Client', 'Merchants']);

To safely delete a row in Town table, we need to check that no row in Client or Merchants has a reference to the town being deleted :

$test = $pdoh->pdo_foreignkeys('Town', 1234);
if ( $test['statut'] )
   // no foreign key detected, we may delete safely the town
   echo "deletion is safe";
else
   echo "deletion is not safe : " . $test['cause']['message'];

Sample : SimpleExceptionHandler

If you encounter an exception, you may catch it and call a ExceptionHandler class to format it and send the output to stdout :

try
{
   // some bad code here
}
catch (\Exception $e)
{
   (new \Nettools\Core\ExceptionHandlers\SimpleExceptionHandler())->handleException($e);
   // the script is halted here
}

PHPUnit

To test with PHPUnit, point the -c configuration option to the /phpunit.xml configuration file.