From fa0d6868532aef241931478cb889060a9ce2f664 Mon Sep 17 00:00:00 2001 From: Enric Florit Date: Wed, 15 Jul 2015 00:14:14 +0200 Subject: [PATCH] Version 0.2. Added formatting and static shortcuts. Fixed some bugs and refactored ArrayS code. --- README.md | 34 +++++++++++++---- composer.json | 2 +- src/Structure/ArrayS.php | 75 +++++++++++++++++++++++++------------ src/Structure/BooleanS.php | 22 ++++++++--- src/Structure/FloatS.php | 9 +++++ src/Structure/IntegerS.php | 9 +++++ src/Structure/NumericS.php | 12 +++++- src/Structure/ScalarS.php | 7 +++- src/Structure/StringS.php | 15 ++++++-- src/Structure/Structure.php | 54 +++++++++++++++++++++++++- tests/ArrayFormatTest.php | 31 +++++++++++++-- tests/ArrayTest.php | 30 +++++++++++++-- tests/NumericTest.php | 25 ++++++++----- tests/ScalarTest.php | 9 ++++- tests/StaticsTest.php | 11 ++++-- 15 files changed, 276 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 9ebb918..07f0795 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ You can install Structure into your project using [Composer](http://getcomposer. ```json "require":{ - "3nr1c/structure": "v0.1" + "3nr1c/structure": "v0.2" } ``` @@ -81,19 +81,37 @@ public function __construct($data = null, $null = false); ``` The $null argument allows the data to be null when running the ```check``` method. -All classes have also the following getters / setters: +All classes have also the following methods: ```php public function setData($data); public function getData(); + +// to avoid the data to be null: public function setNull($null); public function getNull(); -// Not the actual type, shouldn't be used outside the classes -public function setType($type); -public function getType(); +// to check everything (type and range/format) +public function check($data = null); + +// to format things (specially powerfull in ArrayS) +public function format($data = null); +``` + +## Static shortcuts + +The main Structure class provides four static methods to quickly generate checkers: + +```php +public static function ArrayS($format = null, $data = null, $countStrict = true, $null = false); +public static function NumericS($range = null, $data = null, $null = false); +public static function IntegerS($range = null, $data = null, $null = false); +public static function FloatS($range = null, $data = null, $null = false); ``` +All these methods return respectively an ArrayS, NumericS, IntegerS or FloatS object, with the + properties set as passed by the arguments. + ## Class ScalarS This class runs the ```is_scalar()``` test to a variable. @@ -101,7 +119,7 @@ This class runs the ```is_scalar()``` test to a variable. Usage: ```php $scalar = new \Structure\ScalarS(); -$scalar->checkType($var); +$scalar->check($var); ``` ## Class NumericS @@ -159,7 +177,7 @@ This class runs the ```is_string()``` test against a variable. Usage: ```php $string = new \Structure\StringS(); -$string->checkType($var); +$string->check($var); ``` ## Class ArrayS @@ -224,7 +242,7 @@ You can read more documentation by running ```composer doc``` (phpdoc needed) an # Planned features -* [ ] Quick static functions for type testing (ArrayS::check()) +* [x] Quick static functions for type testing (ArrayS::check()) * [ ] Date formats: timestamp, mysql date/time/datetime, standard times * [ ] Email, ip, hash formats * [ ] Improvement for ranges: infinities diff --git a/composer.json b/composer.json index 3f16faf..3e286c5 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "3nr1c/structure", - "version": "0.1", + "version": "0.2", "license": "MIT", "authors": [ { diff --git a/src/Structure/ArrayS.php b/src/Structure/ArrayS.php index c831487..e6af309 100644 --- a/src/Structure/ArrayS.php +++ b/src/Structure/ArrayS.php @@ -1,6 +1,11 @@ countStrict = $countStrict; } - public function checkType($data = null) { + public function check($data = null) { + if ($this->getNull()) { + return (is_null($this->data) || $this->checkType($data)) && $this->checkFormat($data); + } else { + return $this->checkType($data) && $this->checkFormat($data); + } + } + + /** + * @param mixed $data + * @param mixed $format + * @return array + * @throws \Exception + */ + public function format($data = null, $format = null) { + if (!is_null($data)) $this->setData($data); + if (!is_null($format)) $this->setFormat($format); + + if (!is_array($this->data)) { + throw new \Exception("Array format only available for arrays"); + } + + $this->applyFormat(); + return $this->data; + } + + /** + * @param mixed $data + * @return bool + */ + protected function checkType($data = null) { if (!is_null($data)) $this->data = $data; return is_array($this->data); @@ -80,7 +115,7 @@ public function checkType($data = null) { * @return bool * @throws \Exception */ - public function checkFormat($data = null) { + protected function checkFormat($data = null) { if (!is_null($data)) $this->data = $data; if ($this->format === "array") { @@ -121,14 +156,13 @@ public function checkFormat($data = null) { } } - public function check($data = null) { - if ($this->getNull()) { - return (is_null($this->data) || $this->checkType($data)) && $this->checkFormat($data); - } else { - return $this->checkType($data) && $this->checkFormat($data); - } - } - + /** + * @param mixed $data + * @param mixed $format + * @param bool $applyFormat + * @return bool + * @throws \Exception + */ protected function checkValue($data, $format, $applyFormat = false) { $numeric = '/^(numeric|float|integer|int)(\(|\[)-?\d+(\.\d+)?,-?\d+(\.\d+)?(\)|\])$/'; @@ -227,14 +261,20 @@ protected function checkValue($data, $format, $applyFormat = false) { return $valid; } + /** + * @throws \Exception + */ protected function applyFormat() { if (is_string($this->format)) { foreach ($this->data as &$value) { $value = $this->checkValue($value, $this->format, true); } + return true; } - if ($this->isCountStrict() && count($this->data) !== count($this->format)) return false; + if ($this->isCountStrict() && count($this->data) !== count($this->format)) { + throw new \Exception("countStrict doesn't allow comparisons between \$data and \$format"); + } $associativeData = ArrayS::isAssociative($this->data); $associativeFormat = ArrayS::isAssociative($this->format); @@ -252,6 +292,7 @@ protected function applyFormat() { for ($i = 0; $i < count($this->format); $i++) { $this->data[$i] = $this->checkValue($this->data[$i], $this->format[$i], true); } + return true; } else { if ($associativeData) { throw new \Exception("Error to trying to format an associative array to sequential"); @@ -261,18 +302,6 @@ protected function applyFormat() { } } - public function format($data = null, $format = null) { - if (!is_null($data)) $this->setData($data); - if (!is_null($format)) $this->setFormat($format); - - if (!is_array($this->data)) { - throw new \Exception("Array format only available for arrays"); - } - - $this->applyFormat(); - return $this->data; - } - /** * Source: http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential * @param array $data diff --git a/src/Structure/BooleanS.php b/src/Structure/BooleanS.php index 27f615c..57744f7 100644 --- a/src/Structure/BooleanS.php +++ b/src/Structure/BooleanS.php @@ -1,9 +1,12 @@ setType("boolean"); } + /** + * @param mixed $data + * @return bool + */ public function format($data = null) { - return boolval($data); + if (is_string($data)) { + // All strings (except "") evaluate true with boolval + return $data === "true"; + } else { + return boolval($data); + } } } \ No newline at end of file diff --git a/src/Structure/FloatS.php b/src/Structure/FloatS.php index 991594e..54f0d0d 100644 --- a/src/Structure/FloatS.php +++ b/src/Structure/FloatS.php @@ -1,6 +1,11 @@ setType("float"); } + /** + * @param mixed $data + * @return float + */ public function format($data = null) { return floatval($data); } diff --git a/src/Structure/IntegerS.php b/src/Structure/IntegerS.php index f2487a9..ee197ae 100644 --- a/src/Structure/IntegerS.php +++ b/src/Structure/IntegerS.php @@ -1,6 +1,11 @@ setType("integer"); } + /** + * @param mixed $data + * @return int + */ public function format($data = null) { return intval($data); } diff --git a/src/Structure/NumericS.php b/src/Structure/NumericS.php index b97a2c2..e905ee1 100644 --- a/src/Structure/NumericS.php +++ b/src/Structure/NumericS.php @@ -1,6 +1,11 @@ range)) return true; if (!is_null($data)) { @@ -150,6 +155,11 @@ public function check($data = null) { } } + /** + * @param mixed $data + * @return float + * @throws \Exception + */ public function format($data = null) { $validType = $this->checkType($data); $validRange = $this->checkRange($data); diff --git a/src/Structure/ScalarS.php b/src/Structure/ScalarS.php index f616afb..82bcc33 100644 --- a/src/Structure/ScalarS.php +++ b/src/Structure/ScalarS.php @@ -1,6 +1,11 @@ setData($data); } diff --git a/src/Structure/StringS.php b/src/Structure/StringS.php index 93f1202..17f9829 100644 --- a/src/Structure/StringS.php +++ b/src/Structure/StringS.php @@ -1,9 +1,12 @@ setType("string"); } + /** + * @param mixed $data + * @return string + */ public function format($data = null) { return (string)$data; } diff --git a/src/Structure/Structure.php b/src/Structure/Structure.php index 718cbc0..3b6abce 100644 --- a/src/Structure/Structure.php +++ b/src/Structure/Structure.php @@ -1,6 +1,11 @@ setFormat($format); $array->setData($data = null); @@ -99,4 +104,49 @@ public static function ArrayS($format, $data = null, $countStrict = true, $null $array->setNull($null); return $array; } + + /** + * @param string $range + * @param mixed $data + * @param bool $null + * @return NumericS + * @throws \Exception + */ + public static function NumericS($range = null, $data = null, $null = false) { + $numeric = new NumericS(); + $numeric->setRange($range); + $numeric->setData($data); + $numeric->setNull($null); + return $numeric; + } + + /** + * @param string $range + * @param mixed $data + * @param bool $null + * @return IntegerS + * @throws \Exception + */ + public static function IntegerS($range = null, $data = null, $null = false) { + $numeric = new IntegerS(); + $numeric->setRange($range); + $numeric->setData($data); + $numeric->setNull($null); + return $numeric; + } + + /** + * @param string $range + * @param mixed $data + * @param bool $null + * @return FloatS + * @throws \Exception + */ + public static function FloatS($range = null, $data = null, $null = false) { + $numeric = new FloatS(); + $numeric->setRange($range); + $numeric->setData($data); + $numeric->setNull($null); + return $numeric; + } } \ No newline at end of file diff --git a/tests/ArrayFormatTest.php b/tests/ArrayFormatTest.php index ca4fb9f..5af7f06 100644 --- a/tests/ArrayFormatTest.php +++ b/tests/ArrayFormatTest.php @@ -1,9 +1,12 @@ assertTrue($formatter->check($newArray)); } + + public function testFormat2() { + $format = array( + "foo" => "boolean", + "bar" => "float" + ); + + $formatter = \Structure\Structure::ArrayS($format); + + $array = array( + "foo" => "false", + "bar" => "1.3" + ); + + $this->assertFalse($formatter->check($array)); + + $newArray = $formatter->format($array); + + $this->assertTrue($formatter->check($newArray)); + } } diff --git a/tests/ArrayTest.php b/tests/ArrayTest.php index 200c33b..77af84e 100644 --- a/tests/ArrayTest.php +++ b/tests/ArrayTest.php @@ -1,9 +1,12 @@ assertTrue($arrayCheck->check($data)); } + + public function testStrictCount1() { + $array = new \Structure\ArrayS(); + $array->setFormat(array( + "foo" => "string" + )); + + $data = array( + "foo" => "expected key", + "bar" => "unexpected key", + "xyz" => array( + "something", 3, false, true, 3.2 + ) + ); + + $this->assertFalse($array->check($data)); + $array->setCountStrict(false); + $this->assertTrue($array->check($data)); + } } diff --git a/tests/NumericTest.php b/tests/NumericTest.php index 40d31a4..9d954c5 100644 --- a/tests/NumericTest.php +++ b/tests/NumericTest.php @@ -1,25 +1,30 @@ assertTrue($numeric->checkType(3)); + $this->assertTrue($numeric->check(3)); - $this->assertTrue($numeric->checkType(3.2)); + $this->assertTrue($numeric->check(3.2)); - $this->assertTrue($numeric->checkType("1")); + $this->assertTrue($numeric->check("1")); - $this->assertTrue($numeric->checkType("5.7")); + $this->assertTrue($numeric->check("5.7")); - $this->assertTrue($numeric->checkType("5.")); + $this->assertTrue($numeric->check("5.")); - $this->assertFalse($numeric->checkType(array())); - $this->assertFalse($numeric->checkType("1..")); + $this->assertFalse($numeric->check(array())); + $this->assertFalse($numeric->check("1..")); } public function testCorrectRange1() { diff --git a/tests/ScalarTest.php b/tests/ScalarTest.php index 7c1b0f2..788f401 100644 --- a/tests/ScalarTest.php +++ b/tests/ScalarTest.php @@ -1,6 +1,11 @@ setData("Hello world"); - $this->assertTrue($scalar->checkType()); + $this->assertTrue($scalar->check()); $scalar->setData(array()); - $this->assertFalse($scalar->checkType()); + $this->assertFalse($scalar->check()); } } diff --git a/tests/StaticsTest.php b/tests/StaticsTest.php index 7746eb1..28cbcb1 100644 --- a/tests/StaticsTest.php +++ b/tests/StaticsTest.php @@ -1,9 +1,12 @@