sexyboys/inflexiblebundle

Symfony2 Bundle for Inflexible library

dev-master 2013-08-24 13:48 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:40:23 UTC


README

目录

  1. 描述
  2. 安装
  3. 使用
  4. 日期时间 1. 相对
  5. 数字 1. 人类字节 2. 序数化 3. 缩短 4. 文本化
  6. 字符串 1. 驼峰式 2. 去除命名空间 3. 人性化 4. 只命名空间 5. 生成短链接 6. 缩短字符串 7. 复数化 8. 单数化 9. 表格化
  7. 运行测试
  8. 贡献
  9. 要求
  10. 作者
  11. 许可证

描述

InflexibleBundle 提供了一个连接 Symfony2 和 Inflexible 库的桥梁,旨在将一组常用 Inflectors 收集到一个库中。它还提供了 twig 扩展。

安装

使用 Composer,只需执行 $ composer require sexyboys/inflexiblebundle 命令即可安装该包

{
  "require": {
    "sexyboys/inflexiblebundle": "dev-master"
  }
}

在您的 AppKernel.php 文件中定义该包

new Sexyboys\InflexibleBundle\SexyboysInflexibleBundle()

使用

要使用它,只需调用名为 "inflexible" 的服务,它提供了 Inflexible 库的所有功能。

## 日期时间

相对

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

PHP

$this->container->get('inflexible')->relativeDatetime(86400);

Twig

86400|relative_datetime

返回

array(
    1,
    'day'
)

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

PHP

$this->container->get('inflexible')->relativeDatetime(new DateTime('2012-01-10'), new DateTime('2012-01-17'));

Twig

post.firstDate|relativeDatetime(post.secondDate)

返回

array(
    1,
    'week'
)

可用的单位有

  • 分钟
  • 小时

数字

人类字节

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

PHP

$this->container->get('inflexible')->humanByte(1024);
// 1.00 KB
$this->container->get('inflexible')->humanByte(1048576);
// 1.00 MB
$this->container->get('inflexible')->humanByte(1073741824);
// 1.00 GB

Twig

1024|human_byte

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

序数化

将数字转换为其英文序数形式

PHP

$this->container->get('inflexible')->ordinalize(1);
// 1st
$this->container->get('inflexible')->ordinalize(13);
// 13th

Twig

13|ordinalize

缩短

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

PHP

$this->container->get('inflexible')->shorten(100);
// array(100, null)
// No units for number < 1000
$this->container->get('inflexible')->shorten(1523);
// 1k

Twig

1523|shorten_number

文本化

返回数字的文本表示形式

PHP

$this->container->get('inflexible')->textualize(1025433);
// One Million, Twenty Five Thousand, Four Hundred and Thirty Three

Twig

1025433|textualize

字符串

驼峰式

将 "foo_bar" 等单词转换为 "FooBar"。它还删除非字母数字字符

PHP

$this->container->get('inflexible')->camelize('foo_bar');
// FooBar

Twig

'foo_bar'|camelize

去除命名空间

仅返回类名

PHP

$this->container->get('inflexible')->denamespace('\Foo\Bar\Baz');
// Baz

Twig

'\Foo\Bar\Baz'|denamespace

人性化

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

PHP

$this->container->get('inflexible')->humanize('foo_bar');
// Foo Bar
$this->container->get('inflexible')->humanize('FooBar');
// Foo Bar

Twig

'FooBar'|humanize

只命名空间

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

PHP

$this->container->get('inflexible')->namespaceOnly('\Foo\Bar\Baz');
// Foo\Bar

Twig

'\Foo\Bar\Baz'|namespace_only

生成短链接

生成字符串的短链接

PHP

$this->container->get('inflexible')->slugify('lo\rem ipsum do|or sid amet||| #\`[|\" 10 .');
// lo-rem-ipsum-do-or-sid-amet-10

Twig

'lo\rem ipsum do|or sid amet||| #\`[|\" 10 .'|slugify

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

PHP

$this->container->get('inflexible')->slugify(
    'LoRem ipsum do|or sid amet||| #\`[|\" 10 .',
    array(
        'maxlength' => 4,
        'lowercase' => true,
        'separator' => '_'
    )
);
// lore

Twig

'lo\rem ipsum do|or sid amet||| #\`[|\" 10 .'|slugify(options)

缩短

使用最大长度、前缀及其位置等选项缩短字符串

PHP

$this->container->get('inflexible')->shorten("Lorem ipsum dolor sit amet",5,Shorten::AFFIX_POSITION_START);

Twig

"Lorem ipsum dolor sit amet"|shorten_string(5,'start')

复数化

返回复数形式的单词

PHP

$this->container->get('inflexible')->pluralize("child");
//children

Twig

"child"|pluralize

单数化

返回单数形式的单词

PHP

$this->container->get('inflexible')->singularize("children");
//child

Twig

"children"|singularize

表格化

将单词转换为Doctrine表名的格式

PHP

$this->container->get('inflexible')->tableize("ModelName");
//model_name

Twig

"ModelName"|tableize

运行测试

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

$ composer install --dev

然后,在根目录下运行测试

$ phpunit

贡献

  1. 查看问题列表
  2. 分支
  3. 编写测试(针对新功能或错误)
  4. 提交PR

要求

  • PHP 5.3+
  • Symfony 2

作者

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

Eric Pidoux - eric.pidoux@gmail.com - http://twitter.com/epidoux - http://eric-pidoux.com

许可证

InflexibleBundle遵循WTFPL许可证 - 请参阅LICENSE文件以获取详细信息