2009-02-05 01:26:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dibi - tiny'n'smart database abstraction layer
|
|
|
|
* ----------------------------------------------
|
|
|
|
*
|
2010-01-03 15:15:37 +01:00
|
|
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
2009-02-05 01:26:08 +00:00
|
|
|
* @license http://dibiphp.com/license dibi license
|
|
|
|
* @link http://dibiphp.com
|
|
|
|
* @package dibi
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-02-05 23:13:29 +00:00
|
|
|
* Result-set single row.
|
2009-02-05 01:26:08 +00:00
|
|
|
*
|
2010-01-03 15:15:37 +01:00
|
|
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
2009-02-05 01:26:08 +00:00
|
|
|
* @package dibi
|
|
|
|
*/
|
2010-04-26 20:44:17 +02:00
|
|
|
class DibiRow implements ArrayAccess, IteratorAggregate, Countable
|
2009-02-05 01:26:08 +00:00
|
|
|
{
|
|
|
|
|
2010-04-26 20:44:17 +02:00
|
|
|
public function __construct($arr)
|
2009-02-05 01:26:08 +00:00
|
|
|
{
|
2010-04-22 23:19:33 +02:00
|
|
|
foreach ($arr as $k => $v) $this->$k = $v;
|
2009-02-05 01:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-26 20:44:17 +02:00
|
|
|
public function toArray()
|
|
|
|
{
|
|
|
|
return (array) $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-03 13:42:02 +00:00
|
|
|
/**
|
2010-01-26 01:38:47 +01:00
|
|
|
* Converts value to DateTime object.
|
2009-06-03 13:42:02 +00:00
|
|
|
* @param string key
|
2010-02-15 21:16:18 +01:00
|
|
|
* @param string format
|
2010-01-26 01:38:47 +01:00
|
|
|
* @return DateTime
|
2009-06-03 13:42:02 +00:00
|
|
|
*/
|
2010-02-15 21:16:18 +01:00
|
|
|
public function asDateTime($key, $format = NULL)
|
2009-06-03 13:42:02 +00:00
|
|
|
{
|
2009-08-20 23:42:50 +02:00
|
|
|
$time = $this[$key];
|
2010-02-15 21:16:18 +01:00
|
|
|
if ((int) $time === 0) { // '', NULL, FALSE, '0000-00-00', ...
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
$dt = new DateTime53(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
|
|
|
|
return $format === NULL ? $dt : $dt->format($format);
|
2010-01-26 01:38:47 +01:00
|
|
|
}
|
2009-08-20 23:42:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-26 01:38:47 +01:00
|
|
|
/**
|
|
|
|
* Converts value to UNIX timestamp.
|
|
|
|
* @param string key
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function asTimestamp($key)
|
|
|
|
{
|
|
|
|
$time = $this[$key];
|
|
|
|
return (int) $time === 0 // '', NULL, FALSE, '0000-00-00', ...
|
|
|
|
? NULL
|
|
|
|
: (is_numeric($time) ? (int) $time : strtotime($time));
|
2009-06-03 13:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-26 01:38:47 +01:00
|
|
|
/** @deprecated */
|
|
|
|
public function asDate($key, $format = NULL)
|
|
|
|
{
|
|
|
|
if ($format === NULL) {
|
|
|
|
return $this->asTimestamp($key);
|
|
|
|
} else {
|
2010-02-15 21:16:18 +01:00
|
|
|
return $this->asDateTime($key, $format === TRUE ? NULL : $format);
|
2010-01-26 01:38:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-22 23:19:33 +02:00
|
|
|
|
|
|
|
|
2010-04-26 20:44:17 +02:00
|
|
|
/********************* interfaces ArrayAccess, Countable & IteratorAggregate ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final public function count()
|
|
|
|
{
|
|
|
|
return count((array) $this);
|
|
|
|
}
|
2010-04-22 23:19:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final public function getIterator()
|
|
|
|
{
|
|
|
|
return new ArrayIterator($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final public function offsetSet($nm, $val)
|
|
|
|
{
|
|
|
|
$this->$nm = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final public function offsetGet($nm)
|
|
|
|
{
|
|
|
|
return $this->$nm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final public function offsetExists($nm)
|
|
|
|
{
|
|
|
|
return isset($this->$nm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final public function offsetUnset($nm)
|
|
|
|
{
|
|
|
|
unset($this->$nm);
|
|
|
|
}
|
|
|
|
|
2009-02-05 01:26:08 +00:00
|
|
|
}
|