diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f6ad9dd --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "data/field", + "description": "Fields using Data/Type", + "license": "GPL-2.0", + "authors": [ + { + "name": "Romeo Vegvari", + "email": "romeo@vegvari.hu" + } + ], + "minimum-stability": "dev", + "require": { + "php": ">=5.3.0", + "ext-mbstring": "*", + "data/type": "dev-master" + }, + "minimum-stability": "dev", + "autoload": { + "psr-4": { + "Data\\Field\\": "src/" + } + } +} \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..868b7d6 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,28 @@ + + + + + + + + + + ./test/ + + + + + + ./ + + ./test + ./vendor + + + + diff --git a/src/Char.php b/src/Char.php new file mode 100644 index 0000000..2bc3dab --- /dev/null +++ b/src/Char.php @@ -0,0 +1,8 @@ +precision = \Data\Type\Int::cast($precision); + $this->scale = \Data\Type\Int::cast($scale); + + if ($this->precision < 1 || $this->precision > 65) { + throw new \InvalidArgumentException('Precision must be between 1-65, "' . $this->precision . '" given'); + } + + if ($this->scale < 0 || $this->scale > 30) { + throw new \InvalidArgumentException('Scale must be between 0-30, "' . $this->scale . '" given'); + } + + if ($this->scale > $this->precision) { + throw new \InvalidArgumentException('Scale can\'t be larger than precision (precision: "' . $this->precision . '", scale: "' . $this->scale . '")'); + } + } + + public static function nullable($precision, $scale, $default = null) + { + $class = get_called_class(); + $instance = new $class($precision, $scale, $default, true); + return $instance; + } + + public static function unsigned($precision, $scale, $default = null) + { + $class = get_called_class(); + $instance = new $class($precision, $scale, $default, true, true); + return $instance; + } + + public static function unsignedNullable($precision, $scale, $default = null) + { + $class = get_called_class(); + $instance = new $class($precision, $scale, $default, false, true); + return $instance; + } + + protected function check() + { + parent::check(); + + if ($this->data->value !== null && $this->unsigned === false) { + $min = (float) ('-1.0e' . ($this->precision - $this->scale)); + + if ($this->data->value <= $min) { + throw new \InvalidArgumentException('Min value is ' . $min . ', "' . $this->data->value . '" given'); + } + } + + $max = (float) ('1.0e' . ($this->precision - $this->scale)); + if ($this->data->value >= $max) { + throw new \InvalidArgumentException('Max value is ' . $max . ', "' . $this->data->value . '" given'); + } + + if ($this->data->value !== null) { + $this->data = $this->data->set(round($this->data->value, $this->scale)); + } + + // var_dump($min, $max); + // exit; + } +} diff --git a/src/Field.php b/src/Field.php new file mode 100644 index 0000000..4321ca7 --- /dev/null +++ b/src/Field.php @@ -0,0 +1,34 @@ +data; + $this->data = new $class($default); + + $this->nullable = \Data\Type\Bool::cast($nullable); + + $this->check(); + } + + public function __get($name) + { + return $this->$name; + } + + public function set($value) + { + $this->data = $this->data->set($value); + $this->check(); + } + + protected function check() + { + } +} diff --git a/src/Float.php b/src/Float.php new file mode 100644 index 0000000..836ae15 --- /dev/null +++ b/src/Float.php @@ -0,0 +1,27 @@ +unsigned = \Data\Type\Bool::cast($unsigned); + } + + public function primary() + { + $this->primary = true; + } + + protected function check() + { + if ($this->data->value === null) { + return; + } + + if ($this->unsigned === true && $this->data->value < 0) { + throw new \InvalidArgumentException('Unsigned field can\'t be negative, "' . $this->data->value . '" given'); + } + } +} diff --git a/src/Serial.php b/src/Serial.php new file mode 100644 index 0000000..ebf5764 --- /dev/null +++ b/src/Serial.php @@ -0,0 +1,35 @@ +nullable === true) { + throw new \InvalidArgumentException('Serial can\'t be nullable'); + } + + if ($this->unsigned === false) { + throw new \InvalidArgumentException('Serial must be unsigned'); + } + } + + protected function check() + { + parent::check(); + + if ($this->data->value !== null) { + if ($this->data->value < 1) { + throw new \InvalidArgumentException('Serial must be larger than 0, "' . $this->data->value . '" given'); + } + } + } +} diff --git a/src/String.php b/src/String.php new file mode 100644 index 0000000..754776e --- /dev/null +++ b/src/String.php @@ -0,0 +1,28 @@ +data->set($value); + + if ($this->nullable === true && $data->value === '') { + $value = null; + } + + parent::set($value); + } + + protected function check() + { + if (static::$maxLength < $this->data->length) { + throw new \LengthException('Length of the string (' . $this->data->length . ') is larger than maxLength (' . static::$maxLength . ')'); + } + } +} diff --git a/src/Text.php b/src/Text.php new file mode 100644 index 0000000..2b82033 --- /dev/null +++ b/src/Text.php @@ -0,0 +1,15 @@ +length = \Data\Type\Int::castPositive($length); + + if ($this->length < 0) { + throw new \InvalidArgumentException('Length must be 0 or positive integer, ' . $this->length . ' given'); + } + + if ($this->length > static::$maxLength) { + throw new \InvalidArgumentException('Length (' . $this->length . ') is larger than maxLength (' . static::$maxLength . ')'); + } + + parent::__construct($default, $nullable); + } + + public static function nullable($length, $default = null) + { + $class = get_called_class(); + $instance = new $class($length, $default, true); + return $instance; + } + + public function primary() + { + $this->primary = true; + } + + protected function check() + { + parent::check(); + + if ($this->length < $this->data->length) { + throw new \LengthException('Length of the string (' . $this->data->length . ') is larger than lenght of the field (' . $this->length . ')'); + } + } +} diff --git a/test/CharTest.php b/test/CharTest.php new file mode 100644 index 0000000..655b00e --- /dev/null +++ b/test/CharTest.php @@ -0,0 +1,71 @@ +assertTrue($field->data instanceof \Data\Type\String); + $this->assertSame(null, $field->data->value); + $this->assertSame(false, $field->nullable); + $this->assertSame(false, $field->primary); + $this->assertSame(255, $field->length); + } + + public function testDefault() + { + $field = new Char(255, 1); + $this->assertSame('1', $field->data->value); + } + + public function testNullable() + { + $field = new Char(255, null, true); + $this->assertSame(true, $field->nullable); + } + + public function testNullableInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Char(255, null, 'a'); + } + + public function testPrimary() + { + $field = new Char(); + $field->primary(); + $this->assertSame(true, $field->primary); + } + + public function testLength() + { + $field = new Char(10); + $this->assertSame(10, $field->length); + } + + public function testLengthInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Char(-1); + } + + public function testLengthLargerThanMax() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Char(256); + } + + public function testLengthValueTooLong() + { + $this->setExpectedException('\LengthException'); + $field = new VarChar(10, str_repeat(' ', 11)); + } + + public function testMaxLengthValueTooLong() + { + $this->setExpectedException('\LengthException'); + $field = new Char(255, str_repeat(' ', 256)); + } +} diff --git a/test/DateTimeTest.php b/test/DateTimeTest.php new file mode 100644 index 0000000..243ab31 --- /dev/null +++ b/test/DateTimeTest.php @@ -0,0 +1,38 @@ +assertTrue($field->data instanceof \Data\Type\DateTime); + $this->assertSame(false, $field->nullable); + } + + public function testDefault() + { + $field = new DateTime('now'); + $this->assertSame(date('c'), $field->data->value); + } + + public function testSet() + { + $field = new DateTime(); + $field->set('+1day'); + $this->assertSame(date('c', time() + 24 * 60 * 60), $field->data->value); + } + + public function testNullable() + { + $field = new DateTime(null, true); + $this->assertSame(true, $field->nullable); + } + + public function testNullableInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new DateTime(null, 'a'); + } +} diff --git a/test/DecimalTest.php b/test/DecimalTest.php new file mode 100644 index 0000000..8cfe80c --- /dev/null +++ b/test/DecimalTest.php @@ -0,0 +1,122 @@ +assertTrue($field->data instanceof \Data\Type\Float); + $this->assertSame(false, $field->nullable); + $this->assertSame(false, $field->unsigned); + $this->assertSame(false, $field->primary); + $this->assertSame(10, $field->precision); + $this->assertSame(2, $field->scale); + } + + public function testDefault() + { + $field = new Decimal(10, 2, 1); + $this->assertSame(1.0, $field->data->value); + } + + public function testNullable() + { + $field = new Decimal(10, 2, null, true); + $this->assertSame(true, $field->nullable); + } + + public function testNullableInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Decimal(10, 2, null, 'a'); + } + + public function testUnsigned() + { + $field = new Decimal(10, 2, null, false, true); + $this->assertSame(true, $field->unsigned); + } + + public function testUnsignedInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Decimal(10, 2, null, false, 'a'); + } + + public function testPrimary() + { + $field = new Decimal(); + $field->primary(); + $this->assertSame(true, $field->primary); + } + + public function testPrecision() + { + $field = new Decimal(11); + $this->assertSame(11, $field->precision); + } + + public function testPrecisionTooSmall() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Decimal(0); + } + + public function testPrecisionTooLarge() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Decimal(66); + } + + public function testScale() + { + $field = new Decimal(11, 11); + $this->assertSame(11, $field->scale); + } + + public function testScaleTooSmall() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Decimal(10, -1); + } + + public function testScaleTooLarge() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Decimal(10, 31); + } + + public function testScaleLargerThanPrecision() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Decimal(10, 11); + } + + public function testMax() + { + $field = new Decimal(10, 0); + $field->set(9999999999); + } + + public function testMaxInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Decimal(10, 0); + $field->set(10000000000); + } + + public function testMin() + { + $field = new Decimal(10, 0); + $field->set(-9999999999); + } + + public function testMinInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Decimal(10, 0); + $field->set(-10000000000); + } +} diff --git a/test/FloatTest.php b/test/FloatTest.php new file mode 100644 index 0000000..a9b8aff --- /dev/null +++ b/test/FloatTest.php @@ -0,0 +1,52 @@ +assertTrue($field->data instanceof \Data\Type\Float); + $this->assertSame(false, $field->nullable); + $this->assertSame(false, $field->unsigned); + $this->assertSame(false, $field->primary); + } + + public function testDefault() + { + $field = new Float(1); + $this->assertSame(1.0, $field->data->value); + } + + public function testNullable() + { + $field = new Float(null, true); + $this->assertSame(true, $field->nullable); + } + + public function testNullableInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Float(null, 'a'); + } + + public function testUnsigned() + { + $field = new Float(null, false, true); + $this->assertSame(true, $field->unsigned); + } + + public function testUnsignedInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Float(null, false, 'a'); + } + + public function testPrimary() + { + $field = new Float(); + $field->primary(); + $this->assertSame(true, $field->primary); + } +} diff --git a/test/IntTest.php b/test/IntTest.php new file mode 100644 index 0000000..d5080de --- /dev/null +++ b/test/IntTest.php @@ -0,0 +1,52 @@ +assertTrue($field->data instanceof \Data\Type\Int); + $this->assertSame(false, $field->nullable); + $this->assertSame(false, $field->unsigned); + $this->assertSame(false, $field->primary); + } + + public function testDefault() + { + $field = new Int(1); + $this->assertSame(1, $field->data->value); + } + + public function testNullable() + { + $field = new Int(null, true); + $this->assertSame(true, $field->nullable); + } + + public function testNullableInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Int(null, 'a'); + } + + public function testUnsigned() + { + $field = new Int(null, false, true); + $this->assertSame(true, $field->unsigned); + } + + public function testUnsignedInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Int(null, false, 'a'); + } + + public function testPrimary() + { + $field = new Int(); + $field->primary(); + $this->assertSame(true, $field->primary); + } +} diff --git a/test/SerialTest.php b/test/SerialTest.php new file mode 100644 index 0000000..10f1ba9 --- /dev/null +++ b/test/SerialTest.php @@ -0,0 +1,41 @@ +assertTrue($field->data instanceof \Data\Type\Int); + $this->assertSame(false, $field->nullable); + $this->assertSame(true, $field->unsigned); + $this->assertSame(true, $field->primary); + $this->assertSame(true, $field->serial); + } + + public function testDefault() + { + $field = new Serial(1); + $this->assertSame(1, $field->data->value); + } + + public function testNullableInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Serial(null, true); + } + + public function testUnsignedInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Serial(null, true, true); + } + + public function testPrimary() + { + $field = new Serial(); + $field->primary(); + $this->assertSame(true, $field->primary); + } +} diff --git a/test/TextTest.php b/test/TextTest.php new file mode 100644 index 0000000..ad4c8b2 --- /dev/null +++ b/test/TextTest.php @@ -0,0 +1,38 @@ +assertTrue($field->data instanceof \Data\Type\String); + $this->assertSame(null, $field->data->value); + $this->assertSame(false, $field->nullable); + } + + public function testDefault() + { + $field = new Text(1); + $this->assertSame('1', $field->data->value); + } + + public function testNullable() + { + $field = new Text(null, true); + $this->assertSame(true, $field->nullable); + } + + public function testNullableInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new Text(null, 'a'); + } + + public function testMaxLengthValueTooLong() + { + $this->setExpectedException('\LengthException'); + $field = new Text(str_repeat(' ', 65536)); + } +} diff --git a/test/VarCharTest.php b/test/VarCharTest.php new file mode 100644 index 0000000..37fb763 --- /dev/null +++ b/test/VarCharTest.php @@ -0,0 +1,71 @@ +assertTrue($field->data instanceof \Data\Type\String); + $this->assertSame(null, $field->data->value); + $this->assertSame(false, $field->nullable); + $this->assertSame(false, $field->primary); + $this->assertSame(255, $field->length); + } + + public function testDefault() + { + $field = new VarChar(255, 1); + $this->assertSame('1', $field->data->value); + } + + public function testNullable() + { + $field = new VarChar(255, null, true); + $this->assertSame(true, $field->nullable); + } + + public function testNullableInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new VarChar(255, null, 'a'); + } + + public function testPrimary() + { + $field = new VarChar(); + $field->primary(); + $this->assertSame(true, $field->primary); + } + + public function testLength() + { + $field = new VarChar(10); + $this->assertSame(10, $field->length); + } + + public function testLengthInvalid() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new VarChar(-1); + } + + public function testLengthLargerThanMax() + { + $this->setExpectedException('\InvalidArgumentException'); + $field = new VarChar(65536); + } + + public function testLengthValueTooLong() + { + $this->setExpectedException('\LengthException'); + $field = new VarChar(10, str_repeat(' ', 11)); + } + + public function testMaxLengthValueTooLong() + { + $this->setExpectedException('\LengthException'); + $field = new VarChar(255, str_repeat(' ', 65536)); + } +}