diff --git a/src/Utils/Arrays.php b/src/Utils/Arrays.php index 0de1e7523..53a3f8b9c 100644 --- a/src/Utils/Arrays.php +++ b/src/Utils/Arrays.php @@ -280,4 +280,18 @@ public static function map(array $arr, callable $callback): array } return $res; } + + + /** + * Converts array to object + * @param object $obj + * @return object + */ + public static function toObject(array $arr, $obj) + { + foreach ($arr as $k => $v) { + $obj->$k = $v; + } + return $obj; + } } diff --git a/tests/Utils/Arrays.toObject.phpt b/tests/Utils/Arrays.toObject.phpt new file mode 100644 index 000000000..6c8b77bbc --- /dev/null +++ b/tests/Utils/Arrays.toObject.phpt @@ -0,0 +1,30 @@ + 1, 'b' => 2], $obj); + Assert::same($res, $obj); + Assert::type(stdClass::class, $res); + Assert::same(['a' => 1, 'b' => 2], (array) $res); +});