2009-02-05 01:26:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2010-09-14 18:40:41 +02:00
|
|
|
* This file is part of the "dibi" - smart database abstraction layer.
|
2012-01-02 20:24:16 +01:00
|
|
|
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
|
2009-02-05 01:26:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-08-03 22:48:44 +02:00
|
|
|
* Result set single row.
|
2009-02-05 01:26:08 +00:00
|
|
|
*
|
2010-09-14 18:40:41 +02:00
|
|
|
* @author David Grudl
|
2012-01-03 04:50:11 +01:00
|
|
|
* @package dibi
|
2009-02-05 01:26:08 +00:00
|
|
|
*/
|
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
|
|
|
{
|
2013-07-02 18:42:55 +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];
|
2012-01-12 01:19:06 +01:00
|
|
|
if (!$time instanceof DibiDateTime) {
|
2013-12-12 16:23:19 +01:00
|
|
|
if ((int) $time === 0 && substr((string) $time, 0, 3) !== '00:') { // '', NULL, FALSE, '0000-00-00', ...
|
2012-01-12 01:19:06 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-04 01:19:20 +02:00
|
|
|
$time = new DibiDateTime($time);
|
2010-02-15 21:16:18 +01:00
|
|
|
}
|
2012-01-12 01:19:06 +01:00
|
|
|
return $format === NULL ? $time : $time->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)
|
|
|
|
{
|
2012-01-12 01:19:06 +01:00
|
|
|
trigger_error(__METHOD__ . '() is deprecated.', E_USER_WARNING);
|
2013-12-12 16:23:19 +01:00
|
|
|
return $this->asDateTime($key, 'U');
|
2009-06-03 13:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts value to boolean.
|
|
|
|
* @param string key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function asBool($key)
|
|
|
|
{
|
2012-01-12 01:19:06 +01:00
|
|
|
trigger_error(__METHOD__ . '() is deprecated.', E_USER_WARNING);
|
|
|
|
return $this[$key];
|
2009-06-03 13:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-26 01:38:47 +01:00
|
|
|
/** @deprecated */
|
|
|
|
public function asDate($key, $format = NULL)
|
|
|
|
{
|
2012-01-12 01:19:06 +01:00
|
|
|
trigger_error(__METHOD__ . '() is deprecated.', E_USER_WARNING);
|
2010-01-26 01:38:47 +01:00
|
|
|
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
|
|
|
}
|