robert-b/utils

此包的最新版本(dev-master)没有可用的许可证信息。

Laravel 4/5 辅助方法

此包的官方仓库似乎已丢失,因此该包已被冻结。

dev-master 2015-08-20 08:58 UTC

This package is not auto-updated.

Last update: 2019-02-27 16:23:04 UTC


README

这是一个用于个人使用的包,但任何人都可以获取副本并尝试使用。

此包主要是为了Laravel 4和5的使用而创建的,包含一些有用的方法和一组图像缩放方法。

如何安装

  1. 安装 robert-b/utils

    $ composer require "robert-b/utils:1.0.0"
  2. 更新 config/app.php(或Laravel 4的 app/config/app.php)以激活Utils包

    # Add `UtilsServiceProvider` to the `providers` array
    'providers' => array(
        ...
        'RobertB\Utils\UtilsServiceProvider',
    )
  3. (仅限Laravel 5)在您的代码中使用Utils包。

    <?php
    
    use RobertB\Utils\Utils;
    
    class AwesomeController extends Controller {
        ...
    }
    ?>

方法

v1.0.1

inflectorPlural

接受一个字符串作为输入参数,一个单词,并返回传入单词的复数形式,或者如果它已经是复数形式,则返回相同的单词。

用法

$plural = Utils::inflectorPlural("mouse");
// $plural = "mice"

inflectorSingular

接受一个字符串作为输入参数,一个单词,并返回传入单词的单数形式,或者如果它已经是单数形式,则返回相同的单词。

用法

$singular = Utils::inflectorPlural("women");
// $singular = "woman"

inflectorOrdinal

将传入的数字转换为它的序数英语形式。

用法

$ordinal = Utils::inflectorOrdinal(10);
// $ordinal = "10th"
$ordinal = Utils::inflectorOrdinal(2);
// $ordinal = "2nd"

v1.0.0

generateCode

将根据传入的选项数组生成随机代码。

可用选项

  • seed: (array, 可选, 默认 0 through 9) 代码将形成的元素数组。
  • size: (int, 可选, 默认 8) 代码的字符大小
  • prefix: (string, 可选, 默认 null) 生成代码的前缀。将添加到大小。
  • suffix: (string, 可选, 默认 null) 生成代码的后缀。将添加到大小。
  • table: (string, 可选, 默认 null) 检查代码是否存在的数据表名称。提高随机性
  • field: (string, 可选/如果设置了 'table',则为必需, 默认 null) 保存代码的上述表中的字段名称。

用法

$code = Utils::generateCode(['table'=>'codes', 'field'=>'code', 'size'=>16]);
$code = Utils::generateCode(['size'=>32]);

purify

将清理字符串中的所有奇数和特殊字符。通常用于生成数据库条目的URL,但也可以用于在需要时在数据库中保持干净的可搜索文本。

参数

  • str: (string, 必需, 默认 null) 需要紧急清洁的字符串
  • replacer: (string, 可选, 默认 _) 用以替换特殊字符的字符。

用法

$string = "Let's clean, this-puppy up!";
$clean = Utils::purify($string, '-');
// $clean = "let-s-clean-this-puppy-up"

packData

打包字符串以进行传输,使其不便于人类阅读。通常用于表单隐藏元素,以便窥探者不知道您正在发送什么。它不是诺克斯堡,但它可以对付业余爱好者。

用法

$data = "Some random data";
$pack = Utils::packData($data);
// $pack = "U29tZSByYW5kb20gZGF0YQ=="

unpackData

packData 函数相反。

用法

$data = "U29tZSByYW5kb20gZGF0YQ==";
$unpacked = Utils::unpackData($data);
// $unpacked = "Some random data";

fuzzyDate

将时间戳格式化的日期转换为模糊字符串解释。例如:“2分钟前”,“1小时后”。

用法

$now = strtotime("now");
$an_hour_ago = strotime("-1 hour");
$in_an_hour = strotime("+3 months");

$now_fuzzy = Utils::fuzzyDate($now); // a few seconds ago
$an_hour_ago_fuzzy = Utils::fuzzyDate($an_hour_ago); // 1 hour ago
$in_an_hour_fuzzy = Utils::fuzzyDate($in_an_hour); // in 1 hour

formPrep

主要用于清理用于数据库条目的文本的方法。将标签和字符转换为它们的特殊实体对应物。

参数

  • str (string, 必需, 默认 null) 需要清理的字符串。

用法

$content = "<p>Lorem ipsum's sit & dolor.";
$clean = Utils::formPrep($content); // "&lt;p&gt;Lorem ipsum&#039;s sit &amp; dolor."

restoreTags

用于反转部分已准备好的文本的方法。这将恢复标签。

用法

$content = "&lt;p&gt;Lorem ipsum&#039;s sit &amp; dolor.";
$restored = Utils::restoreTags($content); // "<p>Lorem ipsum&#039;s sit &amp; dolor."

stopWords

用于从大量文本中提取单词列表,而不包含英语停用词。

用法

$content = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";
$clean = Utils::stopWords($content) 
//lorem ipsum simply dummy text printing typesetting industry industry's standard dummy text 1500s

使用Utils进行图像缩放

简单的图像缩放方法利用PHP中可用的GD函数来旋转、缩放和裁剪传递给方法的任何本地图像。

用法

$image = "/tmp/original.png";
$save_to = "/tmp/thumb_100.png";

// Load up the original image
$resize = new Utils();
$resize->sirLoad($image);
$resize->sirResizeToWidth(100);
$resize->sirSave($save_to);

可用方法

sirLoad - 用于将原始文件加载到缩放器中

参数

  • path (字符串,必需) 本地文件的路径

sirResizeToWidth - 用于将加载的文件缩放到指定的宽度

参数

  • width (整数,必需) 需要缩放到的目标宽度。

sirResizeToHeight - 用于将加载的文件缩放到指定的高度

参数

  • heigth (整数,必需) 需要缩放到的目标高度。

sirScale - 用于将加载的文件放大或缩小

参数

  • scale (整数,必需) 图像的新缩放比例

sirResize - 用于将加载的图像缩放到指定的宽度和高度

参数

  • width (整数,必需) 需要缩放到的目标宽度。
  • heigth (整数,必需) 需要缩放到的目标高度。

用法

$resize->sirResize(100, 100);

许可

此Utils软件包是免费软件,根据MIT许可证发布。有关详细信息,请参阅LICENSE.txt