Skip to content

Commit 0190be4

Browse files
committed
Merge branch 'v1.x' into v2.x
2 parents b06fd49 + 53d1934 commit 0190be4

5 files changed

Lines changed: 24 additions & 11 deletions

File tree

FeastTests/JsonTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ public function testUnmarshalMarshal(): void
173173
$this->assertEquals($data, Json::marshal(Json::unmarshal($data, TestJsonItem::class)));
174174
}
175175

176+
public function testUnmarshalMarshalStdClass(): void
177+
{
178+
$data = '{"first_name":"FEAST","last_name":"Framework","test_item":{"first_name":"Jeremy","last_name":"Presutti","calls":4},"second_item":{"also_first_name":"Orlando","also_last_name":"Florida"},"items":[{"first_name":"PHP","last_name":"7.4","calls":null},{"first_name":"PHP","last_name":"8.0","calls":null}],"cards":["4",5,["6"]],"otherItems":{"first":{"first_name":"Json","last_name":"Serializer","calls":null},"second":{"first_name":"Item","last_name":"Parsing","calls":null}},"thirdItems":{"test":"theTest","test2":"theTest2"},"otherSet":[{"first_name":"Json","last_name":"Serializer","calls":null},{"first_name":"Item","last_name":"Parsing","calls":null}],"thirdSet":["theTest","theTest2"],"calls":null,"count":4,"aClass":{"test":"ItWorks"},"timestamp":"20210415","otherTimestamp":"2021-04-05T06:41:00-04:00","thirdTimestamp":"2021-04-15T20:56:24-04:00","fourthTimestamp":"20210405"}';
179+
$this->assertEquals($data, Json::marshal(Json::unmarshal($data, stdClass::class)));
180+
}
181+
176182
public function testUnmarshalMarshalUnmarshalMarshal(): void
177183
{
178184
$data = '{"first_name":"FEAST","last_name":"Framework","test_item":{"first_name":"Jeremy","last_name":"Presutti","calls":4},"second_item":{"also_first_name":"Orlando","also_last_name":"Florida"},"items":[{"first_name":"PHP","last_name":"7.4","calls":null},{"first_name":"PHP","last_name":"8.0","calls":null}],"cards":["4",5,["6"]],"otherItems":{"first":{"first_name":"Json","last_name":"Serializer","calls":null},"second":{"first_name":"Item","last_name":"Parsing","calls":null}},"thirdItems":{"test":"theTest","test2":"theTest2"},"otherSet":[{"first_name":"Json","last_name":"Serializer","calls":null},{"first_name":"Item","last_name":"Parsing","calls":null}],"thirdSet":["theTest","theTest2"],"calls":null,"count":4,"aClass":{"test":"ItWorks"},"timestamp":"20210415","otherTimestamp":"2021-04-05T06:41:00-04:00","thirdTimestamp":"2021-04-15T20:56:24-04:00","fourthTimestamp":"20210405"}';

HttpController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ public function forward(
9393
}
9494

9595
/**
96-
* @param object $responseObject
96+
* @param object|array $responseObject
9797
* @param int|null $jsonResponsePropertyTypes (see https://www.php.net/manual/en/class.reflectionproperty.php#reflectionproperty.constants.modifiers)
9898
* @return void
9999
*/
100-
public function sendJsonResponse(object $responseObject, ?int $jsonResponsePropertyTypes = null): void
100+
public function sendJsonResponse(object|array $responseObject, ?int $jsonResponsePropertyTypes = null): void
101101
{
102102
$this->response->setJsonWithResponseObject($responseObject, $jsonResponsePropertyTypes);
103103
}

Interfaces/ResponseInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ public function getRedirectPath(): ?string;
8787
/**
8888
* Mark the Response as a JSON response and send the passed in object.
8989
*
90-
* @param object $response
90+
* @param object|array $response
9191
* @param int|null $jsonResponsePropertyTypes (see https://www.php.net/manual/en/class.reflectionproperty.php#reflectionproperty.constants.modifiers)
9292
*/
93-
public function setJsonWithResponseObject(object $response, ?int $jsonResponsePropertyTypes = null): void;
93+
public function setJsonWithResponseObject(object|array $response, ?int $jsonResponsePropertyTypes = null): void;
9494

9595
/**
9696
* Set an HTTP header. Overrides previous version set.

Json.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ class Json
4141
*
4242
* The field names are kept as is, unless a Feast\Attributes\JsonItem attribute decorates the property.
4343
*
44-
* @param object $object
44+
* @param object|array $object
4545
* @param int|null $propertyTypesFlag (see https://www.php.net/manual/en/class.reflectionproperty.php#reflectionproperty.constants.modifiers)
4646
* @return string
4747
* @throws ReflectionException
4848
* @see \Feast\Attributes\JsonItem
4949
*/
50-
public static function marshal(object $object, ?int $propertyTypesFlag = null): string
50+
public static function marshal(object|array $object, ?int $propertyTypesFlag = null): string
5151
{
52+
if ($object instanceof stdClass || is_array($object)) {
53+
return json_encode($object);
54+
}
5255
$return = new stdClass();
5356
$paramInfo = self::getClassParamInfo($object::class, $propertyTypesFlag);
5457
/**
@@ -98,15 +101,19 @@ public static function marshal(object $object, ?int $propertyTypesFlag = null):
98101
* @param class-string<returned>|returned $objectOrClass
99102
* @psalm-param object|class-string $objectOrClass
100103
* @param bool $skipConstructor
101-
* @throws Exception\InvalidDateException
104+
* @return returned|object
102105
* @throws JsonException
103106
* @throws ReflectionException
104107
* @throws ServerFailureException
108+
* @throws Exception\InvalidDateException
105109
* @see \Feast\Attributes\JsonItem
106-
* @return returned|object
107110
*/
108111
public static function unmarshal(string $data, string|object $objectOrClass, bool $skipConstructor = false): object
109112
{
113+
if ($objectOrClass === stdClass::class) {
114+
/** @var stdClass */
115+
return json_decode($data);
116+
}
110117
if (is_string($objectOrClass)) {
111118
$object = self::getObjectFromClassString($objectOrClass, $skipConstructor);
112119
} else {

Response.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Response implements ServiceContainerItemInterface, ResponseInterface
3939

4040
private ResponseCode $responseCode = ResponseCode::HTTP_CODE_200;
4141
private bool $isJson = false;
42-
private object|null $jsonResponse = null;
42+
private object|array|null $jsonResponse = null;
4343
private ?int $jsonResponsePropertyTypes = null;
4444
private ?string $redirectPath = null;
4545
/** @var array<string,string> */
@@ -152,10 +152,10 @@ public function redirect(string $path, ResponseCode $code = ResponseCode::HTTP_C
152152
/**
153153
* Mark the Response as a JSON response and send the passed in object.
154154
*
155-
* @param object $response
155+
* @param object|array $response
156156
* @param int|null $jsonResponsePropertyTypes (see https://www.php.net/manual/en/class.reflectionproperty.php#reflectionproperty.constants.modifiers)
157157
*/
158-
public function setJsonWithResponseObject(object $response, ?int $jsonResponsePropertyTypes = null): void
158+
public function setJsonWithResponseObject(object|array $response, ?int $jsonResponsePropertyTypes = null): void
159159
{
160160
$this->jsonResponse = $response;
161161
$this->jsonResponsePropertyTypes = $jsonResponsePropertyTypes;

0 commit comments

Comments
 (0)