Open Journal Systems  3.3.0
Parameter.php
1 <?php
2 
4 
6 
10 class Parameter
11 {
12  protected $name;
13  protected $description;
15  protected $type;
16  protected $required;
17  protected $enum;
18  protected $pattern;
19  protected $minimum;
20  protected $maximum;
21  protected $minLength;
22  protected $maxLength;
23  protected $minItems;
24  protected $maxItems;
25  protected $default;
26  protected $static;
27  protected $instanceOf;
28  protected $filters;
29  protected $location;
30  protected $sentAs;
31  protected $data;
32  protected $properties = array();
34  protected $items;
35  protected $parent;
36  protected $ref;
37  protected $format;
38  protected $propertiesCache = null;
39 
95  public function __construct(array $data = array(), ServiceDescriptionInterface $description = null)
96  {
97  if ($description) {
98  if (isset($data['$ref'])) {
99  if ($model = $description->getModel($data['$ref'])) {
100  $data = $model->toArray() + $data;
101  }
102  } elseif (isset($data['extends'])) {
103  // If this parameter extends from another parameter then start with the actual data
104  // union in the parent's data (e.g. actual supersedes parent)
105  if ($extends = $description->getModel($data['extends'])) {
106  $data += $extends->toArray();
107  }
108  }
109  }
110 
111  // Pull configuration data into the parameter
112  foreach ($data as $key => $value) {
113  $this->{$key} = $value;
114  }
115 
116  $this->serviceDescription = $description;
117  $this->required = (bool) $this->required;
118  $this->data = (array) $this->data;
119 
120  if ($this->filters) {
121  $this->setFilters((array) $this->filters);
122  }
123 
124  if ($this->type == 'object' && $this->additionalProperties === null) {
125  $this->additionalProperties = true;
126  }
127  }
128 
134  public function toArray()
135  {
136  static $checks = array('required', 'description', 'static', 'type', 'format', 'instanceOf', 'location', 'sentAs',
137  'pattern', 'minimum', 'maximum', 'minItems', 'maxItems', 'minLength', 'maxLength', 'data', 'enum',
138  'filters');
139 
140  $result = array();
141 
142  // Anything that is in the `Items` attribute of an array *must* include it's name if available
143  if ($this->parent instanceof self && $this->parent->getType() == 'array' && isset($this->name)) {
144  $result['name'] = $this->name;
145  }
146 
147  foreach ($checks as $c) {
148  if ($value = $this->{$c}) {
149  $result[$c] = $value;
150  }
151  }
152 
153  if ($this->default !== null) {
154  $result['default'] = $this->default;
155  }
156 
157  if ($this->items !== null) {
158  $result['items'] = $this->getItems()->toArray();
159  }
160 
161  if ($this->additionalProperties !== null) {
162  $result['additionalProperties'] = $this->getAdditionalProperties();
163  if ($result['additionalProperties'] instanceof self) {
164  $result['additionalProperties'] = $result['additionalProperties']->toArray();
165  }
166  }
167 
168  if ($this->type == 'object' && $this->properties) {
169  $result['properties'] = array();
170  foreach ($this->getProperties() as $name => $property) {
171  $result['properties'][$name] = $property->toArray();
172  }
173  }
174 
175  return $result;
176  }
177 
185  public function getValue($value)
186  {
187  if ($this->static || ($this->default !== null && $value === null)) {
188  return $this->default;
189  }
190 
191  return $value;
192  }
193 
201  public function filter($value)
202  {
203  // Formats are applied exclusively and supersed filters
204  if ($this->format) {
205  return SchemaFormatter::format($this->format, $value);
206  }
207 
208  // Convert Boolean values
209  if ($this->type == 'boolean' && !is_bool($value)) {
210  $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
211  }
212 
213  // Apply filters to the value
214  if ($this->filters) {
215  foreach ($this->filters as $filter) {
216  if (is_array($filter)) {
217  // Convert complex filters that hold value place holders
218  foreach ($filter['args'] as &$data) {
219  if ($data == '@value') {
220  $data = $value;
221  } elseif ($data == '@api') {
222  $data = $this;
223  }
224  }
225  $value = call_user_func_array($filter['method'], $filter['args']);
226  } else {
227  $value = call_user_func($filter, $value);
228  }
229  }
230  }
231 
232  return $value;
233  }
234 
240  public function getName()
241  {
242  return $this->name;
243  }
244 
250  public function getWireName()
251  {
252  return $this->sentAs ?: $this->name;
253  }
254 
262  public function setName($name)
263  {
264  $this->name = $name;
265 
266  return $this;
267  }
268 
274  public function getType()
275  {
276  return $this->type;
277  }
278 
286  public function setType($type)
287  {
288  $this->type = $type;
289 
290  return $this;
291  }
292 
298  public function getRequired()
299  {
300  return $this->required;
301  }
302 
310  public function setRequired($isRequired)
311  {
312  $this->required = (bool) $isRequired;
313 
314  return $this;
315  }
316 
322  public function getDefault()
323  {
324  return $this->default;
325  }
326 
334  public function setDefault($default)
335  {
336  $this->default = $default;
337 
338  return $this;
339  }
340 
346  public function getDescription()
347  {
348  return $this->description;
349  }
350 
358  public function setDescription($description)
359  {
360  $this->description = $description;
361 
362  return $this;
363  }
364 
370  public function getMinimum()
371  {
372  return $this->minimum;
373  }
374 
382  public function setMinimum($min)
383  {
384  $this->minimum = $min;
385 
386  return $this;
387  }
388 
394  public function getMaximum()
395  {
396  return $this->maximum;
397  }
398 
406  public function setMaximum($max)
407  {
408  $this->maximum = $max;
409 
410  return $this;
411  }
412 
418  public function getMinLength()
419  {
420  return $this->minLength;
421  }
422 
430  public function setMinLength($min)
431  {
432  $this->minLength = $min;
433 
434  return $this;
435  }
436 
442  public function getMaxLength()
443  {
444  return $this->maxLength;
445  }
446 
454  public function setMaxLength($max)
455  {
456  $this->maxLength = $max;
457 
458  return $this;
459  }
460 
466  public function getMaxItems()
467  {
468  return $this->maxItems;
469  }
470 
478  public function setMaxItems($max)
479  {
480  $this->maxItems = $max;
481 
482  return $this;
483  }
484 
490  public function getMinItems()
491  {
492  return $this->minItems;
493  }
494 
502  public function setMinItems($min)
503  {
504  $this->minItems = $min;
505 
506  return $this;
507  }
508 
514  public function getLocation()
515  {
516  return $this->location;
517  }
518 
526  public function setLocation($location)
527  {
528  $this->location = $location;
529 
530  return $this;
531  }
532 
539  public function getSentAs()
540  {
541  return $this->sentAs;
542  }
543 
551  public function setSentAs($name)
552  {
553  $this->sentAs = $name;
554 
555  return $this;
556  }
557 
566  public function getData($name = null)
567  {
568  if (!$name) {
569  return $this->data;
570  }
571 
572  if (isset($this->data[$name])) {
573  return $this->data[$name];
574  } elseif (isset($this->{$name})) {
575  return $this->{$name};
576  }
577 
578  return null;
579  }
580 
589  public function setData($nameOrData, $data = null)
590  {
591  if (is_array($nameOrData)) {
592  $this->data = $nameOrData;
593  } else {
594  $this->data[$nameOrData] = $data;
595  }
596 
597  return $this;
598  }
599 
605  public function getStatic()
606  {
607  return $this->static;
608  }
609 
617  public function setStatic($static)
618  {
619  $this->static = (bool) $static;
620 
621  return $this;
622  }
623 
629  public function getFilters()
630  {
631  return $this->filters ?: array();
632  }
633 
641  public function setFilters(array $filters)
642  {
643  $this->filters = array();
644  foreach ($filters as $filter) {
645  $this->addFilter($filter);
646  }
647 
648  return $this;
649  }
650 
659  public function addFilter($filter)
660  {
661  if (is_array($filter)) {
662  if (!isset($filter['method'])) {
663  throw new InvalidArgumentException('A [method] value must be specified for each complex filter');
664  }
665  }
666 
667  if (!$this->filters) {
668  $this->filters = array($filter);
669  } else {
670  $this->filters[] = $filter;
671  }
672 
673  return $this;
674  }
675 
681  public function getParent()
682  {
683  return $this->parent;
684  }
685 
693  public function setParent($parent)
694  {
695  $this->parent = $parent;
696 
697  return $this;
698  }
699 
705  public function getProperties()
706  {
707  if (!$this->propertiesCache) {
708  $this->propertiesCache = array();
709  foreach (array_keys($this->properties) as $name) {
710  $this->propertiesCache[$name] = $this->getProperty($name);
711  }
712  }
713 
714  return $this->propertiesCache;
715  }
716 
724  public function getProperty($name)
725  {
726  if (!isset($this->properties[$name])) {
727  return null;
728  }
729 
730  if (!($this->properties[$name] instanceof self)) {
731  $this->properties[$name]['name'] = $name;
732  $this->properties[$name] = new static($this->properties[$name], $this->serviceDescription);
733  $this->properties[$name]->setParent($this);
734  }
735 
736  return $this->properties[$name];
737  }
738 
746  public function removeProperty($name)
747  {
748  unset($this->properties[$name]);
749  $this->propertiesCache = null;
750 
751  return $this;
752  }
753 
761  public function addProperty(Parameter $property)
762  {
763  $this->properties[$property->getName()] = $property;
764  $property->setParent($this);
765  $this->propertiesCache = null;
766 
767  return $this;
768  }
769 
775  public function getAdditionalProperties()
776  {
777  if (is_array($this->additionalProperties)) {
778  $this->additionalProperties = new static($this->additionalProperties, $this->serviceDescription);
779  $this->additionalProperties->setParent($this);
780  }
781 
783  }
784 
792  public function setAdditionalProperties($additional)
793  {
794  $this->additionalProperties = $additional;
795 
796  return $this;
797  }
798 
806  public function setItems(Parameter $items = null)
807  {
808  if ($this->items = $items) {
809  $this->items->setParent($this);
810  }
811 
812  return $this;
813  }
814 
820  public function getItems()
821  {
822  if (is_array($this->items)) {
823  $this->items = new static($this->items, $this->serviceDescription);
824  $this->items->setParent($this);
825  }
826 
827  return $this->items;
828  }
829 
835  public function getInstanceOf()
836  {
837  return $this->instanceOf;
838  }
839 
847  public function setInstanceOf($instanceOf)
848  {
849  $this->instanceOf = $instanceOf;
850 
851  return $this;
852  }
853 
859  public function getEnum()
860  {
861  return $this->enum;
862  }
863 
871  public function setEnum(array $enum = null)
872  {
873  $this->enum = $enum;
874 
875  return $this;
876  }
877 
883  public function getPattern()
884  {
885  return $this->pattern;
886  }
887 
895  public function setPattern($pattern)
896  {
897  $this->pattern = $pattern;
898 
899  return $this;
900  }
901 
907  public function getFormat()
908  {
909  return $this->format;
910  }
911 
919  public function setFormat($format)
920  {
921  $this->format = $format;
922 
923  return $this;
924  }
925 }
Guzzle\Service\Description\Parameter\getItems
getItems()
Definition: Parameter.php:820
Guzzle\Service\Description\Parameter\$static
$static
Definition: Parameter.php:26
Guzzle\Service\Description\Parameter\filter
filter($value)
Definition: Parameter.php:201
Guzzle\Service\Description\Parameter\getProperty
getProperty($name)
Definition: Parameter.php:724
Guzzle\Service\Description\Parameter\$parent
$parent
Definition: Parameter.php:35
Guzzle\Service\Description\Parameter\setInstanceOf
setInstanceOf($instanceOf)
Definition: Parameter.php:847
Guzzle\Service\Description\Parameter\$filters
$filters
Definition: Parameter.php:28
Guzzle\Service\Description\Parameter\$enum
$enum
Definition: Parameter.php:17
Guzzle\Service\Description\Parameter\$maxItems
$maxItems
Definition: Parameter.php:24
Guzzle\Service\Description\Parameter\$location
$location
Definition: Parameter.php:29
Guzzle\Service\Description\Parameter\$format
$format
Definition: Parameter.php:37
Guzzle\Service\Description\Parameter\getMaximum
getMaximum()
Definition: Parameter.php:394
Guzzle\Service\Description\Parameter\getMinimum
getMinimum()
Definition: Parameter.php:370
Guzzle\Service\Description\Parameter\$items
$items
Definition: Parameter.php:34
Guzzle\Service\Description\Parameter
Definition: Parameter.php:10
Guzzle\Service\Description\Parameter\getMinItems
getMinItems()
Definition: Parameter.php:490
Guzzle\Service\Description\Parameter\$properties
$properties
Definition: Parameter.php:32
Guzzle\Service\Description\Parameter\getValue
getValue($value)
Definition: Parameter.php:185
Guzzle\Service\Description\Parameter\setData
setData($nameOrData, $data=null)
Definition: Parameter.php:589
Guzzle\Service\Description\Parameter\$minItems
$minItems
Definition: Parameter.php:23
Guzzle\Service\Description\Parameter\$name
$name
Definition: Parameter.php:12
Guzzle\Service\Description\Parameter\setMaxLength
setMaxLength($max)
Definition: Parameter.php:454
Guzzle\Service\Description\Parameter\setPattern
setPattern($pattern)
Definition: Parameter.php:895
Guzzle\Service\Description\Parameter\setAdditionalProperties
setAdditionalProperties($additional)
Definition: Parameter.php:792
Guzzle\Service\Description\Parameter\setDescription
setDescription($description)
Definition: Parameter.php:358
Guzzle\Service\Description\Parameter\$ref
$ref
Definition: Parameter.php:36
Guzzle\Service\Description\Parameter\getRequired
getRequired()
Definition: Parameter.php:298
Guzzle\Service\Description\Parameter\$maximum
$maximum
Definition: Parameter.php:20
Guzzle\Service\Description\ServiceDescriptionInterface
Definition: ServiceDescriptionInterface.php:8
Guzzle\Service\Description\Parameter\addFilter
addFilter($filter)
Definition: Parameter.php:659
Guzzle\Service\Description\Parameter\$sentAs
$sentAs
Definition: Parameter.php:30
Guzzle\Service\Description\Parameter\getMaxLength
getMaxLength()
Definition: Parameter.php:442
Guzzle\Service\Description\Parameter\setType
setType($type)
Definition: Parameter.php:286
Guzzle\Service\Description\Parameter\$instanceOf
$instanceOf
Definition: Parameter.php:27
Guzzle\Service\Description\Parameter\$pattern
$pattern
Definition: Parameter.php:18
Guzzle\Service\Description\Parameter\getStatic
getStatic()
Definition: Parameter.php:605
Guzzle\Service\Description\Parameter\getPattern
getPattern()
Definition: Parameter.php:883
Guzzle\Service\Description\Parameter\setFilters
setFilters(array $filters)
Definition: Parameter.php:641
Guzzle\Service\Description\Parameter\$description
$description
Definition: Parameter.php:13
Guzzle\Service\Description\Parameter\setLocation
setLocation($location)
Definition: Parameter.php:526
Guzzle\Service\Description\Parameter\$additionalProperties
$additionalProperties
Definition: Parameter.php:33
Guzzle\Service\Description
Definition: Operation.php:3
Guzzle\Common\Exception\InvalidArgumentException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/InvalidArgumentException.php:5
Guzzle\Service\Description\Parameter\$required
$required
Definition: Parameter.php:16
Guzzle\Service\Description\Parameter\removeProperty
removeProperty($name)
Definition: Parameter.php:746
Guzzle\Service\Description\Parameter\setName
setName($name)
Definition: Parameter.php:262
Guzzle\Service\Description\Parameter\setDefault
setDefault($default)
Definition: Parameter.php:334
Guzzle\Service\Description\Parameter\$propertiesCache
$propertiesCache
Definition: Parameter.php:38
Guzzle\Service\Description\Parameter\getParent
getParent()
Definition: Parameter.php:681
Guzzle\Service\Description\Parameter\setMinimum
setMinimum($min)
Definition: Parameter.php:382
Guzzle\Service\Description\Parameter\getMaxItems
getMaxItems()
Definition: Parameter.php:466
Guzzle\Service\Description\Parameter\$data
$data
Definition: Parameter.php:31
Guzzle\Service\Description\Parameter\toArray
toArray()
Definition: Parameter.php:134
Guzzle\Service\Description\Parameter\getWireName
getWireName()
Definition: Parameter.php:250
Guzzle\Service\Description\Parameter\getProperties
getProperties()
Definition: Parameter.php:705
Guzzle\Service\Description\Parameter\$maxLength
$maxLength
Definition: Parameter.php:22
Guzzle\Service\Description\Parameter\getInstanceOf
getInstanceOf()
Definition: Parameter.php:835
Guzzle\Service\Description\Parameter\getName
getName()
Definition: Parameter.php:240
Guzzle\Service\Description\Parameter\setFormat
setFormat($format)
Definition: Parameter.php:919
Guzzle\Service\Description\Parameter\setRequired
setRequired($isRequired)
Definition: Parameter.php:310
Guzzle\Service\Description\Parameter\$minimum
$minimum
Definition: Parameter.php:19
Guzzle\Service\Description\Parameter\setItems
setItems(Parameter $items=null)
Definition: Parameter.php:806
Guzzle\Service\Description\Parameter\getFilters
getFilters()
Definition: Parameter.php:629
Guzzle\Service\Description\Parameter\setParent
setParent($parent)
Definition: Parameter.php:693
Guzzle\Service\Description\Parameter\getDescription
getDescription()
Definition: Parameter.php:346
Guzzle\Service\Description\SchemaFormatter\format
static format($format, $value)
Definition: SchemaFormatter.php:23
Guzzle\Service\Description\Parameter\getDefault
getDefault()
Definition: Parameter.php:322
Guzzle\Service\Description\Parameter\$type
$type
Definition: Parameter.php:15
Guzzle\Service\Description\Parameter\$default
$default
Definition: Parameter.php:25
Guzzle\Service\Description\Parameter\setMaxItems
setMaxItems($max)
Definition: Parameter.php:478
Guzzle\Service\Description\Parameter\setEnum
setEnum(array $enum=null)
Definition: Parameter.php:871
Guzzle\Service\Description\Parameter\getAdditionalProperties
getAdditionalProperties()
Definition: Parameter.php:775
Guzzle\Service\Description\Parameter\$serviceDescription
$serviceDescription
Definition: Parameter.php:14
Guzzle\Service\Description\Parameter\__construct
__construct(array $data=array(), ServiceDescriptionInterface $description=null)
Definition: Parameter.php:95
Guzzle\Service\Description\Parameter\setMinLength
setMinLength($min)
Definition: Parameter.php:430
Guzzle\Service\Description\Parameter\setMaximum
setMaximum($max)
Definition: Parameter.php:406
Guzzle\Service\Description\Parameter\$minLength
$minLength
Definition: Parameter.php:21
Guzzle\Service\Description\Parameter\addProperty
addProperty(Parameter $property)
Definition: Parameter.php:761
Guzzle\Service\Description\Parameter\getData
getData($name=null)
Definition: Parameter.php:566
Guzzle\Service\Description\Parameter\getType
getType()
Definition: Parameter.php:274
Guzzle\Service\Description\Parameter\setStatic
setStatic($static)
Definition: Parameter.php:617
Guzzle\Service\Description\Parameter\setSentAs
setSentAs($name)
Definition: Parameter.php:551
Guzzle\Service\Description\Parameter\getLocation
getLocation()
Definition: Parameter.php:514
Guzzle\Service\Description\Parameter\getMinLength
getMinLength()
Definition: Parameter.php:418
Guzzle\Service\Description\Parameter\setMinItems
setMinItems($min)
Definition: Parameter.php:502
Guzzle\Service\Description\Parameter\getSentAs
getSentAs()
Definition: Parameter.php:539
Guzzle\Service\Description\Parameter\getFormat
getFormat()
Definition: Parameter.php:907
Guzzle\Service\Description\Parameter\getEnum
getEnum()
Definition: Parameter.php:859