net-tools / core
提供核心功能的Composer库
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-08-29 19:38:37 UTC
README
提供PHP核心功能的Composer库
该包包含以下类:
- 容器(缓存、池)
- 格式化器(将数据导出为CSV或其他任何格式 - 目前仅实现CSV导出)
- 助手(帮助处理请求数据、清洗用户数据、编码/解码数据等)
安装说明
要安装net-tools/core包,只需通过composer要求: require net-tools/core:^1.0.0
如何使用?
助手类中提供的类相对直观,每个类和方法只处理一个目的。所有助手类都不需要实例化(所有方法都是静态的),除了PdoHelper必须实例化。容器和格式化器类也必须实例化。
当在composer.json中提及该包时,它将自动要求包含Includes/Init.php
,这将执行一些初始化操作,例如默认字符集、地区和时区。目前,如果没有指定,错误将显示在标准输出中,并且mb_xxx
函数的默认编码设置为UTF_8,这是处理外文字符的最简单方法。
您可以通过在包含vendor/autoload.php之前定义以下常量来设置其他值
示例
对于大多数类,函数名称和其参数都是自解释的,请参阅下面的API参考链接。
示例:CsvFormatter
格式化器命名空间中有一些类可以帮助导出表格数据。
目前,仅实现了CSV导出,但我们可以通过继承Formatter
类并提供抽象方法(这些方法定义了如何打印行、行、分隔列等)来简单地实现HTML表格导出。对于CsvFormatter
子类,唯一需要实现的是如何分隔列(在CSV中,这是使用';'字符完成的)。新行使用换行符写入。
// 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);
示例:PdoHelper
PdoHelper是PHP Pdo类的子类(需要实例化,使用与Pdo构造函数相同的构造函数参数)。因此,您可以使用任何Pdo的常规方法(如prepare
和execute
)。
有一些简单的函数,如pdo_query
或pdo_query_select
,可以在一个调用中准备并执行请求。
有一个pdo_dbexists
方法,您可以使用它来测试SQL Select语句中是否存在值,只需一行PHP代码(值将被返回)
if ( $name = $pdoh->pdo_dbexists('SELECT name FROM Client WHERE id=?', array(123456)) ) echo "found client ; its name is '$name' !";
PdoHelper的主要优点是其外键查询系统。如果您定义了关系模式(只需具有外键的表,其他表都是无用的),您可以询问“是否有表具有引用特定外键的列的行?”换句话说,我是否可以在不破坏引用表X的列Y的情况下安全地删除表X中的行?
要构建表/外键的架构,您只需为可能被引用的行(即其他表的外键)的表调用addForeignKey
方法。例如,在城镇和客户模式中,城镇是城镇表,它在客户表中通过idTown列被引用。
// defining a schema with 2 tables referencing the Town table through it's idTown column $pdoh->addForeignKey('Town', 'idTown', ['Client', 'Merchants']);
要安全地删除城镇表中的行,我们需要检查客户或经销商中没有行引用要删除的城镇
$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'];
示例:SimpleExceptionHandler
如果遇到异常,您可以捕获它并调用ExceptionHandler
类来格式化它并将输出发送到stdout
try { // some bad code here } catch (\Exception $e) { (new \Nettools\Core\ExceptionHandlers\SimpleExceptionHandler())->handleException($e); // the script is halted here }
PHPUnit
要使用PHPUnit进行测试,请将-c配置选项指向/phpunit.xml配置文件。