1: <?php
2:
3: /**
4: * Copyright 2015, Eduardo Trujillo
5: *
6: * For the full copyright and license information, please view the LICENSE
7: * file that was distributed with this source code.
8: *
9: * This file is part of the Nucleus package
10: */
11:
12: namespace Chromabits\Nucleus\Meditation;
13:
14: use Chromabits\Nucleus\Foundation\BaseObject;
15: use Chromabits\Nucleus\Meditation\Constraints\AbstractConstraint;
16: use Chromabits\Nucleus\Meditation\Constraints\AnyConstraint;
17: use Chromabits\Nucleus\Meditation\Constraints\ArrayOfConstraint;
18: use Chromabits\Nucleus\Meditation\Constraints\BooleanConstraint;
19: use Chromabits\Nucleus\Meditation\Constraints\CallableConstraint;
20: use Chromabits\Nucleus\Meditation\Constraints\ClassTypeConstraint;
21: use Chromabits\Nucleus\Meditation\Constraints\ClosureConstraint;
22: use Chromabits\Nucleus\Meditation\Constraints\EitherConstraint;
23: use Chromabits\Nucleus\Meditation\Constraints\FoldableConstraint;
24: use Chromabits\Nucleus\Meditation\Constraints\FunctorConstraint;
25: use Chromabits\Nucleus\Meditation\Constraints\InArrayConstraint;
26: use Chromabits\Nucleus\Meditation\Constraints\LeftFoldableConstraint;
27: use Chromabits\Nucleus\Meditation\Constraints\LeftFoldableOfConstraint;
28: use Chromabits\Nucleus\Meditation\Constraints\ListConstraint;
29: use Chromabits\Nucleus\Meditation\Constraints\MapConstraint;
30: use Chromabits\Nucleus\Meditation\Constraints\MaybeConstraint;
31: use Chromabits\Nucleus\Meditation\Constraints\NumericConstraint;
32: use Chromabits\Nucleus\Meditation\Constraints\PrimitiveTypeConstraint;
33: use Chromabits\Nucleus\Meditation\Constraints\ReadMapConstraint;
34: use Chromabits\Nucleus\Meditation\Primitives\CompoundTypes;
35: use Chromabits\Nucleus\Meditation\Primitives\ScalarTypes;
36: use Chromabits\Nucleus\Meditation\Primitives\SpecialTypes;
37: use Chromabits\Nucleus\Validation\Constraints\BetweenConstraint;
38: use Closure;
39:
40: /**
41: * Class Boa.
42: *
43: * Shortcuts for defining constraints.
44: *
45: * Constriction is a method used by various snake species to kill their prey.
46: * - Wikipedia (2015)
47: *
48: * @author Eduardo Trujillo <ed@chromabits.com>
49: * @package Chromabits\Nucleus\Meditation
50: */
51: class Boa extends BaseObject
52: {
53: /**
54: * @return AnyConstraint
55: */
56: public static function any()
57: {
58: return new AnyConstraint();
59: }
60:
61: /**
62: * @return PrimitiveTypeConstraint
63: */
64: public static function string()
65: {
66: return new PrimitiveTypeConstraint(ScalarTypes::SCALAR_STRING);
67: }
68:
69: /**
70: * @return PrimitiveTypeConstraint
71: */
72: public static function integer()
73: {
74: return new PrimitiveTypeConstraint(ScalarTypes::SCALAR_INTEGER);
75: }
76:
77: /**
78: * @return PrimitiveTypeConstraint
79: */
80: public static function float()
81: {
82: return new PrimitiveTypeConstraint(ScalarTypes::SCALAR_FLOAT);
83: }
84:
85: /**
86: * @return PrimitiveTypeConstraint
87: */
88: public static function boolean()
89: {
90: return new PrimitiveTypeConstraint(ScalarTypes::SCALAR_BOOLEAN);
91: }
92:
93: /**
94: * @return PrimitiveTypeConstraint
95: */
96: public static function arr()
97: {
98: return new PrimitiveTypeConstraint(CompoundTypes::COMPOUND_ARRAY);
99: }
100:
101: /**
102: * @return ListConstraint
103: */
104: public static function lst()
105: {
106: return new ListConstraint();
107: }
108:
109: /**
110: * @return PrimitiveTypeConstraint
111: */
112: public static function object()
113: {
114: return new PrimitiveTypeConstraint(CompoundTypes::COMPOUND_OBJECT);
115: }
116:
117: /**
118: * @return CallableConstraint
119: */
120: public static function func()
121: {
122: return new CallableConstraint();
123: }
124:
125: /**
126: * @param AbstractConstraint $one
127: * @param AbstractConstraint $other
128: *
129: * @return EitherConstraint
130: */
131: public static function either(
132: AbstractConstraint $one,
133: AbstractConstraint $other
134: ) {
135: return new EitherConstraint($one, $other);
136: }
137:
138: /**
139: * @param AbstractConstraint $one
140: *
141: * @return MaybeConstraint
142: */
143: public static function maybe(AbstractConstraint $one)
144: {
145: return new MaybeConstraint($one);
146: }
147:
148: /**
149: * @return MaybeConstraint
150: */
151: public static function maybeString()
152: {
153: return static::maybe(static::string());
154: }
155:
156: /**
157: * @return MaybeConstraint
158: */
159: public static function maybeInteger()
160: {
161: return static::maybe(static::integer());
162: }
163:
164: /**
165: * @return MaybeConstraint
166: */
167: public static function maybeBoolean()
168: {
169: return static::maybe(static::boolean());
170: }
171:
172: /**
173: * @return MaybeConstraint
174: */
175: public static function maybeFloat()
176: {
177: return static::maybe(static::float());
178: }
179:
180: /**
181: * @return MaybeConstraint
182: */
183: public static function maybeList()
184: {
185: return static::maybe(static::lst());
186: }
187:
188: /**
189: * @return MaybeConstraint
190: */
191: public static function maybeArray()
192: {
193: return static::maybe(static::arr());
194: }
195:
196: /**
197: * @return MaybeConstraint
198: */
199: public static function maybeObject()
200: {
201: return static::maybe(static::object());
202: }
203:
204: /**
205: * @param string $of
206: *
207: * @return MaybeConstraint
208: */
209: public static function maybeInstance($of)
210: {
211: return static::maybe(static::instance($of));
212: }
213:
214: /**
215: * @param string $className
216: *
217: * @return ClassTypeConstraint
218: */
219: public static function instance($className)
220: {
221: return new ClassTypeConstraint($className);
222: }
223:
224: /**
225: * @param int|float $min
226: * @param int|float $max
227: *
228: * @return BetweenConstraint
229: */
230: public static function between($min, $max)
231: {
232: return new BetweenConstraint($min, $max);
233: }
234:
235: /**
236: * @param array $allowed
237: *
238: * @return InArrayConstraint
239: */
240: public static function in(array $allowed)
241: {
242: return new InArrayConstraint($allowed);
243: }
244:
245: /**
246: * @param Closure $closure
247: * @param string|null $description
248: *
249: * @return ClosureConstraint
250: */
251: public static function inline(Closure $closure, $description = null)
252: {
253: return new ClosureConstraint($closure, $description);
254: }
255:
256: /**
257: * @return ReadMapConstraint
258: */
259: public static function readMap()
260: {
261: return new ReadMapConstraint();
262: }
263:
264: /**
265: * @return MapConstraint
266: */
267: public static function map()
268: {
269: return new MapConstraint();
270: }
271:
272: /**
273: * @return FoldableConstraint
274: */
275: public static function foldable()
276: {
277: return new FoldableConstraint();
278: }
279:
280: /**
281: * @return LeftFoldableConstraint
282: */
283: public static function leftFoldable()
284: {
285: return new LeftFoldableConstraint();
286: }
287:
288: /**
289: * @return LeftFoldableConstraint
290: */
291: public static function traversable()
292: {
293: return static::leftFoldable();
294: }
295:
296: /**
297: * @param AbstractConstraint $valueConstraint
298: *
299: * @return LeftFoldableOfConstraint
300: */
301: public static function traversableOf(AbstractConstraint $valueConstraint)
302: {
303: return static::leftFoldableOf($valueConstraint);
304: }
305:
306: /**
307: * @param AbstractConstraint $valueConstraint
308: *
309: * @return LeftFoldableOfConstraint
310: */
311: public static function leftFoldableOf(AbstractConstraint $valueConstraint)
312: {
313: return new LeftFoldableOfConstraint($valueConstraint);
314: }
315:
316: /**
317: * @param AbstractConstraint $valueConstraint
318: *
319: * @return ArrayOfConstraint
320: */
321: public static function arrOf(AbstractConstraint $valueConstraint)
322: {
323: return new ArrayOfConstraint($valueConstraint);
324: }
325:
326: /**
327: * @return PrimitiveTypeConstraint
328: */
329: public static function null()
330: {
331: return new PrimitiveTypeConstraint(
332: SpecialTypes::SPECIAL_NULL
333: );
334: }
335:
336: /**
337: * @return PrimitiveTypeConstraint
338: */
339: public static function resource()
340: {
341: return new PrimitiveTypeConstraint(
342: SpecialTypes::SPECIAL_RESOURCE
343: );
344: }
345:
346: /**
347: * @return FunctorConstraint
348: */
349: public static function functor()
350: {
351: return new FunctorConstraint();
352: }
353:
354: /**
355: * @return BooleanConstraint
356: */
357: public static function booleanLike()
358: {
359: return new BooleanConstraint();
360: }
361:
362: /**
363: * @return NumericConstraint
364: */
365: public static function numeric()
366: {
367: return new NumericConstraint();
368: }
369: }
370: