PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/base.php

http://github.com/jaz303/base-php
PHP | 218 lines | 100 code | 15 blank | 103 comment | 3 complexity | a12447f7a04a8a96c708755903a03f1a MD5 | raw file
  1. <?php
  2. /**
  3. * base.php
  4. */
  5. //
  6. // Common Exceptions
  7. // BEFORE CREATING YOUR OWN, PLEASE REMEMBER THE SPL DEFINES THESE EXCEPTIONS:
  8. //
  9. // LogicException
  10. // BadFunctionCallException
  11. // BadMethodCallException
  12. // DomainException
  13. // InvalidArgumentException
  14. // LengthException
  15. // OutOfRangeException
  16. // RuntimeException
  17. // OutOfBoundsException
  18. // OverflowException
  19. // RangeException
  20. // UnderflowException
  21. // UnexpectedValueException
  22. /**
  23. * Thrown when an object is requested to take some action that its
  24. * current state does not support.
  25. */
  26. class IllegalStateException extends DomainException {}
  27. class UnsupportedOperationException extends RuntimeException {}
  28. /**
  29. * Represents any sort of IO error
  30. */
  31. class IOException extends RuntimeException {}
  32. /**
  33. * Thrown when some operation involving finding something fails.
  34. *
  35. * It's explicitly *not* for range/bounds exceptions (e.g. access beyond an array's bounds).
  36. * In these circumstances use an OutOfRangeException or an OutOfBoundsException from the SPL.
  37. */
  38. class NotFoundException extends LogicException {}
  39. class NoSuchMethodException extends RuntimeException {}
  40. class SecurityException extends Exception {}
  41. class SyntaxException extends Exception {}
  42. /**
  43. * General interface denoting a persistable object.
  44. *
  45. * Additionally, a persistable object should generally implement <tt>get_</tt>,
  46. * <tt>set_</tt> and, optionally, <tt>is_</tt> methods for each of its attributes.
  47. *
  48. * @package BasePHP
  49. */
  50. interface Persistable
  51. {
  52. /**
  53. * Save this object.
  54. *
  55. * @return true on success, false on failure
  56. */
  57. public function save();
  58. /**
  59. * Returns true if this object has been persisted, false otherwise.
  60. *
  61. * @return true if this object has been persisted, false otherwise.
  62. */
  63. public function is_saved();
  64. /**
  65. * Returns a copy of this object's attributes as an associative array.
  66. *
  67. * @return a copy of this object's attributes as an associative array.
  68. */
  69. public function attributes();
  70. /**
  71. * Sets all of this object's attributes from an associative array.
  72. * May throws an <tt>\InvalidArgumentException</tt> exception if any keys in
  73. * <tt>$array</tt> are not valid properties of this object.
  74. *
  75. * @param $attributes array of attributes to set on this object.
  76. * @throws \InvalidArgumentException if specified properties do not exist.
  77. */
  78. public function set_attributes(array $attributes);
  79. /**
  80. * Returns true if this object is valid, false otherwise.
  81. *
  82. * @return true if this object is valid, false otherwise.
  83. */
  84. public function is_valid();
  85. /**
  86. * Returns an <tt>\Errors</tt> objects containing errors generated on the
  87. * last call to <tt>is_valid()</tt>
  88. *
  89. * @return <tt>\Errors</tt> instance detailing validation errors
  90. */
  91. public function errors();
  92. }
  93. /**
  94. * Callbacks are invokable objects providing a common interface to the numerous
  95. * PHP callback types.
  96. *
  97. * @author Jason Frame
  98. * @package BasePHP
  99. */
  100. abstract class Callback
  101. {
  102. /**
  103. * Turns any traditional 'callback' descriptor into an invokable object.
  104. *
  105. * Accepts one or two arguments; supported combinations are:
  106. *
  107. * Closure/other invokable object
  108. * "Class::method"
  109. * "function"
  110. * array("Class", "method")
  111. * array($object, "method")
  112. * "Class", "method"
  113. * $object, "method"
  114. *
  115. * @param $arg1 see above
  116. * @param $arg2 see above
  117. * @return invokable object
  118. * @throws InvalidArgumentException if supplied arguments do not resemble
  119. * valid callback.
  120. */
  121. public static function create($arg1, $arg2 = null) {
  122. if ($arg2 === null) {
  123. if (is_object($arg1)) {
  124. return $arg1; // assume implements __invoke()
  125. } else {
  126. // handles $arg1 being either
  127. // string, array(string, string) or array(object, string)
  128. return function() use($arg1) {
  129. return call_user_func_array($arg1, func_get_args());
  130. };
  131. }
  132. } elseif (is_string($arg2)) {
  133. return function() use ($arg1, $arg2) {
  134. return call_user_func_array(array($arg1, $arg2), func_get_args());
  135. };
  136. }
  137. throw new InvalidArgumentException("Couldn't understand the supplied callback description");
  138. }
  139. }
  140. /**
  141. * A rather naive inflector
  142. *
  143. * I have no interest in making this into some thing complex with tons of rule-based
  144. * processing.
  145. */
  146. class Inflector
  147. {
  148. public static function pluralize($count, $singular, $plural = null) {
  149. if ($count == 1) {
  150. return $singular;
  151. } else {
  152. return $plural === null ? ($singular . 's') : $plural;
  153. }
  154. }
  155. public static function humanize($string) {
  156. return ucfirst(strtolower(str_replace(array('_', '-'), ' ', $string)));
  157. }
  158. /**
  159. * CamelCase -> camel_case
  160. * already_underscored -> already_underscored
  161. */
  162. public static function underscore($string) {
  163. return strtolower(preg_replace('/([^^])([A-Z])/e', '\1_\2', $string));
  164. }
  165. /**
  166. * foo_bar -> FooBar
  167. * FooBar -> FooBar
  168. */
  169. public static function camelize($string) {
  170. return preg_replace('/(^|_)([a-z])/ie', 'strtoupper("$2")', $string);
  171. }
  172. }
  173. class StringUtils
  174. {
  175. const ALPHA_LOWER = 1;
  176. const ALPHA_UPPER = 2;
  177. const ALPHA = 3;
  178. const NUMERIC = 4;
  179. const ALPHA_NUMERIC = 7;
  180. public static function random($length, $set = self::ALPHA_NUMERIC) {
  181. if (is_string($set)) {
  182. $chars = $set;
  183. } else {
  184. $chars = '';
  185. if ($set && self::ALPHA_LOWER) $chars .= 'abcdefghijklmnopqrstuvwxyz';
  186. if ($set && self::ALPHA_UPPER) $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  187. if ($set && self::ALPHA_NUMERIC) $chars .= '0123456789';
  188. }
  189. $cl = strlen($chars) - 1;
  190. $out = '';
  191. for ($i = 0; $i < $length; $i++) {
  192. $out .= $chars[rand(0, $cl)];
  193. }
  194. return $out;
  195. }
  196. }
  197. ?>