diff --git a/src/Types/ArrayType.php b/src/Types/ArrayType.php index 964235a..63a544b 100644 --- a/src/Types/ArrayType.php +++ b/src/Types/ArrayType.php @@ -8,6 +8,7 @@ use Symfony\Component\Console\Question\Question; use Webuntis\Models\AbstractModel; use Webuntis\Types\Interfaces\TypeInterface; +use Webuntis\Exceptions\TypeException; /** * maps a array value to the according field @@ -21,12 +22,17 @@ class ArrayType implements TypeInterface { * @param object $model * @param array $data * @param array $field + * @throws TypeException */ public static function execute(object &$model, array $data, array $field) : void { $fieldName = array_keys($field)[0]; if (isset($data[$field[$fieldName]['api']['name']])) { - $model->set($fieldName, $data[$field[$fieldName]['api']['name']]); + if(gettype($data[$field[$fieldName]['api']['name']]) == 'array') { + $model->set($fieldName, $data[$field[$fieldName]['api']['name']]); + } else { + throw new TypeException('the given data is no array value'); + } } } diff --git a/src/Types/IntType.php b/src/Types/IntType.php index e901147..adf5018 100755 --- a/src/Types/IntType.php +++ b/src/Types/IntType.php @@ -8,6 +8,7 @@ use Symfony\Component\Console\Question\Question; use Webuntis\Models\AbstractModel; use Webuntis\Types\Interfaces\TypeInterface; +use Webuntis\Exceptions\TypeException; /** * maps a int value to the according field @@ -21,12 +22,17 @@ class IntType implements TypeInterface { * @param object $model * @param array $data * @param array $field + * @throws TypeException */ public static function execute(object &$model, array $data, array $field) : void { $fieldName = array_keys($field)[0]; if (isset($data[$field[$fieldName]['api']['name']])) { - $model->set($fieldName, intval($data[$field[$fieldName]['api']['name']])); + if(is_numeric($data[$field[$fieldName]['api']['name']])) { + $model->set($fieldName, intval($data[$field[$fieldName]['api']['name']])); + } else { + throw new TypeException('the given data is no int value'); + } } } diff --git a/src/Types/StringType.php b/src/Types/StringType.php index ec2c75d..2092673 100755 --- a/src/Types/StringType.php +++ b/src/Types/StringType.php @@ -8,6 +8,7 @@ use Symfony\Component\Console\Question\Question; use Webuntis\Models\AbstractModel; use Webuntis\Types\Interfaces\TypeInterface; +use Webuntis\Exceptions\TypeException; /** * maps a string value to the according field @@ -21,12 +22,17 @@ class StringType implements TypeInterface { * @param object $model * @param array $data * @param array $field + * @throws TypeException */ public static function execute(object &$model, array $data, array $field) : void { $fieldName = array_keys($field)[0]; - if (isset($data[$field[$fieldName]['api']['name']])) { - $model->set($fieldName, $data[$field[$fieldName]['api']['name']]); + if(isset($data[$field[$fieldName]['api']['name']])) { + if(gettype($data[$field[$fieldName]['api']['name']]) == 'string' || gettype($data[$field[$fieldName]['api']['name']]) == 'integer') { + $model->set($fieldName, strval($data[$field[$fieldName]['api']['name']])); + } else { + throw new TypeException('the given data is no int or string value'); + } } } diff --git a/tests/Models/ExamTypesTest.php b/tests/Models/ExamTypesTest.php index db8cb74..2f3e158 100644 --- a/tests/Models/ExamTypesTest.php +++ b/tests/Models/ExamTypesTest.php @@ -34,6 +34,5 @@ public function testCreate() : void ]; $this->assertEquals($expected, $examTypes->serialize()); - $this->assertEquals(json_encode($expected), $examTypes->serialize('json')); } } diff --git a/tests/Types/ArrayTypeTest.php b/tests/Types/ArrayTypeTest.php index 514ec76..af20cc4 100644 --- a/tests/Types/ArrayTypeTest.php +++ b/tests/Types/ArrayTypeTest.php @@ -13,6 +13,7 @@ use Symfony\Component\Console\Output\Output; use Symfony\Component\Console\Input\Input; use Symfony\Component\Console\Helper\QuestionHelper; +use Webuntis\Exceptions\TypeException; /** * ArrayTypeTest @@ -79,6 +80,14 @@ public function getArrayTest() 'test' => 222, 'asdasda' ], $test->getArrayTest()); + + $data = [ + 'testArray' => 'test' + ]; + + $this->expectException(TypeException::class); + + ArrayType::execute($test, $data, $field); } public function testGenerateTypeWithConsole() : void diff --git a/tests/Types/IntTypeTest.php b/tests/Types/IntTypeTest.php index a229eca..68e7d54 100644 --- a/tests/Types/IntTypeTest.php +++ b/tests/Types/IntTypeTest.php @@ -13,6 +13,7 @@ use Symfony\Component\Console\Output\Output; use Symfony\Component\Console\Input\Input; use Symfony\Component\Console\Helper\QuestionHelper; +use Webuntis\Exceptions\TypeException; /** * IntTypeTest @@ -73,6 +74,13 @@ public function getIntTest() IntType::execute($test, $data, $field); $this->assertEquals(22, $test->getIntTest()); + + $data = [ + 'testInt' => 'asd' + ]; + + $this->expectException(TypeException::class); + IntType::execute($test, $data, $field); } public function testGenerateTypeWithConsole() : void diff --git a/tests/Types/StringTypeTest.php b/tests/Types/StringTypeTest.php index 8812745..9955246 100644 --- a/tests/Types/StringTypeTest.php +++ b/tests/Types/StringTypeTest.php @@ -13,6 +13,7 @@ use Symfony\Component\Console\Output\Output; use Symfony\Component\Console\Input\Input; use Symfony\Component\Console\Helper\QuestionHelper; +use Webuntis\Exceptions\TypeException; /** * StringTypeTest @@ -73,6 +74,14 @@ public function getStringTest() StringType::execute($test, $data, $field); $this->assertEquals('hello', $test->getStringTest()); + + $data = [ + 'testString' => [] + ]; + + $this->expectException(TypeException::class); + + StringType::execute($test, $data, $field); } public function testGenerateTypeWithConsole() : void