nekland/tools

一些用于提高PHP工作效率的工具

2.6.2 2023-03-06 11:05 UTC

This package is auto-updated.

Last update: 2024-09-06 14:10:09 UTC


README

Build Status

一些帮助在PHP中编码的类。无依赖。高质量代码。

此仓库遵循semver。

安装

composer require "nekland/tools"

参考

此库提供了一些工具来帮助您进行开发。

以下是它提供的工具列表

StringTools类

编码参数是可选的。

::camelize

StringTools::camelize($str, $from, $encoding, $normalize = true) : string
  • $str 输入字符串
  • $from (可选,默认 "_") 输入字符串格式(可以是 "-" 或 "_")
  • $encoding (可选,默认 "UTF-8") 输入字符串的编码
  • $normalize 决定你是否想要规范化(移除特殊字符),在驼峰命名时通常需要这样做

::startsWith

判断给定的字符串是否以指定的字符串开头。

StringTools::startsWith($str, $start) : bool
  • $str 输入字符串
  • $start 应该开始的字符串

::endsWith

判断给定的字符串是否以指定的字符串结尾。

StringTools::endsWith($str, $end) : bool
  • $str 输入字符串
  • $end 应该结尾的字符串

::removeStart

如果字符串以给定的字符串开头,则从字符串中移除开头部分。

StringTools::removeStart($str, $toRemove) : string
  • $str 输入字符串
  • $toRemove 要从 $str 的开头移除的字符串

::removeEnd

如果字符串以给定的字符串结尾,则从字符串中移除结尾部分。

StringTools::removeEnd($str, $toRemove) : string
  • $str 输入字符串
  • $toRemove 要从 $str 的结尾移除的字符串

::contains

StringTools::contains($str, $needle) : bool
  • $str 输入字符串
  • $needle 可能包含的字符串

::mb_ucfirst

ucfirst 标准函数添加缺失的多字节PHP函数。

StringTools::mb_ucfirst($str, $encoding) : string
  • $str 输入字符串
  • $encoding (可选,默认 "UTF-8") 输入字符串的编码

ArrayTools类

::removeValue

ArrayTools::removeValue($array, $value) : void
  • $array 通过引用传递的输入数组
  • $value 从 $array 中移除的值

EqualableInterface

帮助您以类似于 java 中的方式对对象进行相等性比较。

equals

必须实现此方法以检查传入的对象是否相等。

DateTimeComparator类

对于以下方法 lowestgreatest,您可以提供无限多个 DateTimeInterface 对象。请注意,DateTimeInterface对象将被函数忽略。

::greatest

比较参数中的 \DateTimeInterface 并返回最大的一个

DateTimeComparator::greatest($dateTime1, $dateTime2, $dateTime3, ...) : ?\DateTimeInterface

::lowest

比较参数中的 \DateTimeInterface 并返回最小的一个

DateTimeComparator::lowest($dateTime1, $dateTime2, $dateTime3, ...) : ?\DateTimeInterface

临时文件管理

TemporaryFile 帮助您轻松创建临时文件。

::__construct()

TemporaryFile::__construct(TemporaryDirectory $dir = null, string $prefix = '')

示例

// Create a file in temporary folder
$file = new TemporaryFile();

// Create a file inside a temporary directory (see TemporaryDirectory class)
$file = new TemporaryFile($temporaryDir);

// Create a file in a temporary folder with php_ prefix.
$file = new TemporaryFile(null, 'php_');

::setContent & ::getContent

TemporaryFile::setContent(string $content)
TemporaryFile::getContent(): string

::getPathname()

返回文件的完整路径(例如:/tmp/generated-folder/filename

TemporaryFile::getPathname(): string

::remove()

从文件系统中删除文件。

TemporaryFile::remove()

临时目录管理

::__construct()

TemporaryDirectory::__construct(string $dir = null, string $prefix = 'phpgenerated')

示例

// create a new directory
$directory = new TemporaryDirectory();

::getTemporaryFile()

从生成的目录创建一个 TemporaryFile

TemporaryDirectory::getTemporaryFile(): TemporaryFile

::remove()

删除目录。

TemporaryDirectory::remove(bool $force = false): void

如果指定 force 为 true,即使目录中有文件,也会删除目录。

::getPathname()

返回文件夹的完整路径(例如:/tmp/folder-name

TemporaryDirectory::getPathname(): string