summaryrefslogtreecommitdiffstats
path: root/src/Core/ClassValidationArray.php
diff options
context:
space:
mode:
authorHannes Kindströmmer <hannes@kindstrommer.se>2017-03-09 16:54:46 +0100
committerHannes Kindströmmer <hannes@kindstrommer.se>2017-03-09 16:54:46 +0100
commit5504639d9d9c9b247fb05c627c85a212b96405e7 (patch)
treee8376c7b462802e495e13067d90a569d95fa75b5 /src/Core/ClassValidationArray.php
parentc7c8e41696dcf14bf43578cf1b5a2e1c4080267e (diff)
downloadip1-php-sdk-5504639d9d9c9b247fb05c627c85a212b96405e7.zip
ip1-php-sdk-5504639d9d9c9b247fb05c627c85a212b96405e7.tar.gz
ip1-php-sdk-5504639d9d9c9b247fb05c627c85a212b96405e7.tar.bz2
Add documentation and tidy up exception messages
Added phpDoc comments (and some general ones). Fixed typo in function name. Added phpunit tests for the ClassValidationArray. Errors that was found in the tests has been corrected for in the ClassValidationArray class. Signed-off-by: Hannes Kindströmmer <hannes@kindstrommer.se>
Diffstat (limited to 'src/Core/ClassValidationArray.php')
-rw-r--r--src/Core/ClassValidationArray.php34
1 files changed, 23 insertions, 11 deletions
diff --git a/src/Core/ClassValidationArray.php b/src/Core/ClassValidationArray.php
index fabe687..1b342c0 100644
--- a/src/Core/ClassValidationArray.php
+++ b/src/Core/ClassValidationArray.php
@@ -16,6 +16,13 @@ class ClassValidationArray extends \ArrayObject implements \JsonSerializable
*/
private $class;
/**
+ * Takes the input and puts it in the contained array.
+ *
+ * If the input is an object it will add the object to the array and set the object's class to the only allowed class
+ * If the input is an array it will verify that all the objects inside the array are of the same class if not it will
+ * throw an InvalidArgumentException. If all the indexes are of the same class it will add them to the array.
+ *
+ * @param $input array|object If array it must only contain a single type of class.
* @throws InvalidArgumentException
*/
public function __construct($input = [])
@@ -23,12 +30,7 @@ class ClassValidationArray extends \ArrayObject implements \JsonSerializable
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."
- );
+ throw new \InvalidArgumentException("Non object found at index $key. Not allowed.");
}
if (!isset($this->class)) {
$this->class = get_class($value);
@@ -36,7 +38,12 @@ class ClassValidationArray extends \ArrayObject implements \JsonSerializable
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.");
+ throw new \InvalidArgumentException(
+ $this->class.
+ " expected in array. Got" .
+ get_class($value) .
+ " at index $key"
+ );
}
}
} elseif (is_object($input)) {
@@ -48,13 +55,14 @@ class ClassValidationArray extends \ArrayObject implements \JsonSerializable
}
/**
- * Sets the value at the specified index to value if
+ * Sets the value at the specified index to value if value's class matches the one set.
+ * If a class has not been set it will set the given object's class as the only allowed class.
* @throws InvalidArgumentException
*/
public function offsetSet($index, $value): void
{
if (!is_object($value)) {
- throw new \InvalidArgumentException("Excepcted object, ". gettype($value) ." given.");
+ throw new \InvalidArgumentException("Excepcted object, got ". gettype($value));
}
if (!isset($this->class)) {
$this->class = get_class($value);
@@ -62,9 +70,13 @@ class ClassValidationArray extends \ArrayObject implements \JsonSerializable
if (get_class($value) === $this->class) {
parent::offsetSet($index, $value);
} else {
- throw new \InvalidArgumentException("Excepcted $this->class, ". get_class($value) ." given.");
+ throw new \InvalidArgumentException("Excepcted $this->class, got ". get_class($value));
}
}
+ /**
+ * Returns the only class that is allowed to be added.
+ * @return string
+ */
public function getContainedClass(): string
{
return $this->class;
@@ -72,7 +84,7 @@ class ClassValidationArray extends \ArrayObject implements \JsonSerializable
/**
* {@inheritDoc}
*/
- public function JsonSerialize(): array
+ public function jsonSerialize(): array
{
$returnArray = [];
foreach ($this->getArrayCopy() as $value) {