1: <?php
2:
3: namespace Racoon\Api\Schema;
4:
5:
6: use TomWright\Validator\Constraint\BoolConstraint;
7: use TomWright\Validator\Constraint\ConstraintGroup;
8: use TomWright\Validator\Constraint\ConstraintGroupTranslator;
9: use TomWright\Validator\Constraint\EmailConstraint;
10: use TomWright\Validator\Constraint\IntConstraint;
11: use TomWright\Validator\Constraint\NotNullConstraint;
12: use TomWright\Validator\Constraint\NullConstraint;
13: use TomWright\Validator\Constraint\StringConstraint;
14: use TomWright\Validator\Constraint\ArrayConstraint;
15: use TomWright\Validator\Constraint\ObjectConstraint;
16:
17: class Translator
18: {
19:
20: 21: 22:
23: protected $item;
24:
25: 26: 27:
28: protected $propertyName;
29:
30: 31: 32:
33: protected $readableName;
34:
35: 36: 37:
38: protected $currentConstraintGroup;
39:
40:
41: 42: 43:
44: public function getItem()
45: {
46: if (! is_object($this->item)) {
47: $this->item = new Item($this->propertyName, $this->readableName);
48: }
49: return $this->item;
50: }
51:
52:
53: 54: 55:
56: public function returnItem()
57: {
58: if ($this->hasCurrentConstraintGroup()) {
59: $this->getItem()->getValidator()->addConstraintGroup($this->getCurrentConstraintGroup());
60: }
61: return $this->getItem();
62: }
63:
64:
65: 66: 67: 68:
69: public function newConstraintGroup()
70: {
71: if ($this->hasCurrentConstraintGroup()) {
72: $this->getItem()->getValidator()->addConstraintGroup($this->getCurrentConstraintGroup());
73: $this->currentConstraintGroup = null;
74: }
75: return $this;
76: }
77:
78:
79: 80: 81:
82: public function getCurrentConstraintGroup()
83: {
84: if (! $this->hasCurrentConstraintGroup()) {
85: $this->currentConstraintGroup = ConstraintGroupTranslator::create();
86: }
87: return $this->currentConstraintGroup;
88: }
89:
90:
91: 92: 93:
94: public function hasCurrentConstraintGroup()
95: {
96: return (is_object($this->currentConstraintGroup));
97: }
98:
99:
100: 101: 102: 103:
104: public function alt()
105: {
106: return $this->newConstraintGroup();
107: }
108:
109:
110: 111: 112: 113: 114:
115: public static function item($propertyName, $readableName = null)
116: {
117: $translator = new static();
118:
119: $translator->propertyName = $propertyName;
120: if ($readableName === null) {
121: $readableName = $propertyName;
122: }
123: $translator->readableName = $readableName;
124:
125: return $translator;
126: }
127:
128:
129: 130: 131:
132: public function required()
133: {
134: $this->getItem()->setRequired(true);
135: return $this;
136: }
137:
138:
139: 140: 141:
142: public function optional()
143: {
144: $this->getItem()->setRequired(false);
145: return $this;
146: }
147:
148:
149: 150: 151: 152:
153: public function optionalConstraintGroup(ConstraintGroup $group = null)
154: {
155: $this->getItem()->setOptionalConstraintGroup($group);
156: return $this;
157: }
158:
159:
160: 161: 162: 163:
164: public function isBool($requiredValue = null)
165: {
166: $this->getCurrentConstraintGroup()->isBool($requiredValue);
167: return $this;
168: }
169:
170:
171: 172: 173:
174: public function isTrue()
175: {
176: $this->getCurrentConstraintGroup()->isTrue();
177: return $this;
178: }
179:
180:
181: 182: 183:
184: public function isFalse()
185: {
186: $this->getCurrentConstraintGroup()->isFalse();
187: return $this;
188: }
189:
190:
191: 192: 193: 194: 195:
196: public function isString($minLength = null, $maxLength = null)
197: {
198: $this->getCurrentConstraintGroup()->isString($minLength, $maxLength);
199: return $this;
200: }
201:
202:
203: 204: 205: 206: 207:
208: public function isInt($minValue = null, $maxValue = null)
209: {
210: $this->getCurrentConstraintGroup()->isInt($minValue, $maxValue);
211: return $this;
212: }
213:
214:
215: 216: 217: 218: 219:
220: public function isFloat($minValue = null, $maxValue = null)
221: {
222: $this->getCurrentConstraintGroup()->isFloat($minValue, $maxValue);
223: return $this;
224: }
225:
226:
227: 228: 229: 230: 231:
232: public function isNumeric($minValue = null, $maxValue = null)
233: {
234: $this->getCurrentConstraintGroup()->isNumeric($minValue, $maxValue);
235: return $this;
236: }
237:
238:
239: 240: 241:
242: public function isNull()
243: {
244: $this->getCurrentConstraintGroup()->isNull();
245: return $this;
246: }
247:
248:
249: 250: 251:
252: public function notNull()
253: {
254: $this->getCurrentConstraintGroup()->notNull();
255: return $this;
256: }
257:
258:
259: 260: 261:
262: public function isEmail()
263: {
264: $this->getCurrentConstraintGroup()->isEmail();
265: return $this;
266: }
267:
268:
269: 270: 271:
272: public function isArray()
273: {
274: $this->getCurrentConstraintGroup()->isArray();
275: return $this;
276: }
277:
278:
279: 280: 281: 282:
283: public function isObject($requiredClass = null)
284: {
285: $this->getCurrentConstraintGroup()->isObject($requiredClass);
286: return $this;
287: }
288:
289: }