ride/lib-common

Ride框架共享类

1.2.1 2023-10-09 12:35 UTC

README

PHP Ride框架的共享类。

库中包含的内容

装饰器

装饰器用于将值从一种上下文转换为另一种上下文。它们仅应在传入的值可处理时才起作用。

查看此代码示例

<?php

use ride\library\decorator\DateFormatDecorator;
use ride\library\decorator\StorageSizeDecorator;
use ride\library\decorator\VariableDecorator;

// decorate dates into a formatted date
$decorator = new DateFormatDecorator();
$decorator->setDateFormat('y-m-d');
$result = $decorator->decorate(1372582573); // 2013-06-30
$result = $decorator->decorate(new DateTime('30 June 2013')); // 2013-06-30

// decorate byte values into human readable format
$decorator = new StorageSizeDecorator();
$result = $decorator->decorate(5000); // 4.88 Kb

// decorate variables into a output string
$decorator = new VariableDecorator();
$result = $decorator->decorate(null); // 'null'
$result = $decorator->decorate(true); // 'true'
$result = $decorator->decorate(array('key' => 'value')); // '["key" => "value"]'
$result = $decorator->decorate($decorator); // 'ride\library\decorator\VariableDecorator'

自动加载器

Ride框架的自动加载器是一个PSR-0自动加载器。

它可以处理以下类名:

  • ride\library\Autoloader: 检查为 ride/library/Autoloader.php
  • ride_library_Autoloader: 检查为 ride/library/Autoloader.phpride_library_Autoloader.php

查看此代码示例

<?php

use ride\library\Autoloader;
use ride\library\StringHelper;

require_once('path/to/ride/library/Autoloader.php');

$autoloader = new Autoloader();
$autoloader->addIncludePath('module1/src');
$autoloader->addIncludePath('module2/src');
$autoloader->addIncludePath('application/src'); // last added path will be checked first
$autoloader->registerAutoloader();

// go and use some classes
$string = StringHelper::generate();

错误处理器

Ride框架的错误处理器将可处理的错误转换为异常。

查看此代码示例

<?php

use ride\library\ErrorHandler;

$errorHandler = new ErrorHandler();
$errorHandler->registerErrorHandler();

try {
    $tokens = explode(null);
} catch (Exception $e) {
    // ErrorException thrown
}

字符串助手

当处理值时,字符串助手非常有用。

<?php

use ride\library\StringHelper;

$string = "Let's create a stràngé STRING";
$result = StringHelper::safeString($string); // 'lets-create-a-strange-string'
$result = StringHelper::safeString($string, '_', false); // 'Lets_create_a_strange_STRING'
$result = StringHelper::startsWith($string, array('yes', 'no')); // false
$result = StringHelper::startsWith($string, array('yes', 'no', 'Let')); // true
$result = StringHelper::startsWith($string, 'let'); // false
$result = StringHelper::startsWith($string, 'let', true); // true
$result = StringHelper::truncate($string, 12); // 'Let's...'
$result = StringHelper::truncate($string, 12, '...', true); // 'Let's cre...'
$result = StringHelper::generate(10); // a random string of 10 characters

计时器

计时器可以用来追踪动作的时间。它具有微秒级的详细信息。

<?php

use ride\library\Timer;

$timer = new Timer();
$timer->reset();
$time = $timer->getTime(); // get the current time and keep on going
$time = $timer->getTime(true); // get the current time and reset

安装

您可以使用Composer安装此库。

composer require ride/lib-common