- Description
- Installation
- Usage
- Datetime 1. Relative
- Number 1. HumanByte 2. Ordinalize 3. Shorten 4. Textualize
- String 1. Camelize 2. Denamespace 3. Humanize 4. NamespaceOnly 5. Slugify 6. Shorten 7. Pluralize 8. Singularize 9. Tableize
- Run the test
- Contributing
- Requirements
- Authors
- License
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.
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()
To use it, just call the service named "inflexible" which provides all functions of Inflexible library.
##Datetime
Convert a DateTime
object or a number of seconds into the most fitted unit:
$this->container->get('inflexible')->relativeDatetime(86400);
86400|relative_datetime
Returns
array(
1,
'day'
)
You may also want to get the relative datetime from a given date:
$this->container->get('inflexible')->relativeDatetime(new DateTime('2012-01-10'), new DateTime('2012-01-17'));
post.firstDate|relativeDatetime(post.secondDate)
Returns
array(
1,
'week'
)
The available units are:
- second
- minute
- hour
- day
- week
- month
- year
Convert bytes to an human readable representation to the most fitted unit:
$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
1024|human_byte
You may also provided an optional precision as a second argument (default to 2)
Converts number to its ordinal English form:
$this->container->get('inflexible')->ordinalize(1);
// 1st
$this->container->get('inflexible')->ordinalize(13);
// 13th
13|ordinalize
Formats a number using the SI units (k, M, G, etc.):
$this->container->get('inflexible')->shorten(100);
// array(100, null)
// No units for number < 1000
$this->container->get('inflexible')->shorten(1523);
// 1k
1523|shorten_number
Returns the textual representation of a number
$this->container->get('inflexible')->textualize(1025433);
// One Million, Twenty Five Thousand, Four Hundred and Thirty Three
1025433|textualize
Converts a word like "foo_bar" to "FooBar". It also removes non-alphanumeric characters:
$this->container->get('inflexible')->camelize('foo_bar');
// FooBar
'foo_bar'|camelize
Returns only the class name
$this->container->get('inflexible')->denamespace('\Foo\Bar\Baz');
// Baz
'\Foo\Bar\Baz'|denamespace
Converts CamelCased word and underscore to space to return a readable string:
$this->container->get('inflexible')->humanize('foo_bar');
// Foo Bar
$this->container->get('inflexible')->humanize('FooBar');
// Foo Bar
'FooBar'|humanize
Returns the namespace of a fully qualified class name:
$this->container->get('inflexible')->namespaceOnly('\Foo\Bar\Baz');
// Foo\Bar
'\Foo\Bar\Baz'|namespace_only
Slugify a string:
$this->container->get('inflexible')->slugify('lo\rem ipsum do|or sid amet||| #\`[|\" 10 .');
// lo-rem-ipsum-do-or-sid-amet-10
'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:
$this->container->get('inflexible')->slugify(
'LoRem ipsum do|or sid amet||| #\`[|\" 10 .',
array(
'maxlength' => 4,
'lowercase' => true,
'separator' => '_'
)
);
// lore
'lo\rem ipsum do|or sid amet||| #\`[|\" 10 .'|slugify(options)
Shorten a string using the options such as the maximum length, affix and its position:
$this->container->get('inflexible')->shorten("Lorem ipsum dolor sit amet",5,Shorten::AFFIX_POSITION_START);
"Lorem ipsum dolor sit amet"|shorten_string(5,'start')
Returns a word in plural form:
$this->container->get('inflexible')->pluralize("child");
//children
"child"|pluralize
Returns a word in singular form:
$this->container->get('inflexible')->singularize("children");
//child
"children"|singularize
Converts a word into the format for a Doctrine table name:
$this->container->get('inflexible')->tableize("ModelName");
//model_name
"ModelName"|tableize
First make sure you have installed all the dependencies, run:
$ composer install --dev
then, run the test from within the root directory:
$ phpunit
- Take a look at the list of issues.
- Fork
- Write a test (for either new feature or bug)
- Make a 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
is licensed under the WTFPL License - see the LICENSE file for details