Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
fix(JsonObject): fix JsonObject::hasField method to return if the fie…
Browse files Browse the repository at this point in the history
…ld has a value set

Closes #173
  • Loading branch information
Jens Schulze committed May 27, 2016
1 parent 940e90a commit c2eaed5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/Model/Common/JsonObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __call($method, $arguments)
$action = substr($method, 0, 3);
$field = lcfirst(substr($method, 3));

if (!$this->hasField($field)) {
if (!$this->isValidField($field)) {
if ($action == 'get' || $action == 'set') {
throw new \BadMethodCallException(
sprintf(Message::UNKNOWN_FIELD, $field, $method, implode(', ', $arguments))
Expand All @@ -72,20 +72,29 @@ public function __call($method, $arguments)

public function __get($field)
{
if (!$this->hasField($field)) {
if (!$this->isValidField($field)) {
throw new \BadMethodCallException(
sprintf(Message::UNKNOWN_FIELD, $field, 'get', $field)
);
}
return $this->get($field);
}

/**
* @param $field
* @return bool
*/
public function hasField($field)
{
return isset($this->typeData[$field]);
}

/**
* @param string $field
* @return bool
* @internal
*/
protected function hasField($field)
protected function isValidField($field)
{
if (isset($this->fieldDefinitions()[$field])) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Model/CustomField/FieldContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function set($field, $value)
return parent::set($field, $value);
}

public function hasField($field)
protected function isValidField($field)
{
return true;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/Model/Common/JsonObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,18 @@ public function testOptional()
$this->assertTrue($obj->isOptional('optional'));
$this->assertFalse($obj->isOptional('required'));
}

public function testHasField()
{
$obj = JsonObject::fromArray(['test' => 1234]);

$this->assertTrue($obj->hasField('test'));
}

public function testNotHasField()
{
$obj = JsonObject::fromArray([]);

$this->assertFalse($obj->hasField('test'));
}
}
10 changes: 10 additions & 0 deletions tests/unit/Model/CustomField/CustomFieldObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,14 @@ public function testData($dataArray, $type, $elementType = null)
}
$this->assertJsonStringEqualsJsonString(json_encode($value), json_encode($field));
}


public function testHasField()
{
$container = FieldContainer::of();
$this->assertFalse($container->hasField('test'));

$container->set('test', 1234);
$this->assertTrue($container->hasField('test'));
}
}

0 comments on commit c2eaed5

Please # to comment.