kohanaworld / zz-kohana-jelly

A flexible ORM for Kohana 3
http://jelly.jonathan-geiger.com
MIT License
4 stars 0 forks source link

Model field data formatting #3

Open smgladkovskiy opened 13 years ago

smgladkovskiy commented 13 years ago

In master branch of Jelly we had form items generating. In this functional there was a data formatting mechanism (such as pretty_format for timestamps) that helped to convert data that was requested from the database in database (or some other) format to a readable to the user format.

In 3.1 unstable branch we offer to make a 'format' method that will be in model and tries to format a field data to represent it in some pretty format that was set in the model meta ('pretty_format' in field properties).

It would be defined in model meta initialization like something this:

'birthday' => Jelly::field('Timestamp', array(
    'pretty_format' => 'd.m.Y',
)),

/* for Integer fields would be used number_format function and params
 * for pretty_format should set as 
 * array(int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
 */
'price' => Jelly::field('Float', array(
    'pretty_format' => array(2, ',', ' ')),
)),
'monkeys' => Jelly::field('String', array(
    'pretty_format' => '[%010s]\n', // sprintf format is set
)),

And will be called like that:

// fields formats
echo $model->format('birthday'); // will echo formatted date:   04.02.1999
echo $model->format('price');    // will echo formatted price:  12 000,25
echo $model->format('monkeys');  // will echo formatted string: [0000monkey]
biakaveron commented 13 years ago

Additional suggestion: add optional param $format, so we can set custom field format, for example $model->format('birthday', 'd.m.y')