Skip to content

Commit

Permalink
Fixed issues with $null. Fixed #1
Browse files Browse the repository at this point in the history
  • Loading branch information
3nr1c committed Jul 15, 2015
1 parent e72bdb8 commit 30631c3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
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.2.1",
"version": "0.2.2",
"license": "MIT",
"authors": [
{
Expand Down
13 changes: 9 additions & 4 deletions src/Structure/ArrayS.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function setCountStrict($countStrict) {

public function check($data = null) {
if ($this->getNull()) {
return is_null($this->data) || ($this->checkType($data) && $this->checkFormat($data));
return (is_null($this->data) || $this->checkType($data)) && $this->checkFormat($data);
} else {
return $this->checkType($data) && $this->checkFormat($data);
}
Expand Down Expand Up @@ -141,7 +141,7 @@ protected function checkFormat($data = null) {
if ($associativeData && $associativeFormat) {
foreach ($this->getFormat() as $key=>$value) {
if (!array_key_exists($key, $this->data)) {
$valid = false;
$valid = $this->getNull();
} else {
$valid = $this->checkValue($this->data[$key], $value);
}
Expand Down Expand Up @@ -185,6 +185,7 @@ protected function checkValue($data, $format, $applyFormat = false) {
break;
}
/** @var NumericS $structure */
$structure->setNull($this->getNull());
$structure->setRange(preg_replace("/^(numeric|float|integer)/", "", $format));
if ($applyFormat) {
return $structure->format($data);
Expand Down Expand Up @@ -288,8 +289,12 @@ protected function applyFormat() {

if ($associativeData && $associativeFormat) {
foreach ($this->getFormat() as $key=>$value) {
if (!array_key_exists($key, $this->data) && !$this->null) {
throw new \Exception("Non existent key '" . $key . "'");
if (!array_key_exists($key, $this->data)) {
if ($this->null) {
$this->data[$key] = null;
} else {
throw new \Exception("Undefined key '" . $key . "'");
}
} else {
$this->data[$key] = $this->checkValue($this->data[$key], $value, true);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Structure/FloatS.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function __construct($data = null, $null = false) {
* @return float
*/
public function format($data = null) {
return floatval($data);
$data = parent::format($data);

if (!is_null($data)) return floatval($data);
else return null;
}
}
5 changes: 4 additions & 1 deletion src/Structure/IntegerS.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function __construct($data = null, $null = false) {
* @return int
*/
public function format($data = null) {
return intval($data);
$data = parent::format($data);

if (!is_null($data)) return intval($data);
else return null;
}
}
20 changes: 16 additions & 4 deletions src/Structure/NumericS.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ protected function checkRange($data = null) {
*/
public function check($data = null) {
if ($this->getNull()) {
return is_null($this->data) || ($this->checkType($data) && $this->checkRange($data));
return (is_null($this->data) || $this->checkType($data)) && $this->checkRange($data);
} else {
return $this->checkType($data) && $this->checkRange($data);
}
Expand All @@ -167,14 +167,26 @@ public function format($data = null) {
if ($validType && $validRange) {
return $data;
} else if ($validType && !$validRange) {
throw new \Exception("Unable to format " . $this->getType() . " to range" . $this->getRange());

if ($this->getNull()) {
return null;
} else {
throw new \Exception("Unable to format " . $this->getType() . " to range" . $this->getRange());
}

} else if (!$validType) {
if (!settype($data, $this->getType())) {
$data = (float)$data;
}
$validRange = $this->checkRange($data);
if ($validRange) return $data;
else throw new \Exception("Unable to format " . $this->getType() . " to range" . $this->getRange());

if ($validRange) {
return $data;
} else if ($this->getNull()) {
return null;
} else {
throw new \Exception("Unable to format " . $this->getType() . " to range" . $this->getRange());
}
}
}
}
20 changes: 20 additions & 0 deletions tests/ArrayFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,24 @@ public function testFormat2() {

$this->assertTrue($formatter->check($newArray));
}

public function testFormat3() {
$format = array(
"full_name" => "string",
"height" => "integer",
"weight" => "integer",
"gender" => "integer[0,2]"
);
$formatter = \Structure\Structure::ArrayS($format, null, false, true);

$array = array(
"full_name" => "My Name",
"height" => 175,
"gender" => "-1"
);

$this->assertFalse($formatter->check($array));
$newArray = $formatter->format($array);
$this->assertTrue($formatter->check($newArray));
}
}

0 comments on commit 30631c3

Please sign in to comment.