Skip to content

Commit

Permalink
#32 added better validation to the base types (array, int, string)
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasFranek committed Jul 7, 2018
1 parent 191baed commit 9258ddf
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/Types/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/Types/IntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/Types/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
}
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/Models/ExamTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ public function testCreate() : void
];

$this->assertEquals($expected, $examTypes->serialize());
$this->assertEquals(json_encode($expected), $examTypes->serialize('json'));
}
}
9 changes: 9 additions & 0 deletions tests/Types/ArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/Types/IntTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/Types/StringTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9258ddf

Please sign in to comment.