Implemented scalar types for the app.
This commit is contained in:
parent
64cc62ea9c
commit
5362c30784
17 changed files with 296 additions and 18 deletions
|
@ -11,8 +11,10 @@
|
||||||
* @since v1.2.0
|
* @since v1.2.0
|
||||||
* ---------------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
namespace \EA\Engine\Types;
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
class Bool extends Type {
|
class Bool extends Type {
|
||||||
|
protected function _validate($value) {
|
||||||
|
return is_bool($value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
* Easy!Appointments - Open Source Web Scheduler
|
||||||
|
*
|
||||||
|
* @package EasyAppointments
|
||||||
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||||
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||||
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||||
|
* @link http://easyappointments.org
|
||||||
|
* @since v1.2.0
|
||||||
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
|
class Email extends NonEmptyString {
|
||||||
|
protected function _validate($value) {
|
||||||
|
return parent::_validate($value) && filter_var($value, FILTER_VALIDATE_EMAIL);
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,8 +11,10 @@
|
||||||
* @since v1.2.0
|
* @since v1.2.0
|
||||||
* ---------------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
namespace \EA\Engine\Types;
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
class Double extends Type {
|
|
||||||
|
|
||||||
|
class Float extends Type {
|
||||||
|
protected function _validate($value) {
|
||||||
|
return is_float($value);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -14,9 +14,7 @@
|
||||||
namespace EA\Engine\Types;
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
class Int extends Type {
|
class Int extends Type {
|
||||||
protected function _validate() {
|
protected function _validate($value) {
|
||||||
if (!is_numeric($this->value) && (int)$this->value !== (float)$this->value) {
|
return is_numeric($value) && !is_float($value);
|
||||||
throw new \InvalidArgumentException('Invalid type value provided (expected int): ' . $this->value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
20
src/engine/Types/NonEmptyString.php
Normal file
20
src/engine/Types/NonEmptyString.php
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
* Easy!Appointments - Open Source Web Scheduler
|
||||||
|
*
|
||||||
|
* @package EasyAppointments
|
||||||
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||||
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||||
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||||
|
* @link http://easyappointments.org
|
||||||
|
* @since v1.2.0
|
||||||
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
|
class NonEmptyString extends String {
|
||||||
|
protected function _validate($value) {
|
||||||
|
return parent::_validate($value) && $value !== '';
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,8 +11,10 @@
|
||||||
* @since v1.2.0
|
* @since v1.2.0
|
||||||
* ---------------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
namespace \EA\Engine\Types;
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
class String extends Type {
|
class String extends Type {
|
||||||
|
protected function _validate($value) {
|
||||||
|
return is_string($value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,8 +25,11 @@ abstract class Type {
|
||||||
* @param mixed $value The type value to be validated.
|
* @param mixed $value The type value to be validated.
|
||||||
*/
|
*/
|
||||||
public function __construct($value) {
|
public function __construct($value) {
|
||||||
|
if (!$this->_validate($value)) {
|
||||||
|
throw new \InvalidArgumentException(__CLASS__ . ': Invalid argument value provided (' . $value . ')');
|
||||||
|
}
|
||||||
|
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
$this->_validate($value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,7 +44,9 @@ abstract class Type {
|
||||||
/**
|
/**
|
||||||
* Implement the validation logic in the children classes.
|
* Implement the validation logic in the children classes.
|
||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException If the validation fails.
|
* @param mixed $value The value to be validated.
|
||||||
|
*
|
||||||
|
* @return bool Returns whether the value is valid or not.
|
||||||
*/
|
*/
|
||||||
abstract protected function _validate();
|
abstract protected function _validate($value);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,10 @@
|
||||||
* @since v1.2.0
|
* @since v1.2.0
|
||||||
* ---------------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
namespace \EA\Engine\Types;
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
class UnsignedInt extends Int {
|
class UnsignedInt extends Int {
|
||||||
|
protected function _validate($value) {
|
||||||
|
return parent::_validate($value) && $value > -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
20
src/engine/Types/Url.php
Normal file
20
src/engine/Types/Url.php
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
* Easy!Appointments - Open Source Web Scheduler
|
||||||
|
*
|
||||||
|
* @package EasyAppointments
|
||||||
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||||
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||||
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||||
|
* @link http://easyappointments.org
|
||||||
|
* @since v1.2.0
|
||||||
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
|
class Url extends NonEmptyString {
|
||||||
|
protected function _validate($value) {
|
||||||
|
return parent::_validate($value) && filter_var($value, FILTER_VALIDATE_URL);
|
||||||
|
}
|
||||||
|
}
|
26
test/php/engine/Types/BoolTest.php
Normal file
26
test/php/engine/Types/BoolTest.php
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
* Easy!Appointments - Open Source Web Scheduler
|
||||||
|
*
|
||||||
|
* @package EasyAppointments
|
||||||
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||||
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||||
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||||
|
* @link http://easyappointments.org
|
||||||
|
* @since v1.2.0
|
||||||
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
|
class BoolTest extends \PHPUnit_Framework_TestCase {
|
||||||
|
public function testBoolType() {
|
||||||
|
$type = new Bool(true);
|
||||||
|
$this->assertEquals(true, $type->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBoolTypeThrowsExceptionWithInvalidValue() {
|
||||||
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
|
new Bool(null);
|
||||||
|
}
|
||||||
|
}
|
31
test/php/engine/Types/EmailTest.php
Normal file
31
test/php/engine/Types/EmailTest.php
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
* Easy!Appointments - Open Source Web Scheduler
|
||||||
|
*
|
||||||
|
* @package EasyAppointments
|
||||||
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||||
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||||
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||||
|
* @link http://easyappointments.org
|
||||||
|
* @since v1.2.0
|
||||||
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
|
class EmailTest extends \PHPUnit_Framework_TestCase {
|
||||||
|
public function testEmailType() {
|
||||||
|
$type = new Email('john@doe.com');
|
||||||
|
$this->assertEquals('john@doe.com', $type->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEmailTypeThrowsExceptionWithInvalidEmail() {
|
||||||
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
|
new Email('abcdef');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEmailTypeThrowsExceptionWithInvalidValue() {
|
||||||
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
|
new Email(null);
|
||||||
|
}
|
||||||
|
}
|
26
test/php/engine/Types/FloatTest.php
Normal file
26
test/php/engine/Types/FloatTest.php
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
* Easy!Appointments - Open Source Web Scheduler
|
||||||
|
*
|
||||||
|
* @package EasyAppointments
|
||||||
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||||
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||||
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||||
|
* @link http://easyappointments.org
|
||||||
|
* @since v1.2.0
|
||||||
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
|
class FloatTest extends \PHPUnit_Framework_TestCase {
|
||||||
|
public function testFloatType() {
|
||||||
|
$type = new Float(100.00);
|
||||||
|
$this->assertEquals(100.00, $type->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFloatTypeThrowsExceptionWithInvalidValue() {
|
||||||
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
|
new Float(null);
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,13 +13,18 @@
|
||||||
|
|
||||||
namespace EA\Engine\Types;
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
class AuthorizationTest extends \PHPUnit_Framework_TestCase {
|
class IntTest extends \PHPUnit_Framework_TestCase {
|
||||||
public function testIntType() {
|
public function testIntType() {
|
||||||
$type = new Int(1);
|
$type = new Int(1);
|
||||||
$this->assertEquals(1, $type->get());
|
$this->assertEquals(1, $type->get());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIntTypeThrowsExceptionWithInvalidValue() {
|
public function testIntTypeThrowsExceptionWithFloat() {
|
||||||
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
|
new Int(100.00);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIntTypeThrowsExceptionWithWithString() {
|
||||||
$this->setExpectedException('\InvalidArgumentException');
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
new Int('invalid');
|
new Int('invalid');
|
||||||
}
|
}
|
||||||
|
|
31
test/php/engine/Types/NonEmptyStringTest.php
Normal file
31
test/php/engine/Types/NonEmptyStringTest.php
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
* Easy!Appointments - Open Source Web Scheduler
|
||||||
|
*
|
||||||
|
* @package EasyAppointments
|
||||||
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||||
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||||
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||||
|
* @link http://easyappointments.org
|
||||||
|
* @since v1.2.0
|
||||||
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
|
class NonEmptyStringTest extends \PHPUnit_Framework_TestCase {
|
||||||
|
public function testNonEmptyStringType() {
|
||||||
|
$type = new NonEmptyString('Hello!');
|
||||||
|
$this->assertEquals('Hello!', $type->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNonEmptyStringTypeThrowsExceptionWithEmptyString() {
|
||||||
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
|
new NonEmptyString('');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNonEmptyStringTypeThrowsExceptionWithInvalidValue() {
|
||||||
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
|
new NonEmptyString(null);
|
||||||
|
}
|
||||||
|
}
|
26
test/php/engine/Types/StringTest.php
Normal file
26
test/php/engine/Types/StringTest.php
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
* Easy!Appointments - Open Source Web Scheduler
|
||||||
|
*
|
||||||
|
* @package EasyAppointments
|
||||||
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||||
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||||
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||||
|
* @link http://easyappointments.org
|
||||||
|
* @since v1.2.0
|
||||||
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
|
class StringTest extends \PHPUnit_Framework_TestCase {
|
||||||
|
public function testStringType() {
|
||||||
|
$type = new String('Hello!');
|
||||||
|
$this->assertEquals('Hello!', $type->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testStringTypeThrowsExceptionWithInvalidValue() {
|
||||||
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
|
new String(null);
|
||||||
|
}
|
||||||
|
}
|
31
test/php/engine/Types/UnsignedIntTest.php
Normal file
31
test/php/engine/Types/UnsignedIntTest.php
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
* Easy!Appointments - Open Source Web Scheduler
|
||||||
|
*
|
||||||
|
* @package EasyAppointments
|
||||||
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||||
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||||
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||||
|
* @link http://easyappointments.org
|
||||||
|
* @since v1.2.0
|
||||||
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
|
class UnsignedIntTest extends \PHPUnit_Framework_TestCase {
|
||||||
|
public function testUnsignedIntType() {
|
||||||
|
$type = new UnsignedInt(1);
|
||||||
|
$this->assertEquals(1, $type->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnsignedIntTypeThrowsExceptionWithNegative() {
|
||||||
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
|
new UnsignedInt(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnsignedIntTypeThrowsExceptionWithWithString() {
|
||||||
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
|
new UnsignedInt('invalid');
|
||||||
|
}
|
||||||
|
}
|
31
test/php/engine/Types/UrlTest.php
Normal file
31
test/php/engine/Types/UrlTest.php
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
* Easy!Appointments - Open Source Web Scheduler
|
||||||
|
*
|
||||||
|
* @package EasyAppointments
|
||||||
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||||
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||||
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||||
|
* @link http://easyappointments.org
|
||||||
|
* @since v1.2.0
|
||||||
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
namespace EA\Engine\Types;
|
||||||
|
|
||||||
|
class UrlTest extends \PHPUnit_Framework_TestCase {
|
||||||
|
public function testUrlType() {
|
||||||
|
$type = new Url('http://localhost');
|
||||||
|
$this->assertEquals('http://localhost', $type->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUrlTypeThrowsExceptionWithInvalidUrl() {
|
||||||
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
|
new Url('abcdef');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUrlTypeThrowsExceptionWithInvalidValue() {
|
||||||
|
$this->setExpectedException('\InvalidArgumentException');
|
||||||
|
new Url(null);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue