Implemented scalar types for the app.

This commit is contained in:
Alex Tselegidis 2016-07-09 11:03:34 +02:00
parent 64cc62ea9c
commit 5362c30784
17 changed files with 296 additions and 18 deletions

View file

@ -11,8 +11,10 @@
* @since v1.2.0
* ---------------------------------------------------------------------------- */
namespace \EA\Engine\Types;
namespace EA\Engine\Types;
class Bool extends Type {
protected function _validate($value) {
return is_bool($value);
}
}

View 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 Email extends NonEmptyString {
protected function _validate($value) {
return parent::_validate($value) && filter_var($value, FILTER_VALIDATE_EMAIL);
}
}

View file

@ -11,8 +11,10 @@
* @since v1.2.0
* ---------------------------------------------------------------------------- */
namespace \EA\Engine\Types;
class Double extends Type {
namespace EA\Engine\Types;
class Float extends Type {
protected function _validate($value) {
return is_float($value);
}
}

View file

@ -14,9 +14,7 @@
namespace EA\Engine\Types;
class Int extends Type {
protected function _validate() {
if (!is_numeric($this->value) && (int)$this->value !== (float)$this->value) {
throw new \InvalidArgumentException('Invalid type value provided (expected int): ' . $this->value);
}
protected function _validate($value) {
return is_numeric($value) && !is_float($value);
}
}

View 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 !== '';
}
}

View file

@ -11,8 +11,10 @@
* @since v1.2.0
* ---------------------------------------------------------------------------- */
namespace \EA\Engine\Types;
namespace EA\Engine\Types;
class String extends Type {
protected function _validate($value) {
return is_string($value);
}
}

View file

@ -25,8 +25,11 @@ abstract class Type {
* @param mixed $value The type value to be validated.
*/
public function __construct($value) {
if (!$this->_validate($value)) {
throw new \InvalidArgumentException(__CLASS__ . ': Invalid argument value provided (' . $value . ')');
}
$this->value = $value;
$this->_validate($value);
}
/**
@ -40,8 +43,10 @@ abstract class Type {
/**
* Implement the validation logic in the children classes.
*
* @param mixed $value The value to be validated.
*
* @throws \InvalidArgumentException If the validation fails.
* @return bool Returns whether the value is valid or not.
*/
abstract protected function _validate();
abstract protected function _validate($value);
}

View file

@ -11,8 +11,10 @@
* @since v1.2.0
* ---------------------------------------------------------------------------- */
namespace \EA\Engine\Types;
namespace EA\Engine\Types;
class UnsignedInt extends Int {
protected function _validate($value) {
return parent::_validate($value) && $value > -1;
}
}

20
src/engine/Types/Url.php Normal file
View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View file

@ -13,13 +13,18 @@
namespace EA\Engine\Types;
class AuthorizationTest extends \PHPUnit_Framework_TestCase {
class IntTest extends \PHPUnit_Framework_TestCase {
public function testIntType() {
$type = new Int(1);
$this->assertEquals(1, $type->get());
}
public function testIntTypeThrowsExceptionWithInvalidValue() {
public function testIntTypeThrowsExceptionWithFloat() {
$this->setExpectedException('\InvalidArgumentException');
new Int(100.00);
}
public function testIntTypeThrowsExceptionWithWithString() {
$this->setExpectedException('\InvalidArgumentException');
new Int('invalid');
}

View 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);
}
}

View 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);
}
}

View 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');
}
}

View 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);
}
}