eftec/mapache-commons

这是一个用于PHP的有用函数集合。名字是一个双关语。

1.24 2024-08-25 00:44 UTC

This package is auto-updated.

Last update: 2024-08-25 00:44:54 UTC


README

这是一个用于PHP的有用函数集合。名字是一个双关语(在西班牙语中“Mapache”是“浣熊”的意思)

Packagist Maintenance composer php php CocoaPods

Mapache Commons
Mapache Commons

目标

这是一组满足以下要求的有用函数

  • 函数不得有依赖(除非它需要php模块)。
  • 函数必须比语法糖更快且内存友好。
  • 函数必须能够静态运行,并且必须是自包含的。
  • 函数必须是通用的,并且必须解决通用问题。例如,不允许计算特定国家增值税的函数。

家族

  • 集合库
  • 文本库
  • 调试库

集合库

类 CollectionLib

方法 isAssoc()

如果数组是关联数组则返回true,如果是索引数组则返回false
示例

$isAssoc=CollectionLib::isAssoc($array); // slow, more precise.
$isAssoc=CollectionLib::isAssoc($array,true); // fast, less precise

参数

  • $array 输入数组(数组)

方法 first()

返回数组的第一个元素。
有时第一个元素不是索引[0],例如 ['key1'=>1,0=2] 其中第一个元素是 'key1' 而不是 0。此函数始终返回正确的值。

参数

  • $array 输入数组(数组)

方法 firstKey()

返回数组的第一个键。

参数

  • $array 输入数组(数组)

方法 arrayKeyLower()

将键的字母大小写更改为小写

参数

  • $array 输入数组(数组)

方法 arrayKeyUpper()

将键的字母大小写更改为小写

参数

  • $array 输入数组(数组)

方法 generateTable()

从数组生成HTML表格

参数

  • $array 输入数组(数组|null)
  • $css 如果为true则使用内置样式。如果为false则不使用样式。如果为字符串则使用类(字符串|bool)

方法 splitOpeningClosing()

使用开标签和闭标签分割字符串,默认为 "(" 和 ")"。
示例

CollectionLib::splitOpeningClosing("a(B,C,D)e(F,G,H)"); // ['a','B,C,D','e','F,G,H']
CollectionLib::splitOpeningClosing("a(B,C,D)e(F,G,H)i"); // ['a','B,C,D','e','F,G,H','i']

参数

  • $text 输入要分割的文本(字符串)
  • $openingTag 开标签,默认为 "("(字符串)
  • $closingTag 闭标签,默认为 ")"(字符串)
  • $startPosition 起始位置(默认为零)(int)
  • $excludeEmpty 如果为true则排除所有空值。 (bool)
  • $includeTag 如果为true则包括标签。 (bool)

方法 splitNotString()

通过忽略字符串中值为 " 或 ' 的部分来分割字符串。
示例

CollectionLib::splitNotString('a,b,"CC,D,E",e,f' ,","); // ['a','b','CC,D,E','e','f']

参数

  • $text 输入文本(字符串)
  • $separator 参数字符串 $separator(字符串)
  • $offset 参数 int $offset(int)
  • $excludeEmpty 参数 bool $excludeEmpty (bool)

方法 arrayChangeKeyCaseRecursive()

它递归地更改数组键的大小写(转换为小写或大写)
示例

$arr=['A'=>'a','b'=>'b'];
CollectionLib::arrayChangeKeyCaseRecursive($arr);
// returns ['a'=>'a','b'=>'b']
CollectionLib::arrayChangeKeyCaseRecursive($arr,true);
// returns ['A'=>'a','B'=>'b']

参数

  • $array 输入数组(数组)
  • $case [可选] 默认为 CASE_LOWER

    可以是 CASE_UPPER 或 CASE_LOWER(默认)

    (int)

方法 arraySearchField()

返回数组/对象中与字段值匹配的第一个(或所有)键的数组
示例

$array=[['name'=>'john'],['name'=>'mary']];
CollectionLib::arraySearchField($array,'name','mary'); // 1
CollectionLib::arraySearchField([(object)['name'=>'john'],(object)['name'=>'mary']],'name','mary'); // 1
CollectionLib::arraySearchField([['name'=>'john'],['name'=>'mary'],['name'=>'mary']],'name','mary',true); // returns [1,2]

参数

  • $array 输入数组(数组)
  • $fieldName 字段索引的名称(string|int)
  • $value 要搜索的值(mixed)
  • $returnAll 如果为 true,则返回所有匹配项。如果为 false,则返回第一个值。(bool)

方法 xmlToString()

将 xml(SimpleXMLElement 对象)转换为字符串
示例

$string=CollectionLib::xmlToString($xml,true); // "<root>...</root>"

参数

  • $xml 参数 SimpleXMLElement $xml(SimpleXMLElement)
  • $format 如果为 true,则结果被格式化。(bool)

方法 arrayToXML()

将数组转换为 xml(SimpleXMLElement 对象)示例:

$xml=CollectionLib::arrayToXML($array,'root'); // <root>...</root>

参数

  • $data 参数 array $data(array)
  • $rootName 根标签的名称(默认为 root)(string)
  • $insidetag 用于使用 PHP 生成的 xml 设置特定条件。(string)

方法 stringToXML()

使用 simplexml_load_string 包括修正将字符串转换为 xml(SimpleXMLElement 对象)
示例

$xml=CollectionLib::stringToXML('<root><item arg="1">a</item><item arg="2">b</item></root>');

参数

  • $string 要转换的值(string)
  • $className 参数 null $className(null)
  • $options libxml 选项(int)
  • $insidetag 默认为 'value_inside'。在某些情况下,解析器在子元素为空时无法生成 XML 属性。2. 解决方案是生成一个新的标签 <_value>2</_value>(string)

方法 xmlToArray()

将 XML 类(SimpleXMLElement 对象)转换为数组。 示例:

$array=CollectionLib::xmlToArray($xml);

参数

  • $xml 参数 SimpleXMLElement $xml(SimpleXMLElement)

调试库

类 Debug

方法 var_dump()

var_dump 的替代方案。它 "pre" 结果或它在 JavaScript 的控制台中显示结果。
示例

DebugLib::var_dump($value,true); // returns a var_dump visible via the console of javascript (browser)

参数

  • $value 参数 $value()
  • $type : 0=normal (
    ), 1=javascript console, 2=table (use future) (int)
  • $returnValue 参数 bool $returnValue(bool)

方法 WriteLog()

写入日志文件。如果文件超过 10mb,则文件被重置。

DebugLib::WriteLog('somefile.txt','warning','it is a warning');
DebugLib::WriteLog('somefile.txt','it is a warning');

参数

  • $logFile 要写入的文件(string)
  • $level 消息级别,例如 "error","info",1 等。(mixed)
  • $txt 如果 txt 为空,则将级别定义为警告,并使用级别进行描述(string|object|array)

文本库

类 TextLib

方法 isUpper()

如果 str 是(完全)大写,则返回 true

参数

  • $str 输入文本(string)

方法 isLower()

如果 str 是(完全)小写,则返回 true

参数

  • $str 参数 $str()

方法 between()

获取一个字符串中的一个文本和另一个文本之间的字符串。 示例:

TextLib::between('Hello Brave World','Hello','World');  // returns " Brave "
TextLib::between('mary has a lamb','has','lamb') // returns ' a '

参数

  • $haystack 参数 string $haystack(string)
  • $startNeedle 要搜索的初始文本
    如果为空,则从 haystack 的开始处开始。(string)
  • $endNeedle 要搜索的结束标签
    如果为空,则从 haystack 的末尾结束(string)
  • $offset 参数 null|int $offset(null|int)
  • $ignoreCase 参数 bool $ignoreCase(bool)

方法 stripQuotes()

