borisguery/inflexible

字符串、日期、数字等的屈折变化器集合

dev-master 2013-06-17 12:50 UTC

This package is not auto-updated.

Last update: 2024-09-14 11:57:06 UTC


README

目录

  1. 描述
  2. 安装
  3. 用法
  4. 可用的屈折变化器
  5. 日期时间 1. 相对时间
  6. 数字 1. 人类字节 2. 序数化 3. 缩短 4. 文本化
  7. 字符串 1. 驼峰式 2. 去除命名空间 3. 人性化 4. 仅命名空间 5. URL化
  8. 运行测试
  9. 贡献
  10. 需求
  11. 作者
  12. 许可

描述

Inflexible旨在将常用的屈折变化器集合到一个库中。

安装

使用Composer,只需执行$ composer require borisguery/inflexible即可安装包或

{
  "require": {
    "borisguery/inflexible": "dev-master"
  }
}

用法

可用的屈折变化器

日期时间

相对时间

DateTime对象或秒数转换为最合适的单位

Inflexible::relativeDatetime(86400);

返回

array(
    1,
    'day'
)

您还可以从给定日期获取相对日期时间

Inflexible::relativeDatetime(new DateTime('2012-01-10'), new DateTime('2012-01-17'));

返回

array(
    1,
    'week'
)

可用的单位有

  • 分钟
  • 小时

数字

人类字节

将字节转换为人类可读的表示形式,转换为最合适的单位

Inflexible::humanByte(1024);
// 1.00 KB
Inflexible::humanByte(1048576);
// 1.00 MB
Inflexible::humanByte(1073741824);
// 1.00 GB

您还可以提供一个可选的精度作为第二个参数(默认为2)

序数化

将数字转换为它的序数英文形式

Inflexible::ordinalize(1);
// 1st
Inflexible::ordinalize(13);
// 13th
缩短

使用SI单位(k、M、G等)格式化数字

Inflexible::shorten(100);
// array(100, null)
// No units for number < 1000
Inflexible::shorten(1523);
// 1k
文本化

返回数字的文本表示形式

Inflexible::textualize(1025433);
// One Million, Twenty Five Thousand, Four Hundred and Thirty Three

字符串

驼峰式

将“foo_bar”之类的单词转换为“FooBar”。它还会删除非字母数字字符

Inflexible::camelize('foo_bar');
// FooBar
去除命名空间

返回完整的类名

Inflexible::denamespace('\Foo\Bar\Baz');
// Baz
人性化

将驼峰式单词和下划线转换为空格以返回可读的字符串

Inflexible::humanize('foo_bar');
// Foo Bar
Inflexible::humanize('FooBar');
// Foo Bar
仅命名空间

返回完全限定类名的命名空间

Inflexible::namespaceOnly('\Foo\Bar\Baz');
// Foo\Bar
URL化

URL化字符串

Inflexible::namespaceOnly('lo\rem ipsum do|or sid amet||| #\`[|\" 10 .');
// lo-rem-ipsum-do-or-sid-amet-10

您可以可选地设置分隔符、最大长度或决定是否小写

Inflexible::slugify(
    'LoRem ipsum do|or sid amet||| #\`[|\" 10 .',
    array(
        'maxlength' => 4,
        'lowercase' => true,
        'separator' => '_'
    )
);
// lore

运行测试

首先确保您已安装所有依赖项,运行

$ composer install --dev

然后,从根目录中运行测试

$ phpunit

贡献

  1. 查看问题列表
  2. 分支
  3. 编写测试(用于新功能或错误)
  4. 发起PR

需求

  • PHP 5.3+

作者

Boris Guéry - guery.b@gmail.com - http://twitter.com/borisguery - http://borisguery.com

许可

Inflexible采用WTFPL许可证 - 详细信息请参阅LICENSE文件