Skip to content

Commit 1fda6fd

Browse files
committed
Update Entity::__toString method
1 parent 87b2b9b commit 1fda6fd

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/Entity.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,21 @@ public function __get(string $property) : mixed
110110
throw $this->propertyNotDefined($property);
111111
}
112112

113+
/**
114+
* Converts the entity to a JSON string.
115+
* All properties will be included.
116+
* Please note that sensitive property data may be exposed!
117+
*
118+
* @return string
119+
*/
113120
public function __toString() : string
114121
{
115-
return \json_encode($this, $this->_jsonFlags); // @phpstan-ignore-line
122+
$origin = $this->_jsonVars;
123+
$all = \array_keys($this->getObjectVars());
124+
$this->_jsonVars = $all;
125+
$json = \json_encode($this, $this->_jsonFlags);
126+
$this->_jsonVars = $origin;
127+
return $json; // @phpstan-ignore-line
116128
}
117129

118130
protected function propertyNotDefined(string $property) : OutOfBoundsException

tests/EntityTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,14 @@ public function testJsonVars() : void
183183

184184
public function testToString() : void
185185
{
186+
$origin = $this->entity->_jsonVars;
186187
$json = (string) $this->entity;
188+
self::assertSame($origin, $this->entity->_jsonVars);
187189
self::assertStringStartsWith('{', $json);
188-
self::assertStringEndsWith('}', $json);
190+
$values = \json_decode($json, true);
191+
self::assertArrayHasKey('array', $values);
192+
self::assertIsArray($values['array']);
193+
self::assertArrayHasKey('id', $values);
194+
self::assertNull($values['id']);
189195
}
190196
}

0 commit comments

Comments
 (0)