Skip to content

Commit

Permalink
Version 0.2. Added formatting and static shortcuts. Fixed some bugs a…
Browse files Browse the repository at this point in the history
…nd refactored ArrayS code.
  • Loading branch information
3nr1c committed Jul 14, 2015
1 parent 2328568 commit fa0d686
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 69 deletions.
34 changes: 26 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand Down Expand Up @@ -81,27 +81,45 @@ 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.

Usage:
```php
$scalar = new \Structure\ScalarS();
$scalar->checkType($var);
$scalar->check($var);
```

## Class NumericS
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "3nr1c/structure",
"version": "0.1",
"version": "0.2",
"license": "MIT",
"authors": [
{
Expand Down
75 changes: 52 additions & 23 deletions src/Structure/ArrayS.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
/**
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @author Enric Florit
* @since 0.1.0
* @date 13/7/15
*/

Expand Down Expand Up @@ -68,7 +73,37 @@ public function setCountStrict($countStrict) {
$this->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);
Expand All @@ -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") {
Expand Down Expand Up @@ -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+)?(\)|\])$/';

Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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
Expand Down
22 changes: 17 additions & 5 deletions src/Structure/BooleanS.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
/**
* Created by PhpStorm.
* User: enric
* Date: 14/7/15
* Time: 14:52
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @author Enric Florit
* @since 0.2.0
* @date 14/7/15
*/

namespace Structure;
Expand All @@ -19,7 +22,16 @@ public function __construct($data = null, $null = false) {
$this->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);
}
}
}
9 changes: 9 additions & 0 deletions src/Structure/FloatS.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
/**
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @author Enric Florit
* @since 0.1.0
* @date 13/7/15
*/

Expand All @@ -17,6 +22,10 @@ public function __construct($data = null, $null = false) {
$this->setType("float");
}

/**
* @param mixed $data
* @return float
*/
public function format($data = null) {
return floatval($data);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Structure/IntegerS.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
/**
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @author Enric Florit
* @since 0.1.0
* @date 13/7/15
*/

Expand All @@ -17,6 +22,10 @@ public function __construct($data = null, $null = false) {
$this->setType("integer");
}

/**
* @param mixed $data
* @return int
*/
public function format($data = null) {
return intval($data);
}
Expand Down
12 changes: 11 additions & 1 deletion src/Structure/NumericS.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
/**
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @author Enric Florit
* @since 0.1.0
* @date 13/7/15
*/

Expand Down Expand Up @@ -114,7 +119,7 @@ public function getRange() {
* @param integer|float $data
* @return bool
*/
public function checkRange($data = null) {
protected function checkRange($data = null) {
if (is_null($this->range)) return true;

if (!is_null($data)) {
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/Structure/ScalarS.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
/**
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @author Enric Florit
* @since 0.1.0
* @date 13/7/15
*/

Expand All @@ -20,7 +25,7 @@ public function __construct($data = null, $null = false) {
* @param mixed $data
* @return bool
*/
public function checkType($data = null) {
protected function checkType($data = null) {
if (!is_null($data)) {
$this->setData($data);
}
Expand Down
15 changes: 11 additions & 4 deletions src/Structure/StringS.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
/**
* Created by PhpStorm.
* User: enric
* Date: 13/7/15
* Time: 16:23
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @author Enric Florit
* @since 0.1.0
* @date 13/7/15
*/

namespace Structure;
Expand All @@ -19,6 +22,10 @@ public function __construct($data = null, $null = false) {
$this->setType("string");
}

/**
* @param mixed $data
* @return string
*/
public function format($data = null) {
return (string)$data;
}
Expand Down
Loading

0 comments on commit fa0d686

Please sign in to comment.