/src/engine/ICE/base/base.php

https://github.com/PressCrew/infinity · PHP · 279 lines · 121 code · 26 blank · 132 comment · 26 complexity · be5cd6995cd8e63eb654384ddc6855ff MD5 · raw file

  1. <?php
  2. /**
  3. * ICE API: base class file
  4. *
  5. * @author Marshall Sorenson <marshall@presscrew.com>
  6. * @link http://infinity.presscrew.com/
  7. * @copyright Copyright (C) 2010-2011 Marshall Sorenson
  8. * @license http://www.gnu.org/licenses/gpl.html GPLv2 or later
  9. * @package ICE
  10. * @subpackage base
  11. * @since 1.0
  12. */
  13. /**
  14. * Every ICE class extends this one
  15. *
  16. * @package ICE
  17. * @subpackage base
  18. */
  19. abstract class ICE_Base
  20. {
  21. /**
  22. * Default magic getter.
  23. *
  24. * @param string $name
  25. * @throws Exception
  26. */
  27. public function __get( $name )
  28. {
  29. throw new Exception(
  30. sprintf( 'The "%s" property does not exist (get).', $name ) );
  31. }
  32. /**
  33. * Default magic setter.
  34. *
  35. * @param string $name
  36. * @param mixed $value
  37. * @throws Exception
  38. */
  39. public function __set( $name, $value )
  40. {
  41. throw new Exception(
  42. sprintf( 'The "%s" property does not exist (set).', $name ) );
  43. }
  44. /**
  45. * Default magic issetter.
  46. *
  47. * @param string $name
  48. */
  49. public function __isset( $name )
  50. {
  51. throw new Exception(
  52. sprintf( 'The "%s" property does not exist (isset).', $name ) );
  53. }
  54. /**
  55. * Default magic unsetter.
  56. *
  57. * @param string $name
  58. */
  59. public function __unset( $name )
  60. {
  61. throw new Exception(
  62. sprintf( 'The "%s" property does not exist (unset).', $name ) );
  63. }
  64. /**
  65. * Default magic caller.
  66. *
  67. * @param string $name
  68. * @param array $arguments
  69. */
  70. public function __call( $name, $arguments )
  71. {
  72. throw new Exception(
  73. sprintf( 'The "%s" method does not exist (obj context).', $name ) );
  74. }
  75. /**
  76. * Default toStringer.
  77. *
  78. * @return string
  79. */
  80. public function __toString()
  81. {
  82. throw new Exception(
  83. sprintf( 'The "%s" class cannot be converted to a string.', get_class($this) ) );
  84. }
  85. /**
  86. * Default getter.
  87. *
  88. * @param string $name Property name
  89. * @return mixed
  90. */
  91. public function get_property( $name )
  92. {
  93. throw new Exception(
  94. sprintf( 'The "%s" property is not accessible for reading.', $name ) );
  95. }
  96. /**
  97. * Default setter.
  98. *
  99. * @param string $name Property name
  100. * @param mixed $value Property value
  101. * @return ICE_Base
  102. */
  103. protected function set_property( $name, $value )
  104. {
  105. throw new Exception(
  106. sprintf( 'The "%s" property is not accessible for writing.', $name ) );
  107. }
  108. /**
  109. * Cast a value from one type to another.
  110. *
  111. * This method simplifies type casting. It supports numeric tests and some sanity checks
  112. * to prevent silly mistakes like casting an array to a string, etc.
  113. *
  114. * @param mixed $value The value to cast
  115. * @param string $type One of string|integer|float|number|boolean|array|object|unset
  116. * @return mixed
  117. * @throws Exception
  118. */
  119. final public function cast( $value, $type )
  120. {
  121. // get the type for the received value
  122. $valtype = gettype( $value );
  123. // if actual type matches requested type, nothing to do
  124. if ( $type == $valtype ) {
  125. // return value untouched
  126. return $value;
  127. }
  128. // value has different type, need to cast
  129. switch ( $type ) {
  130. // cast to string
  131. case 'string' :
  132. // must be scalar
  133. if ( is_scalar( $value ) ) {
  134. // ok to cast
  135. return (string) $value;
  136. } else {
  137. throw new Exception( 'Casting an non-scalar value to a string is silly!' );
  138. }
  139. // cast to integer
  140. case 'integer' :
  141. // must be scalar
  142. if ( is_scalar( $value ) ) {
  143. // make sure its numeric
  144. if ( is_numeric( $value ) ) {
  145. // its numeric!
  146. return (integer) $value;
  147. } else {
  148. // not numeric
  149. throw new Exception( 'Casting a non-numeric value to an integer is silly' );
  150. }
  151. } else {
  152. // not scalar
  153. throw new Exception( 'Casting a non-scalar value to an integer is silly' );
  154. }
  155. // cast to float
  156. case 'float' :
  157. // must be scalar
  158. if ( is_scalar( $value ) ) {
  159. // make sure its numeric
  160. if ( is_numeric( $value ) ) {
  161. // its numeric!
  162. return (float) $value;
  163. } else {
  164. // not numeric
  165. throw new Exception( 'Casting a non-numeric value to a float is silly' );
  166. }
  167. } else {
  168. // not scalar
  169. throw new Exception( 'Casting a non-scalar value to a float is silly' );
  170. }
  171. // cast to a number (float OR integer)
  172. case 'number' :
  173. // make sure its numeric
  174. if ( is_numeric( $value ) ) {
  175. // its numeric! if it has a dot, cast to float, otherwise cast to int
  176. if ( strpos( $value, '.' ) ) {
  177. // call again using float as type
  178. return $this->cast( $value, 'float' );
  179. } else {
  180. // call again using integer as type
  181. return $this->cast( $value, 'integer' );
  182. }
  183. } else {
  184. // not numeric
  185. throw new Exception( 'Casting a non-numeric value to a number is silly' );
  186. }
  187. // cast to boolean
  188. case 'boolean' :
  189. // sanity check
  190. if ( 'object' != $valtype && 'resource' != $valtype ) {
  191. // ok to cast
  192. return (boolean) $value;
  193. } else {
  194. throw new Exception( 'Casting an object or resource to a boolean is silly!' );
  195. }
  196. // cast to array
  197. case 'array' :
  198. return (array) $value;
  199. // cast to object
  200. case 'object' :
  201. // sanity check
  202. if ( !is_scalar( $value ) && 'resource' != $valtype ) {
  203. // ok to cast
  204. return (object) $value;
  205. } else {
  206. throw new Exception( 'Casting a scalar value or a resource to an object is silly!' );
  207. }
  208. // cast to unset
  209. case 'unset' :
  210. // this does NOT unset the value, but always returns NULL
  211. return (unset) $value;
  212. // cast to resource
  213. case 'resource' :
  214. throw new Exception( 'Casting any value to a resource is silly!' );
  215. // default
  216. default :
  217. // must have a valid type
  218. throw new Exception( sprintf( 'The type: "%s" is not a valid type', $type ) );
  219. }
  220. }
  221. }
  222. /**
  223. * This exception is thrown when a fatal error occurs due
  224. * to an incomplete and/or insecure environment at runtime.
  225. */
  226. abstract class ICE_Environment_Exception extends RuntimeException
  227. {
  228. // nothing special yet
  229. }
  230. /**
  231. * This exception is thrown when a fatal error occurs due
  232. * to missing or otherwise broken external requirements.
  233. */
  234. class ICE_Requirements_Exception extends ICE_Environment_Exception
  235. {
  236. // nothing special yet
  237. }
  238. /**
  239. * This exception is thrown when a fatal error occurs due
  240. * to inadequate permissions and/or capabilities.
  241. */
  242. class ICE_Capabilities_Exception extends ICE_Environment_Exception
  243. {
  244. // nothing special yet
  245. }
  246. /**
  247. * This exception is thrown when a fatal error occurs due
  248. * to missing or uninitialized settings and/or data.
  249. */
  250. class ICE_Initialization_Exception extends ICE_Environment_Exception
  251. {
  252. // nothing special yet
  253. }