Skip to content
Permalink
Browse files

added Arrays::toObject()

  • Loading branch information...
dg committed Mar 15, 2019
1 parent 632465f commit ed27db45a148c48659a544801a866696e35a9eba
Showing with 44 additions and 0 deletions.
  1. +14 −0 src/Utils/Arrays.php
  2. +30 −0 tests/Utils/Arrays.toObject.phpt
@@ -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;
}
}
@@ -0,0 +1,30 @@
<?php
/**
* Test: Nette\Utils\Arrays::toObject()
*/
declare(strict_types=1);
use Nette\Utils\Arrays;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
test(function () {
$obj = new stdClass;
$res = Arrays::toObject([], $obj);
Assert::same($res, $obj);
Assert::type(stdClass::class, $res);
Assert::same([], (array) $res);
});
test(function () {
$obj = new stdClass;
$res = Arrays::toObject(['a' => 1, 'b' => 2], $obj);
Assert::same($res, $obj);
Assert::type(stdClass::class, $res);
Assert::same(['a' => 1, 'b' => 2], (array) $res);
});

0 comments on commit ed27db4

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.