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

Commit c2eaed5

Browse files
author
Jens Schulze
committed
fix(JsonObject): fix JsonObject::hasField method to return if the field has a value set
Closes #173
1 parent 940e90a commit c2eaed5

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

src/Model/Common/JsonObject.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __call($method, $arguments)
5050
$action = substr($method, 0, 3);
5151
$field = lcfirst(substr($method, 3));
5252

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

7373
public function __get($field)
7474
{
75-
if (!$this->hasField($field)) {
75+
if (!$this->isValidField($field)) {
7676
throw new \BadMethodCallException(
7777
sprintf(Message::UNKNOWN_FIELD, $field, 'get', $field)
7878
);
7979
}
8080
return $this->get($field);
8181
}
8282

83+
/**
84+
* @param $field
85+
* @return bool
86+
*/
87+
public function hasField($field)
88+
{
89+
return isset($this->typeData[$field]);
90+
}
91+
8392
/**
8493
* @param string $field
8594
* @return bool
8695
* @internal
8796
*/
88-
protected function hasField($field)
97+
protected function isValidField($field)
8998
{
9099
if (isset($this->fieldDefinitions()[$field])) {
91100
return true;

src/Model/CustomField/FieldContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function set($field, $value)
5757
return parent::set($field, $value);
5858
}
5959

60-
public function hasField($field)
60+
protected function isValidField($field)
6161
{
6262
return true;
6363
}

tests/unit/Model/Common/JsonObjectTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,18 @@ public function testOptional()
251251
$this->assertTrue($obj->isOptional('optional'));
252252
$this->assertFalse($obj->isOptional('required'));
253253
}
254+
255+
public function testHasField()
256+
{
257+
$obj = JsonObject::fromArray(['test' => 1234]);
258+
259+
$this->assertTrue($obj->hasField('test'));
260+
}
261+
262+
public function testNotHasField()
263+
{
264+
$obj = JsonObject::fromArray([]);
265+
266+
$this->assertFalse($obj->hasField('test'));
267+
}
254268
}

tests/unit/Model/CustomField/CustomFieldObjectTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,14 @@ public function testData($dataArray, $type, $elementType = null)
159159
}
160160
$this->assertJsonStringEqualsJsonString(json_encode($value), json_encode($field));
161161
}
162+
163+
164+
public function testHasField()
165+
{
166+
$container = FieldContainer::of();
167+
$this->assertFalse($container->hasField('test'));
168+
169+
$container->set('test', 1234);
170+
$this->assertTrue($container->hasField('test'));
171+
}
162172
}

0 commit comments

Comments
 (0)