1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace picon;
24:
25: 26: 27: 28: 29: 30:
31: class Args
32: {
33: 34: 35: 36:
37: public static function callBack($object, $argName)
38: {
39: if(!is_callable($object) && !($object instanceof SerializableClosure))
40: {
41: throw new \InvalidArgumentException(sprintf("%s expected argument %s to be callable", self::getCallingMethod(), $argName));
42: }
43: }
44:
45: 46: 47: 48: 49: 50:
51: public static function callBackArgs($callback, $amount, $argName)
52: {
53: self::isNumeric($amount, 'amount');
54:
55: if(!is_callable($callback) && !($callback instanceof SerializableClosure))
56: {
57: throw new \InvalidArgumentException(sprintf("%s expected argument %s to be callable", self::getCallingMethod(), $argName));
58: }
59:
60: if($callback instanceof SerializableClosure)
61: {
62: $reflection = $callback->getReflection();
63: }
64: else
65: {
66: $reflection = new \ReflectionFunction($callback);
67: }
68: if(count($reflection->getParameters())!=$amount)
69: {
70: throw new \InvalidArgumentException(sprintf("%s expected argument %s to be callable and take %d argument(s)", self::getCallingMethod(), $argName, $amount));
71: }
72: }
73:
74: 75: 76: 77:
78: public static function isNumeric($number, $argName)
79: {
80: if(!is_numeric($number))
81: {
82: throw new \InvalidArgumentException(sprintf("%s expected argument %s to be numeric", self::getCallingMethod(), $argName));
83: }
84: }
85:
86: 87: 88: 89: 90:
91: public static function isArray($object, $argName)
92: {
93: if(!is_array($object))
94: {
95: throw new \InvalidArgumentException(sprintf("%s expected argument %s to be an array", self::getCallingMethod(), $argName));
96: }
97: }
98:
99: 100: 101: 102: 103:
104: public static function isString($object, $argName)
105: {
106: if(!is_string($object))
107: {
108: throw new \InvalidArgumentException(sprintf("%s expected argument %s to be a string", self::getCallingMethod(), $argName));
109: }
110: }
111:
112: 113: 114: 115: 116:
117: public static function isBoolean($object, $argName)
118: {
119: if(!is_bool($object))
120: {
121: throw new \InvalidArgumentException(sprintf("%s expected argument %s to be a boolean", self::getCallingMethod(), $argName));
122: }
123: }
124:
125: 126: 127: 128:
129: private static function getCallingMethod()
130: {
131: $trace = debug_backtrace();
132: return $trace[2]['class'].$trace[2]['type'].$trace[2]['function'].'()';
133: }
134:
135: 136: 137: 138: 139:
140: public static function notNull($object, $argName)
141: {
142: if($object==null)
143: {
144: throw new \InvalidArgumentException(sprintf("%s expected argument %s to be non null", self::getCallingMethod(), $argName));
145: }
146: }
147:
148: 149: 150: 151: 152: 153:
154: public static function identifierOf(Identifier $object, Identifier $expected, $argName)
155: {
156: if(!$object->of($expected))
157: {
158: throw new \InvalidArgumentException(sprintf("%s expected argument %s to be an identifier of %s, actual %s", self::getCallingMethod(), $argName, $expected->getFullyQualifiedName(), $object->getFullyQualifiedName()));
159: }
160: }
161: }
162:
163: ?>
164: