Source for file DibiRow.php

Documentation is available at DibiRow.php

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