Overview

Namespaces

  • Racoon
    • Api
      • Auth
      • Exception
      • Response
        • Format
        • Generate
      • Schema

Classes

  • Racoon\Api\App
  • Racoon\Api\Auth\ApiKeyAuthenticator
  • Racoon\Api\Auth\NoAuthenticator
  • Racoon\Api\Controller
  • Racoon\Api\Request
  • Racoon\Api\Response\Format\JsonFormatter
  • Racoon\Api\Response\Generate\DetailedResponse
  • Racoon\Api\Schema\Item
  • Racoon\Api\Schema\Schema
  • Racoon\Api\Schema\Translator

Interfaces

  • Racoon\Api\Auth\AuthInterface
  • Racoon\Api\Response\Format\FormatterInterface
  • Racoon\Api\Response\Generate\GeneratorInterface

Exceptions

  • Racoon\Api\Exception\AuthenticationException
  • Racoon\Api\Exception\Exception
  • Racoon\Api\Exception\InvalidArgumentException
  • Racoon\Api\Exception\InvalidJsonException
  • Racoon\Api\Exception\InvalidRouteException
  • Racoon\Api\Exception\NotFoundException
  • Racoon\Api\Exception\ResponseFormattingException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Racoon\Api\Schema;
  4: 
  5: 
  6: use Racoon\Api\Exception\InvalidArgumentException;
  7: use TomWright\Validator\Constraint\ConstraintGroup;
  8: use TomWright\Validator\Validator;
  9: 
 10: class Item
 11: {
 12: 
 13:     /**
 14:      * @var null|string
 15:      */
 16:     protected $propertyName;
 17: 
 18:     /**
 19:      * @var null|mixed
 20:      */
 21:     protected $propertyValue;
 22: 
 23:     /**
 24:      * @var null|string
 25:      */
 26:     protected $readableName;
 27: 
 28:     /**
 29:      * @var Validator
 30:      */
 31:     protected $validator;
 32: 
 33:     /**
 34:      * @var bool
 35:      */
 36:     protected $required;
 37: 
 38:     /**
 39:      * @var ConstraintGroup
 40:      */
 41:     protected $optionalConstraintGroup;
 42: 
 43: 
 44:     /**
 45:      * Item constructor.
 46:      * @param string $propertyName
 47:      * @param string $readableName
 48:      * @param array|ConstraintGroup[] $constraintGroups
 49:      * @param bool $required
 50:      * @param ConstraintGroup $optionalConstraintGroup
 51:      */
 52:     public function __construct($propertyName, $readableName, array $constraintGroups = [], $required = true, ConstraintGroup $optionalConstraintGroup = null)
 53:     {
 54:         $this->validator = new Validator(null, null);
 55:         $this->setPropertyName($propertyName);
 56:         $this->setReadableName($readableName);
 57:         $this->setRequired($required);
 58:         if (is_array($constraintGroups) && count($constraintGroups) > 0) {
 59:             foreach ($constraintGroups as $constraintGroup) {
 60:                 $this->validator->addConstraintGroup($constraintGroup);
 61:             }
 62:         }
 63:         $this->setOptionalConstraintGroup($optionalConstraintGroup);
 64:     }
 65: 
 66: 
 67:     /**
 68:      * @param $propertyName
 69:      * @param $readableName
 70:      * @param array|ConstraintGroup[] $constraintGroups
 71:      * @param bool $required
 72:      * @param ConstraintGroup $optionalConstraintGroup
 73:      * @return static
 74:      */
 75:     public static function create($propertyName, $readableName, array $constraintGroups = [], $required = true, ConstraintGroup $optionalConstraintGroup = null)
 76:     {
 77:         $item = new static($propertyName, $readableName, $constraintGroups, $required, $optionalConstraintGroup);
 78:         return $item;
 79:     }
 80: 
 81: 
 82:     /**
 83:      * @param \stdClass $request
 84:      * @return bool
 85:      * @throws InvalidArgumentException
 86:      * @throws \TomWright\Validator\Exception\FailedConstraintException
 87:      */
 88:     public function validate($request)
 89:     {
 90:         $valueExists = (is_object($request) && property_exists($request, $this->getPropertyName()));
 91:         if ($this->isRequired() && ! $valueExists) {
 92:             throw new InvalidArgumentException(null, "Missing required field: {$this->getPropertyName()}");
 93:         } elseif (! $this->isRequired()) {
 94:             if (! $valueExists) {
 95:                 $this->validator->addConstraintGroup(
 96:                     ConstraintGroup::create([
 97:                         NullConstraint::create(),
 98:                     ])
 99:                 );
100:             } elseif (is_object($this->getOptionalConstraintGroup())) {
101:                 $this->validator->addConstraintGroup($this->getOptionalConstraintGroup());
102:             }
103:         }
104: 
105:         $value = null;
106:         if ($valueExists) {
107:             $value = $request->{$this->getPropertyName()};
108:         }
109:         $this->setPropertyValue($value);
110: 
111:         return $this->validator->validate()->hasPassed();
112:     }
113: 
114: 
115:     /**
116:      * @return null|string
117:      */
118:     public function getPropertyName()
119:     {
120:         return $this->propertyName;
121:     }
122: 
123: 
124:     /**
125:      * @param null|string $propertyName
126:      * @return $this
127:      */
128:     public function setPropertyName($propertyName)
129:     {
130:         $this->propertyName = $propertyName;
131:         return $this;
132:     }
133: 
134: 
135:     /**
136:      * @return mixed|null
137:      */
138:     public function getPropertyValue()
139:     {
140:         return $this->propertyValue;
141:     }
142: 
143: 
144:     /**
145:      * @param mixed|null $propertyValue
146:      * @return $this
147:      */
148:     public function setPropertyValue($propertyValue)
149:     {
150:         $this->propertyValue = $propertyValue;
151:         $this->validator->setValue($propertyValue);
152:         return $this;
153:     }
154: 
155: 
156:     /**
157:      * @return null|string
158:      */
159:     public function getReadableName()
160:     {
161:         return $this->readableName;
162:     }
163: 
164: 
165:     /**
166:      * @param null|string $readableName
167:      * @return $this
168:      */
169:     public function setReadableName($readableName)
170:     {
171:         $this->readableName = $readableName;
172:         $this->validator->setName($readableName);
173:         return $this;
174:     }
175: 
176: 
177:     /**
178:      * @return Validator
179:      */
180:     public function getValidator()
181:     {
182:         return $this->validator;
183:     }
184: 
185: 
186:     /**
187:      * @return string
188:      */
189:     public function getRequirements()
190:     {
191:         return $this->getValidator()->getRequirementsString();
192:     }
193: 
194: 
195:     /**
196:      * @return boolean
197:      */
198:     public function isRequired()
199:     {
200:         return $this->required;
201:     }
202: 
203: 
204:     /**
205:      * @param boolean $required
206:      */
207:     public function setRequired($required)
208:     {
209:         $this->required = $required;
210:     }
211: 
212: 
213:     /**
214:      * @return ConstraintGroup
215:      */
216:     public function getOptionalConstraintGroup()
217:     {
218:         return $this->optionalConstraintGroup;
219:     }
220: 
221: 
222:     /**
223:      * @param ConstraintGroup $optionalConstraintGroup
224:      */
225:     public function setOptionalConstraintGroup($optionalConstraintGroup)
226:     {
227:         $this->optionalConstraintGroup = $optionalConstraintGroup;
228:     }
229: 
230: }
API documentation generated by ApiGen