承接 semelapavel/php-util 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

semelapavel/php-util

Composer 安装命令:

composer require semelapavel/php-util

包简介

PHP Utils for easier life.

README 文档

README

Latest Stable Version License

About

PHP-Util library is a set of useful PHP classes that make life easier.

Installation

Use the package manager composer to install php-util.

composer require semelapavel/php-util

Table of contents

Object

Object\ClassLoader

A simple PSR-4 autoloader that loads files with required classes using namespaces.

$classLoader = new \SemelaPavel\Object\ClassLoader();
$classLoader->addNamespace('MyNamespace', '/src/myApp');
$classLoader->addDirectory('/src/other');
$classLoader->register();

In this example, classLoader will try to find each file with a fully-qualified class name starting with the prefix MyNamespace in the /src/myApp directory: \MyNamespace\Class should be found as /src/myApp/Class.php.

If the file cannot not be found, or if it does not have MyNamespace prefix, the classloader will try to find a file by the fully-qualified class name in /src/other directory: \MyNamespace\Class should be found as /src/other/MyNamespace/Class.php.

Object\Byte

This class wraps an integer value and represents it as a binary byte.

$byte = new Byte(1024);
echo $byte;                      // 1024
echo $byte->floatValue('KB');    // 1
echo $byte->floatValue('MB', 5); // 0,00098

Byte of any value and its specified binary unit:

$byte = Byte::from(2.5, 'MB'); // 2621440

Easy parsing of php.ini values:

$byte = Byte::fromPhpIniNotation(ini_get('upload_max_filesize'));

Parsing byte value from any string:

$byte = Byte::parse('8');     // 8
$byte = Byte::parse('16B');   // 16
$byte = Byte::parse('1 KiB'); // 1024
$byte = Byte::parse('0,5MB'); // 524288

File

The File namespace contains a set of classes for working with files.

File\File

An instance of this class represents a file in the file system. The file does not need to exist, or be readable when creating a File object. This class extends \SplFileInfo class from Standard PHP Library (SPL).

$file = new File('file.txt');
echo $file->getMimeType();       // 'text/plain'
$content = $file->getContents(); // Reads the file and returns its contents as a string.

Check if the file name is safe and valid for most used operating systems:

$file->hasValidName(); // Returns true in this example.

and without creating an object:

File::isValidFileName('file.txt/'); // false

Strip the ASCII control characters, whitespaces, slashes, dots and backslashes from the end of file name:

File::rtrimFileName('file.txt/'); // Returns 'file.txt' in this example.

File\FileFilter

An instance of this class helps filter out unwanted files by file names using shell wildcards or a regular expression, files with a file size out of set size or range and files with specific date and time or date-time range.

$filter = new FileFilter();
$filter->setFileNameWhiteList(['*.jpg', '*.png', '*.gif']);
$filter->setFileNameBlackList(['*.php.*']);
$filter->setFileNameRegex(new \SemelaPavel\String\RegexPattern('^[^0-9]*$'));
$filter->setFileSize('>1 KB < 1 MB');
$filter->setMTime('>= 2021-01-01 < 2021-01-02 12:00');

Lets have image.jpg with file size 4 KB and modif. time 2021-01-01:

$filter->fileNameMatch('image.jpg')        // true
$filter->compareFileSize(4096);            // true
$filter->compareMTime('2021-01-01 13:37'); // true

Http

The Http namespace contains a set of classes handling requests and responses over HTTP.

Http\FileUpload

The FileUpload provides basic functionality for retrieving normalized file upload data for further processing. Each leaf of the files array is an instance of UploadedFile or null if error UPLOAD_ERR_NO_FILE occured.

See simplified code below:

<form action="" method="post" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="file"><br>
    Select file to upload:
    <input type="file" name="filesArray[]"><br>
    Select file to upload:
    <input type="file" name="filesArray[]"><br>
    <input type="submit" value="Upload files" name="upload_submit">
</form>
$upload = new FileUpload();
$files = $upload->getUploadedFiles();

if ($files['file']) {
    $files['file']->move(dirname(__DIR__) . '/upload/', 'newFile.txt'); // move the file with rename
}

if ($files['filesArray'][0]) {
    $files['filesArray'][0]->move(dirname(__DIR__) . '/upload/');
}

Pagination

