sexyboys/inflexiblebundle

Symfony2 Bundle for Inflexible library

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

This package is not auto-updated.

Last update: 2022-10-01 05:11:34 UTC


README

Table of contents

  1. Description
  2. Installation
  3. Usage
  4. Datetime 1. Relative
  5. Number 1. HumanByte 2. Ordinalize 3. Shorten 4. Textualize
  6. String 1. Camelize 2. Denamespace 3. Humanize 4. NamespaceOnly 5. Slugify 6. Shorten 7. Pluralize 8. Singularize 9. Tableize
  7. Run the test
  8. Contributing
  9. Requirements
  10. Authors
  11. License

Description

InflexibleBundle provides a bridge between Symfony2 and Inflexible library which aims to gather a collection of commonly used Inflectors into a single lib. It provides twig extensions too.

Installation

Using Composer, just $ composer require sexyboys/inflexiblebundle package or:

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

Define the bundle inside your AppKernel.php file

new Sexyboys\InflexibleBundle\SexyboysInflexibleBundle()

Usage

To use it, just call the service named "inflexible" which provides all functions of Inflexible library.

##Datetime

Relative

Convert a DateTime object or a number of seconds into the most fitted unit:

Php

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

Twig

86400|relative_datetime

Returns

array(
    1,
    'day'
)

You may also want to get the relative datetime from a given date:

Php

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

Twig

post.firstDate|relativeDatetime(post.secondDate)

Returns

array(
    1,
    'week'
)

The available units are:

  • second
  • minute
  • hour
  • day
  • week
  • month
  • year

Number

HumanByte

Convert bytes to an human readable representation to the most fitted unit:

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

You may also provided an optional precision as a second argument (default to 2)

Ordinalize

Converts number to its ordinal English form:

Php

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

Twig

13|ordinalize

Shorten

Formats a number using the SI units (k, M, G, etc.):

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

Textualize

Returns the textual representation of a number

Php

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

Twig

1025433|textualize

String

Camelize

Converts a word like "foo_bar" to "FooBar". It also removes non-alphanumeric characters:

Php

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

Twig

'foo_bar'|camelize

Denamespace

Returns only the class name

Php

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

Twig

'\Foo\Bar\Baz'|denamespace

Humanize

Converts CamelCased word and underscore to space to return a readable string:

Php

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

Twig

'FooBar'|humanize

NamespaceOnly

Returns the namespace of a fully qualified class name:

Php

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

Twig

'\Foo\Bar\Baz'|namespace_only

Slugify

Slugify a string:

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

You may optionally set the separator, a max length or decide to whether lower the case:

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)

Shorten

Shorten a string using the options such as the maximum length, affix and its position:

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')

Pluralize

Returns a word in plural form:

Php

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

Twig

"child"|pluralize

Singularize

Returns a word in singular form:

Php

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

Twig

"children"|singularize

Tableize

Converts a word into the format for a Doctrine table name:

Php

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

Twig

"ModelName"|tableize

Run the test

First make sure you have installed all the dependencies, run:

$ composer install --dev

then, run the test from within the root directory:

$ phpunit

Contributing

  1. Take a look at the list of issues.
  2. Fork
  3. Write a test (for either new feature or bug)
  4. Make a PR

Requirements

  • PHP 5.3+
  • Symfony 2

Authors

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

License

InflexibleBundle is licensed under the WTFPL License - see the LICENSE file for details