1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 13:47:33 +02:00

- DibiRow: added helper methods asDate() & asBool()

- DibiTranslator: added new modifier %in - integer or NULL
This commit is contained in:
David Grudl
2009-06-03 13:42:02 +00:00
parent 9c435acd2e
commit bf15d60fd1
3 changed files with 46 additions and 1 deletions

View File

@@ -493,7 +493,10 @@ class DibiResult extends DibiObject implements IDataSource
/**
* Converts value to specified type and format.
* @return array ($type, $format)
* @param mixed value
* @param int type
* @param string format
* @return mixed
*/
final public function convert($value, $type, $format = NULL)
{

View File

@@ -40,6 +40,44 @@ class DibiRow extends ArrayObject
/**
* Converts value to date-time format.
* @param string key
* @param string format
* @return mixed
*/
public function asDate($key, $format = NULL)
{
$value = $this[$key];
if ($value === NULL || $value === FALSE) {
return $value;
} else {
$value = is_numeric($value) ? (int) $value : strtotime($value);
return $format === NULL ? $value : date($format, $value);
}
}
/**
* Converts value to boolean.
* @param string key
* @return mixed
*/
public function asBool($key)
{
$value = $this[$key];
if ($value === NULL || $value === FALSE) {
return $value;
} else {
return ((bool) $value) && $value !== 'f' && $value !== 'F';
}
}
/**
* PHP < 5.3 workaround
* @return void

View File

@@ -319,6 +319,10 @@ final class DibiTranslator extends DibiObject
case 'sn': // string or NULL
return $value == '' ? 'NULL' : $this->driver->escape($value, dibi::TEXT); // notice two equal signs
case 'in': // signed int or NULL
if ($value == '') $value = NULL;
// intentionally break omitted
case 'i': // signed int
case 'u': // unsigned int, ignored
// support for long numbers - keep them unchanged