A set of useful classes for managing pagination of your web pages.

Pagination\Paginator

Simple pagination calculator to help with calculate number of pages for specific number of items, number of items per page, offset etc. See example below with total number of items set to 100, items per page set to 5 and current page set to 5.

$paginator = new Paginator(100, 5, 5);

$paginator->getCurrentPage();       // 5
$paginator->getNumOfPages();        // 20
$paginator->getCurrentPageLength(); // 5 = number of page items
$paginator->getOffset();            // 20 = current page contains items nr. 21-25
$paginator->getFirstPage();         // 1
$paginator->getLastPage();          // 20 
$paginator->isFirst();              // false 
$paginator->isLast();               // false
$paginator->getNextPage();          // 6
$paginator->getPrevPage();          // 4

Pagination\Pagination

Paginator extension that adds method to get an array of pages for advanced pagination. See example below with total number of items set to 100, items per page set to 5 and current page set to 5.

$pagination = new Pagination(100, 5, 5);
$pages = $pagination->toArray(1, 2);

The $pages array structure will looks like:

[
    ['page' => 1, 'isCurrent' => false],
    ['page' => null, 'isCurrent' => false],
    ['page' => 3, 'isCurrent' => false],
    ['page' => 4, 'isCurrent' => false],
    ['page' => 5, 'isCurrent' => true],
    ['page' => 6, 'isCurrent' => false],
    ['page' => 7, 'isCurrent' => false],
    ['page' => null, 'isCurrent' => false],
    ['page' => 20, 'isCurrent' => false]
]   

String

String\RegexPattern

This class represents a regular expression pattern.

Simple pattern example:

$pattern = new RegexPattern('[a-z]{3}', RegexPattern::CASE_INSENSITIVE);

if ($pattern->isValid()) {
    \preg_match((string) $pattern, 'aBc');  // 1
    $pattern->match('aBc');                 // true
}

Pattern with bind example:

$pattern = new RegexPattern('[a-z]{3}bind', 0, array('bind' => '.value'));
echo $pattern; // ~[a-z]{3}\.value~

Time

Time\Holidays

ArrayAccess class which handles holidays and provides some extra functionality.

$holidays = new Holidays();
$holidays[Holidays::goodFriday(2021)] = "Good Friday";
$holidays[Holidays::easterMonday(2021)] = "Easter Monday";
$holidays["2021-05-01"] = "May Day";

echo $holidays['2021-05-01']; // "May Day"

foreach ($holidays->toArray() as $date => $description) { 
    echo $date . ': ' . $description . '<br>';
}

Time\Calendar

This class helps with date calculations.

$today = new \DateTime();
Calendar::isDayOff($today);
$prevWorkday = Calendar::prevWorkday($today);
$nextWorkday = Calendar::nextWorkday($today);
$lastDayOfPrevMonth = Calendar::lastDayOfPrevMonth($today);
$lastDayOfMonth = Calendar::lastDayOfMonth($today);

With Holidays object set:

$holidays = new Holidays();
$holidays["2021-05-01"] = "May Day";

$today = new \DateTime();
Calendar::isDayOff($today, holidays);
$prevWorkday = Calendar::prevWorkday($today, holidays);
$nextWorkday = Calendar::nextWorkday($today, holidays);

Time\LocalDateTime

This class is a DateTime factory. All functions in this class use default time zone.

LocalDateTime::setLocalTimeZone(new \DateTimeZone('Europe/Prague'));

$dateTimeStr = ' 2020-  07 - 06 T 13 :37 : 00 . 001337  + 02: 00 '; 
$normalizedDateTimeStr = LocalDateTime::normalize($dateTimeStr); // "2020-07-06T13:37:00.001337+02:00"

$localDateTime = LocalDateTime::parse($normalizedDateTimeStr);
$now = LocalDateTime::now();
$today = LocalDateTime::today();

$now->format(LocalDateTime::SQL_DATETIME)  // 'Y-m-d H:i:s' format

semelapavel/php-util 适用场景与选型建议

semelapavel/php-util 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 09 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「pagination」 「class loader」 「regex」 「file upload」 「date time」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 semelapavel/php-util 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 semelapavel/php-util 我们能提供哪些服务?
定制开发 / 二次开发

基于 semelapavel/php-util 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 24
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-01