diff options
author | Hannes Kindströmmer <hannes@kindstrommer.se> | 2017-03-09 15:19:34 +0100 |
---|---|---|
committer | Hannes Kindströmmer <hannes@kindstrommer.se> | 2017-03-09 15:19:34 +0100 |
commit | c7c8e41696dcf14bf43578cf1b5a2e1c4080267e (patch) | |
tree | a8be12f7cae65056a04b6c5d6ffe0a06dff2f322 | |
parent | f351106405a90286b761e3c69b5c1d5591b4917b (diff) | |
download | ip1-php-sdk-c7c8e41696dcf14bf43578cf1b5a2e1c4080267e.zip ip1-php-sdk-c7c8e41696dcf14bf43578cf1b5a2e1c4080267e.tar.gz ip1-php-sdk-c7c8e41696dcf14bf43578cf1b5a2e1c4080267e.tar.bz2 |
Add documentation
-rw-r--r-- | src/Core/ClassValidationArray.php | 83 | ||||
-rw-r--r-- | src/Core/ProcessedComponent.php | 1 |
2 files changed, 84 insertions, 0 deletions
diff --git a/src/Core/ClassValidationArray.php b/src/Core/ClassValidationArray.php new file mode 100644 index 0000000..fabe687 --- /dev/null +++ b/src/Core/ClassValidationArray.php @@ -0,0 +1,83 @@ +<?php + +namespace IP1\RESTClient\Core; + +/** +* +* An extension of ArrayObject that ensures that there's only one type of object in the array. +* @package IP1\RESTClient\Core +*/ +class ClassValidationArray extends \ArrayObject implements \JsonSerializable +{ + + /** + * Stores the only allowed class that is allowed in the array. + * @var string $class + */ + private $class; + /** + * @throws InvalidArgumentException + */ + public function __construct($input = []) + { + if (is_array($input)) { + foreach ($input as $key => $value) { + if (!is_object($value)) { + throw new \InvalidArgumentException( + get_class($value). + " is not the same class as ". + $this->class . + " given in the first index of argument array." + ); + } + if (!isset($this->class)) { + $this->class = get_class($value); + } + if ($this->class === get_class($value)) { + parent::offsetSet(null, $value); + } else { + throw new \InvalidArgumentException($this->class. " expected as set by the first index in given array. " . get_class($value) . " given in index $key of given array."); + } + } + } elseif (is_object($input)) { + $this->class = get_class($input); + parent::offsetSet(null, $input); + } else { + throw new \InvalidArgumentException(gettype($input). " given argument was not an array or an object."); + } + } + + /** + * Sets the value at the specified index to value if + * @throws InvalidArgumentException + */ + public function offsetSet($index, $value): void + { + if (!is_object($value)) { + throw new \InvalidArgumentException("Excepcted object, ". gettype($value) ." given."); + } + if (!isset($this->class)) { + $this->class = get_class($value); + } + if (get_class($value) === $this->class) { + parent::offsetSet($index, $value); + } else { + throw new \InvalidArgumentException("Excepcted $this->class, ". get_class($value) ." given."); + } + } + public function getContainedClass(): string + { + return $this->class; + } + /** + * {@inheritDoc} + */ + public function JsonSerialize(): array + { + $returnArray = []; + foreach ($this->getArrayCopy() as $value) { + $returnArray[] = $value->JsonSerialize(); + } + return $returnArray; + } +} diff --git a/src/Core/ProcessedComponent.php b/src/Core/ProcessedComponent.php index 787d29c..dc2f530 100644 --- a/src/Core/ProcessedComponent.php +++ b/src/Core/ProcessedComponent.php @@ -26,6 +26,7 @@ interface ProcessedComponent extends \JsonSerializable public function getCreated(\DateTimeZone $timezone = null): ?\DateTime; /** * Serializes the object to a value that can be serialized natively by json_encode(). + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php */ public function jsonSerialize(): array; } |