The purpose of this blog post is to review the last features that have been included into the BCCExtraToolsBundle.
As you may know, its current main feature is a command that extract translation string from your template and dump them into your translation files. It has been so popular that the code has been adapted and merged it into the code Framework a few months ago with the help of the community.
Sadly, I will eventually remove this functionality from the bundle when the 2.1 version will come out. Hopefully, I recently add new cool stuffs into the Bundle recently that you may find useful.
A date parser
You may know that the Bundle currently includes a date formatter that provides you a nice way to localize your dates directly with a twig filter.
I reviewed the code recently to provide a way to make the operation working the other way. You can now do such things :
// obtain a datetime instance for a normally formated string
$date = $dateFormatter->parse(‘November 1, 2011′);
// obtain a datetime instance using a defined locale
$date = $dateFormatter->parse(‘1 Novembre 2011, 20;14’, ‘fr’);
// obtains a datetime instance with a uncommon string
$date = $dateFormatter->parse(‘Nov. 2011);
Basically, the code is trying every parsing it can using the formats available for the formatter (more information here http://www.michelsalib.com/2011/07/a-twig-extension-that-translates-countries-and-dates/) and more so that it can parse almost anything.
Of course it can be very handy when you consume some weird API using nonstandard datetime formatting.
A unit converter
About consuming API. I can be very annoying to find some values in a unit don’t want to use. The unit formatter is here to help you:
// transform a value knowing the source and destination units
echo $unitConverter->convert(1000, ‘m’, ‘km’); // echoes : 1
// transform a value knowing only the destination units
echo $unitConverter->guessConvert(’1h’, ‘m’); // echoes : 60
To do such work, your value will go through different phases::
- A chain unit converter will try to convert using its DI registered unit converters
- A ratio unit converter which converts units that are strictly proportional will take care of most your conversion
- The ratio unit converter supports different kind of units registered using DI, such as length, weight, speed, time… those are called ratio unit providers
- Ratio units providers defines units and ratios related to a specific unit kind. It includes also the locale where the unit is applicable and the prefixes that can be associated to the unit. For instance, the computer capacity unit kind will define the octet, the bit and the byte with their corresponding conversion ratio and the applicable prefixes (k, M, G…).
With such a structure, the converter insure you that any prefix is taken into account, that the conversion is done between coherent units (it won’t try to convert time into money… sadly), and it also takes into account your current locale (the ‘metre’ is the french unit for the ‘meter’).
I’ll try to add more units and documentation in the coming posts.
The BCCExtraToolsBundle continues to provide useful features to the developers. Get the code of the datetime parser and the unit converter on the BCCExtraToolsBundle github : https://github.com/michelsalib/BCCExtraToolsBundle. Don’t hesitate to report issues, feedback and improve the code