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: 
  8: class Schema
  9: {
 10: 
 11:     /**
 12:      * @var Item[]
 13:      */
 14:     protected $items = [];
 15: 
 16: 
 17:     /**
 18:      * Schema constructor.
 19:      */
 20:     public function __construct()
 21:     {
 22:     }
 23: 
 24: 
 25:     /**
 26:      * @param array $items
 27:      * @return static
 28:      */
 29:     public static function create(array $items = [])
 30:     {
 31:         $schema = new static();
 32:         $schema->addItems($items);
 33:         return $schema;
 34:     }
 35: 
 36: 
 37:     /**
 38:      * @param array|Item[] $items
 39:      */
 40:     public function addItems(array $items)
 41:     {
 42:         foreach ($items as $item) {
 43:             $this->addItem($item);
 44:         }
 45:     }
 46: 
 47: 
 48:     /**
 49:      * @param Item $item
 50:      * @return bool
 51:      */
 52:     public function addItem(Item $item)
 53:     {
 54:         $result = false;
 55: 
 56:         if (! $this->hasItem($item)) {
 57:             $result = true;
 58:             $this->items[] = $item;
 59:         }
 60: 
 61:         return $result;
 62:     }
 63: 
 64: 
 65:     /**
 66:      * @param Item $item
 67:      * @return mixed
 68:      */
 69:     public function hasItem(Item $item)
 70:     {
 71:         return in_array($item, $this->items);
 72:     }
 73: 
 74: 
 75:     /**
 76:      * @param Item $item
 77:      * @return bool
 78:      */
 79:     public function removeItem(Item $item)
 80:     {
 81:         $result = false;
 82: 
 83:         $index = array_search($item, $this->items);
 84:         if ($index !== false) {
 85:             $result = true;
 86:             unset($this->items[$index]);
 87:         }
 88: 
 89:         return $result;
 90:     }
 91: 
 92: 
 93:     /**
 94:      * @param \stdClass $request
 95:      * @return bool
 96:      * @throws InvalidArgumentException
 97:      */
 98:     public function validate($request)
 99:     {
100:         $passed = true;
101: 
102:         foreach ($this->items as $item) {
103:             $itemPassed = $item->validate($request);
104:             if (! $itemPassed) {
105:                 $passed = false;
106:                 break;
107:             }
108:         }
109: 
110:         if (! $passed) {
111:             $requirements = $item->getRequirements();
112:             throw new InvalidArgumentException(null, $requirements);
113:         }
114:         
115:         return $passed;
116:     }
117: 
118: 
119:     /**
120:      * @return Item[]
121:      */
122:     public function getItems()
123:     {
124:        return $this->items;
125:     }
126: 
127: 
128:     /**
129:      * @return \stdClass
130:      */
131:     public function getDefinition()
132:     {
133:         $result = new \stdClass();
134: 
135:         foreach ($this->items as $item) {
136:             $result->{$item->getPropertyName()} = $item->getRequirements();
137:         }
138: 
139:         return $result;
140:     }
141: 
142: }
API documentation generated by ApiGen