net-tools / core
Composer library providing core functionalities
Installs: 1 022
Dependents: 6
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >= 7.2
Requires (Dev)
- mikey179/vfsstream: ^1.0.0
- phpunit/phpunit: ^9.0.0
- dev-master
- 1.0.82
- 1.0.81
- 1.0.80
- 1.0.79
- 1.0.78
- 1.0.77
- 1.0.76
- 1.0.75
- 1.0.74
- 1.0.73
- 1.0.72
- 1.0.71
- 1.0.70
- 1.0.69
- 1.0.68
- 1.0.67
- 1.0.66
- 1.0.65
- 1.0.64
- 1.0.63
- 1.0.62
- 1.0.61
- 1.0.60
- 1.0.59
- 1.0.58
- 1.0.57
- 1.0.56
- 1.0.55
- 1.0.54
- 1.0.53
- 1.0.52
- 1.0.51
- 1.0.50
- 1.0.49
- 1.0.48
- 1.0.47
- 1.0.46
- 1.0.45
- 1.0.44
- 1.0.43
- 1.0.42
- 1.0.41
- 1.0.40
- 1.0.39
- 1.0.38
- 1.0.37
- 1.0.36
- 1.0.35
- 1.0.34
- 1.0.33
- 1.0.32
- 1.0.31
- 1.0.30
- 1.0.29
- 1.0.28
- 1.0.27
- 1.0.26
- 1.0.25
- 1.0.24
- 1.0.23
- 1.0.22
- 1.0.21
- 1.0.20
- 1.0.19
- 1.0.18
- 1.0.17
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-10-29 20:00:52 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.