1: <?php
2:
3: /**
4: * This file is part of the "dibi" - smart database abstraction layer.
5: *
6: * Copyright (c) 2005, 2010 David Grudl (http://davidgrudl.com)
7: *
8: * This source file is subject to the "dibi license", and/or
9: * GPL license. For more information please see http://dibiphp.com
10: * @package dibi
11: */
12:
13:
14:
15: /**
16: * Result set single row.
17: *
18: * @author David Grudl
19: */
20: class DibiRow implements ArrayAccess, IteratorAggregate, Countable
21: {
22:
23: public function __construct($arr)
24: {
25: foreach ($arr as $k => $v) $this->$k = $v;
26: }
27:
28:
29:
30: public function toArray()
31: {
32: return (array) $this;
33: }
34:
35:
36:
37: /**
38: * Converts value to DateTime object.
39: * @param string key
40: * @param string format
41: * @return DateTime
42: */
43: public function asDateTime($key, $format = NULL)
44: {
45: $time = $this[$key];
46: if ((int) $time === 0) { // '', NULL, FALSE, '0000-00-00', ...
47: return NULL;
48: }
49: $dt = new DibiDateTime(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
50: return $format === NULL ? $dt : $dt->format($format);
51: }
52:
53:
54:
55: /**
56: * Converts value to UNIX timestamp.
57: * @param string key
58: * @return int
59: */
60: public function asTimestamp($key)
61: {
62: $time = $this[$key];
63: return (int) $time === 0 // '', NULL, FALSE, '0000-00-00', ...
64: ? NULL
65: : (is_numeric($time) ? (int) $time : strtotime($time));
66: }
67:
68:
69:
70: /**
71: * Converts value to boolean.
72: * @param string key
73: * @return mixed
74: */
75: public function asBool($key)
76: {
77: $value = $this[$key];
78: if ($value === NULL || $value === FALSE) {
79: return $value;
80:
81: } else {
82: return ((bool) $value) && $value !== 'f' && $value !== 'F';
83: }
84: }
85:
86:
87:
88: /** @deprecated */
89: public function asDate($key, $format = NULL)
90: {
91: if ($format === NULL) {
92: return $this->asTimestamp($key);
93: } else {
94: return $this->asDateTime($key, $format === TRUE ? NULL : $format);
95: }
96: }
97:
98:
99:
100: /********************* interfaces ArrayAccess, Countable & IteratorAggregate ****************d*g**/
101:
102:
103:
104: final public function count()
105: {
106: return count((array) $this);
107: }
108:
109:
110:
111: final public function getIterator()
112: {
113: return new ArrayIterator($this);
114: }
115:
116:
117:
118: final public function offsetSet($nm, $val)
119: {
120: $this->$nm = $val;
121: }
122:
123:
124:
125: final public function offsetGet($nm)
126: {
127: return $this->$nm;
128: }
129:
130:
131:
132: final public function offsetExists($nm)
133: {
134: return isset($this->$nm);
135: }
136:
137:
138:
139: final public function offsetUnset($nm)
140: {
141: unset($this->$nm);
142: }
143:
144: }
145: