-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Romeo Vegvari
committed
Mar 2, 2015
0 parents
commit 50290f8
Showing
21 changed files
with
861 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "data/field", | ||
"description": "Fields using Data/Type", | ||
"license": "GPL-2.0", | ||
"authors": [ | ||
{ | ||
"name": "Romeo Vegvari", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"require": { | ||
"php": ">=5.3.0", | ||
"ext-mbstring": "*", | ||
"data/type": "dev-master" | ||
}, | ||
"minimum-stability": "dev", | ||
"autoload": { | ||
"psr-4": { | ||
"Data\\Field\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd" | ||
backupGlobals="false" | ||
colors="true" | ||
bootstrap="vendor/autoload.php" | ||
> | ||
<php> | ||
<ini name="error_reporting" value="-16385"/> | ||
</php> | ||
|
||
<testsuites> | ||
<testsuite name="data\field tests"> | ||
<directory>./test/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./</directory> | ||
<exclude> | ||
<directory>./test</directory> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Data\Field; | ||
|
||
class Char extends VarChar | ||
{ | ||
protected static $maxLength = 255; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Data\Field; | ||
|
||
class DateTime extends Field | ||
{ | ||
protected $data = '\Data\Type\DateTime'; | ||
|
||
public static function nullable($default = null) | ||
{ | ||
$class = get_called_class(); | ||
$instance = new $class($default, true); | ||
return $instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Data\Field; | ||
|
||
class Decimal extends Number | ||
{ | ||
protected $precision = 10; | ||
protected $scale = 2; | ||
|
||
public function __construct($precision = 10, $scale = 2, $default = null, $nullable = false, $unsigned = false) | ||
{ | ||
parent::__construct($default, $nullable, $unsigned); | ||
|
||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Data\Field; | ||
|
||
abstract class Field | ||
{ | ||
protected $data; | ||
protected $nullable = false; | ||
|
||
public function __construct($default = null, $nullable = false) | ||
{ | ||
$class = $this->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() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Data\Field; | ||
|
||
class Float extends Number | ||
{ | ||
public static function nullable($default = null) | ||
{ | ||
$class = get_called_class(); | ||
$instance = new $class($default, true); | ||
return $instance; | ||
} | ||
|
||
public static function unsigned($default = null) | ||
{ | ||
$class = get_called_class(); | ||
$instance = new $class($default, true, true); | ||
return $instance; | ||
} | ||
|
||
public static function unsignedNullable($default = null) | ||
{ | ||
$class = get_called_class(); | ||
$instance = new $class($default, false, true); | ||
return $instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Data\Field; | ||
|
||
class Int extends Float | ||
{ | ||
protected $data = '\Data\Type\Int'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Data\Field; | ||
|
||
abstract class Number extends Field | ||
{ | ||
protected $data = '\Data\Type\Float'; | ||
protected $unsigned = false; | ||
protected $primary = false; | ||
|
||
public function __construct($default = null, $nullable = false, $unsigned = false) | ||
{ | ||
parent::__construct($default, $nullable); | ||
|
||
$this->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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Data\Field; | ||
|
||
class Serial extends Int | ||
{ | ||
protected $nullable = false; | ||
protected $unsigned = true; | ||
protected $primary = true; | ||
protected $serial = true; | ||
|
||
public function __construct($default = null, $nullable = false, $unsigned = true) | ||
{ | ||
parent::__construct($default, $nullable, $unsigned); | ||
|
||
if ($this->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'); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Data\Field; | ||
|
||
abstract class String extends Field | ||
{ | ||
protected static $maxLength = 4294967295; | ||
|
||
protected $data = '\Data\Type\String'; | ||
|
||
public function set($value) | ||
{ | ||
$data = $this->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 . ')'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Data\Field; | ||
|
||
class Text extends String | ||
{ | ||
protected static $maxLength = 65535; | ||
|
||
public static function nullable($default = null) | ||
{ | ||
$class = get_called_class(); | ||
$instance = new $class($default, true); | ||
return $instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Data\Field; | ||
|
||
class VarChar extends String | ||
{ | ||
protected static $maxLength = 65535; | ||
|
||
protected $length = 255; | ||
protected $primary = false; | ||
|
||
public function __construct($length = 255, $default = null, $nullable = false) | ||
{ | ||
$this->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 . ')'); | ||
} | ||
} | ||
} |
Oops, something went wrong.