删除文本中的引号 " 或 ',如果值在引号之间
如果值未加引号,则不修改。
如果值未正确关闭("hello 或 "hello'),则不删除引号。
值被修剪 ' "hello world"' --> 'hello world'
示例

TextLib::stripQuotes('"hello world"');
// returns hello world

参数

  • $text 参数 $text()

方法 removeParenthesis()

删除初始和结束括号,但只有当两者都匹配时。
如果 $start 和 $end 是数组,则两者必须具有相同的计数,并且数组通过索引配对进行比较
示例

TextLib::removeParenthesis('hello'); // return "hello";
TextLib::removeParenthesis('(hello)'); // return "hello";
TextLib::removeParenthesis('[hello]'
,['(','{','[']
,[')','}',']']); // returns "hello"
TextLib::removeParenthesis("'hello'"
,"'"
,"'"); // returns "hello"

参数

  • $txt 输入值。例如 "hello","(hello)"(string)
  • $start 开放括号,默认为 '('. (string|array)
  • $end 关闭括号,默认为 ')'. (string|array)

方法 replaceBetween()

替换两个针之间的文本
示例

TextLib::replaceBetween('Hello Brave World','Hello','World',' Wayne ') // returns "Hello Wayne World"

参数

  • $haystack 输入值(string)
  • $startNeedle 要搜索的初始文本
    如果为空,则从 haystack 的开始处开始。(string)
  • $endNeedle 要搜索的结束标签
    如果为空,则从 haystack 的末尾结束(string)
  • $replaceText 要替换的文本(string)
  • $offset 开始搜索的偏移位置(null|int)
  • $replaceTag 如果为 true,则还替换标签(bool)

方法 removeFirstChars()

从字符串中移除第一个字符(或多个字符)
示例

TextLib::removeFirstChars('Hello') // returns "ello"

参数

  • $str 输入文本(字符串)
  • $length 要移除的字符数量(默认为1)(整型)

方法 removeLastChars()

从字符串中移除最后一个字符(或多个字符)
示例

TextLib::removeLastChars('Hello') // returns "Hell"

参数

  • $str 输入文本(字符串)
  • $length 要移除的字符数量(默认为1)(整型)

方法 getArgument()

它将参数与值分开到设置的值中。
返回一个包含参数名称和值(如有)的数组。它总是返回一个二维数组 示例:

self::getArgument("arg=200"); // returns ["arg","200"]
self::getArgument("arg:200",':'); // returns ["arg","200"]

参数

  • $str 输入文本(字符串)
  • $set 操作符的分隔符(字符串)
  • $trimValue 参数 bool $trimValue(布尔型)

方法 strPosNotSpace()

它返回字符串中的第一个非空格位置。 示例:

TextLib::strPosNotSpace('   abc  def'); // returns 3

参数

  • $str 输入字符串(字符串)
  • $offset 偏移位置(整型)
  • $charlist 被视为空格的字符列表(字符串)

方法 strposArray()

它查找文本的第一个(或最后一个)出现。
与 strpos() 不同,此方法允许找到多个针。
示例

TextLib::strposArray('a,b.d.e,f.g',['x','t','.']); // return 3
TextLib::strposArray('a,b.d.e,f.g',['x','t',','],0,true); // return 7

参数

  • $haystack 输入值(string)
  • $needles 要查找的值(或值)(字符串或数组)
  • $offset 偏移位置(初始为0)(整型)
  • $last 如果为 false(默认)则返回第一个出现。如果为 true 返回最后一个(布尔型)

方法 parseArg()

将文本 = 'a1=1,a2=2' 转换为关联数组
使用 parse_str() 方法进行转换
注意: 它不适用于引号或双引号。a1="aa,bb",bb=30 不起作用 示例:

TextLib::parseArg('a=1,b=1'); // returns ['a'=>'1','b'=>'1']

参数

  • $text 包含初始值的输入字符串(字符串)
  • $separator 分隔符(字符串)

方法 parseArg2()

与 parseArg() 相同,但速度慢3倍。
它还考虑引号和双引号。
示例

TextLib::parseArg2("a1=1,a2=2,a3="aa,bb"); // ["a1"=>1,"a2"=>2,"a3"=>""aa,bb""]
TextLib::parseArg("a1=1,a2=2,a3="aa,bb"); // ["a1"=>1,"a2"=>2,"a3"=>""aa","bb""=>""]

参数

  • $text 包含初始值的输入字符串(字符串)
  • $separator 分隔符。它不分隔引号或双引号内的文本。(字符串)

方法 naturalArg()

解析自然字符串并返回声明数组
“自然字符串”,它是一组由空格分隔的值或参数,其中值是索引,新值是索引的值。

TextLib::naturalArg('select * from table where 1=1'
,['select'=>'req','from'=>'req','where'=>'opt']);
// returns ['select'=>'*','from'=>'table','where'=>'1=1']
TextLib::naturalArg('item export table inport file'
,['item'=>'first','export'=>'opt','inport'=>'opt']);
// returns: ['item' => 'item', 'export' => 'table', 'inport' => 'file']

参数

  • $txt 输入值。例如 "somevalue TYPE int LENGHT 30"(字符串)
  • $separators 每个字段的指示器。
    first = 表示第一个元素(可选)
    opt = 表示字段是可选的
    req = 表示字段是必需的
    (数组)

方法 str_replace_ex()

它类似于 str_replace,但它还允许限制替换次数。

参数

  • $search 参数字符串 $search(字符串)
  • $replace 参数字符串 $replace(字符串)
  • $subject 参数字符串 $subject(字符串)
  • $limit 参数整型 $limit(整型)

方法 wildCardComparison()

与通配符 (*) 进行比较,如果两个字符串相等则返回 true
通配符仅在字符串的开始或结尾处起作用。
示例:

TextLib::wildCardComparison('abcdef','abc*'); // true
TextLib::wildCardComparison('abcdef','*def'); // true
TextLib::wildCardComparison('abcdef','*abc*'); // true
TextLib::wildCardComparison('abcdef','*cde*'); // true
TextLib::wildCardComparison('abcdef','*cde'); // false

参数

  • $text 参数字符串 $text(字符串)
  • $textWithWildcard 参数字符串|null $textWithWildcard(字符串|null)

方法 endsWith()

如果 $string 以 $endString 结尾,则返回 true
示例:

TextLib::endsWidth('hello world','world'); // true

参数

  • $string 参数 $string ()
  • $endString 参数 $endString ()

方法 replaceCurlyVariable()

将所有在 {{ }} 之间定义的变量替换为值字典中的变量。
示例
replaceCurlyVariable('hello={{var}}',['var'=>'world']) // hello=world
replaceCurlyVariable('hello={{var}}',['varx'=>'world']) // hello=
replaceCurlyVariable('hello={{var}}',['varx'=>'world'],true) // hello={{var}}

参数

  • $string 输入值。它可以包含定义为 {{namevar}} 的变量(字符串)
  • $values 值字典。(数组)
  • $notFoundThenKeep [false] 如果为 true 且值未找到,则保留值。否则,用空值替换(布尔型)

方法 addParenthesis()

在文本的开始和结束处添加括号(或其他符号)。如果已经存在,则不会添加。
示例

TextLib::addParenthesis('hello'); // return '(hello)';
TextLib::addParenthesis('(hello)');// return '(hello)';

参数

  • $txt 输入值。例如 "hello","(hello)"(string)
  • $start 开放括号,默认为 '('. (string|array)
  • $end 关闭括号,默认为 ')'. (string|array)

方法 hasParenthesis()

如果文本有开头的和结束的括号(或其他符号),则返回 true
示例

TextLib::hasParenthesis('hello'); // return false;
TextLib::hasParenthesis('(hello)'); // return true;

参数

  • $txt 输入值。例如 "hello","(hello)"(string)
  • $start 开放括号,默认为 '('. (string|array)
  • $end 关闭括号,默认为 ')'. (string|array)

方法 camelCase()

保留小写,除了第一个字母被转换为小写之外
如果文本包含字符 "" 或 " ",则下一个字符是大写
如果文本不包含任何字符 "
" 或 " ",则只有第一个字符被替换。示例:

TextLib::camelCase('HelloWorld'); // return "helloWorld";
TextLib::camelCase('hello_world'); // return "helloWorld";

参数

  • $txt 输入值(字符串)

文件库

类文件
该类包含与文件和目录交互的函数。

字段 lastError ()

方法 getDirFiles()

它获取目录的内容(文件)。它不包括其他目录。

参数

  • $dir 要扫描的目录。(字符串)
  • $extensions 要查找的扩展名(不带点)。['*'] 表示任何扩展名。(数组)
  • $recursive 如果为真(默认),则递归扫描文件夹。(布尔值)

方法 getDirFirstFile()

它返回目录中找到的第一个文件(排除目录)与特定扩展名匹配。
扩展名的顺序可能很重要。
如果有两个具有相同扩展名的文件,则返回第一个。

参数

  • $dir 要扫描的目录。(字符串)
  • $extensions 要查找的扩展名(不带点)。['*'] 表示任何扩展名。(数组)
  • $sort(默认为false),如果为真,则在过滤之前对列表进行排序。(布尔值)
  • $descending(默认为false),如果为真,则在启用排序的情况下按降序排序。(布尔值)

方法 getDirFolders()

它获取目录的内容(文件夹)而不包含尾部斜杠。它不包括文件。

参数

  • $dir 要扫描的目录。(字符串)
  • $recursive 如果为真(默认),则递归扫描文件夹。(布尔值)

方法 getExtensionPath()

它获取文件的扩展名(不带点)。如果没有扩展名,则返回空字符串。

参数

  • $fullPath 要分析的路径。(字符串)

方法 getFileNamePath()

它获取文件名(不带扩展名和目录)

参数

  • $fullPath 要分析的路径。(字符串)

方法 getBaseNamePath()

它获取基本名称(带扩展名的文件名)。它不包括目录。

参数

  • $fullPath 要分析的路径。(字符串)

方法 getDirPath()

它从完整路径中获取目录(不带尾部斜杠)

参数

  • $fullPath 要分析的路径。(字符串)

方法 fixUrlSeparator()

它通过将斜杠和反向斜杠转换为系统文件夹分隔符来修复路径

参数

  • $fullPath 要分析的路径。(字符串)

方法 safeFileGetContent()

它获取文件内容。它从不抛出异常
如果出现错误,则返回默认值

参数

  • $filename 要打开的文件名。(字符串)
  • $use_include_path 用于触发包含路径搜索。(布尔值)
  • $context 使用(null)创建的有效上下文资源。
  • $offset 读取开始的偏移量。(整数)
  • $length 读取数据的最大长度。默认情况下,读取直到文件末尾。(整数|null)
  • $default 如果无法打开文件则返回的值(null|混合值)

方法 safeFilePutContent()

将字符串写入文件。此操作符是安全的,它从不抛出异常。

参数

  • $filename 要写入数据的文件路径。(字符串)
  • $data 要写入的数据。可以是字符串、数组或流资源。(混合值)
  • $flag 标志的值可以是以下标志的任何组合,带有一些限制,通过二进制 OR(|)运算符连接。(整数)
  • $context 使用 stream_context_create 创建的有效上下文资源。(混合值)
  • $tries 尝试的次数(默认3)。每次尝试有300ms的延迟。(整数)

方法 isAbsolutePath()

如果路径是绝对路径,则返回 true,否则返回 false。
此函数在 Linux 和 Windows(以及很可能在其他 UNIX 操作系统中)上工作。

参数

  • $fullPath 要分析的路径。(字符串)

版本列表

  • 1.24
    • 修复了与 FileLib 相关的异常。它还将在字段中存储每个错误。
    • 完成 README.md
    • 清理 PHPDocs。
  • 1.23 修改 .gitattributes 和 .gitignore 以简化最终版本。
  • 1.22 以下类已被重命名
    • Text(已弃用)=> TextLib
    • Collection(已弃用)=> CollectionLib
    • Debug(已弃用)=> DebugLib
    • 这是因为有一打库使用相同的类名。
    • 您仍然可以使用相同的库而无需更改,但对于新代码,我建议使用新名称。
    • [new] FileLib::getDirFirstFile()
  • 1.21 添加 FileLib 类
    • getDirFiles()
    • getDirFolders()
    • getExtensionPath()
    • getDirPath()
    • getBaseNamePath()
    • getFileNamePath()
    • fixUrlSeparator()
    • safeFileGetContent()
    • safeFilePutContent()
    • isAbsolutePath()
  • 1.20 新方法 CollectionLib::xmlToString, CollectionLib::arrayToXML, CollectionLib::stringToXML, CollectionLib::xmlToArray
  • 1.17 新方法 TextLib::str_replace_ex()
  • 1.16 新方法 TextLib::wildcardComparison() 和 TextLib::endsWith()
  • 1.15 新方法 TextLib::parseArg2()
  • 1.14 TextLib::camelCase() 解决了一个小问题
  • 1.13 TextLib::replaceCurlyVariable() 更新
  • 1.12
    • Collection:splitOpeningClosing 添加参数 $includeTag
  • 1.11
    • TextLib::replaceCurlyVariable 添加方法
  • 1.10
    • TextLib::strPosNotSpace() 添加参数 $charlist
  • 1.9 2019-12-09
    • TextLib::replacetext() 如果缺少结束标签,它不会崩溃。
    • TextLib::replacetext() 添加为新参数
  • 1.8 2019-12-04
    • TextLib::between() 现在允许空 $startNeedle 和 $endNeedle
  • 1.7 2019-12-04 新方法
    • TextLib::addParenthesis()
    • TextLib::hasParenthesis()
  • 1.6 2019-12-04 新方法
    • TextLib::parseArg()
    • TextLib::naturalArg()
    • TextLib::strposArray()
    • TextLib::camelCase()
    • TextLib::removeParenthesis()
    • CollectionLib::arrayChangeKeyCaseRecursive()
    • CollectionLib::arraySearchField()
  • 1.5 2019-03-10 新函数
    Collection:splitOpeningClosing()
    TextLib::strPosNotSpace()
    TextLib::getArgument()
    CollectionLib::splitNotString()
  • 1.4 2019-02-16 新函数 TextLib::removeFirstChars(), TextLib::removeLastChars()
  • 1.3 2019-02-16 添加新方法和单元测试。
  • 1.2 2018-10-27 类集合中的一些更改。
  • 1.0 2018-09-18 第一个版本

许可证

Apache-2.0.