mirror of
https://github.com/dg/dibi.git
synced 2025-08-05 05:37:39 +02:00
- DibiRow: added helper methods asDate() & asBool()
- DibiTranslator: added new modifier %in - integer or NULL
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user