Skip to content

Commit d80f4ca

Browse files
committed
PhpGenerator\Helpers: added createObject() (experimental)
1 parent fb027e7 commit d80f4ca

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/PhpGenerator/Helpers.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private static function _dump(&$var, $level = 0)
118118
}
119119
return get_class($var) === 'stdClass'
120120
? "(object) array($s)"
121-
: get_class($var) . "::__set_state(array($s))";
121+
: __CLASS__ . "::createObject('" . get_class($var) . "', array($s))";
122122

123123
} else {
124124
return var_export($var, TRUE);
@@ -180,4 +180,11 @@ public static function formatMember($name)
180180
: $name ;
181181
}
182182

183+
184+
185+
public static function createObject($class, array $props)
186+
{
187+
return unserialize('O' . substr(serialize((string) $class), 1, -1) . substr(serialize($props), 1));
188+
}
189+
183190
}

tests/PhpGenerator/PhpGenerator.dump.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ Assert::same( "array(\n\t'a' => 1,\n\tarray(\n\t\t\"\\r\" => \"\\r\",\n\t\t2,\n\
4444

4545
Assert::same( "(object) array(\n\t'a' => 1,\n\t'b' => 2,\n)", Helpers::dump((object) array('a' => 1, 'b' => 2)) );
4646
Assert::same( "(object) array(\n\t'a' => (object) array(\n\t\t'b' => 2,\n\t),\n)" , Helpers::dump((object) array('a' => (object) array('b' => 2))) );
47-
Assert::same( "Test::__set_state(array(\n\t'a' => 1,\n\t'b' => 2,\n\t'c' => 3,\n))", Helpers::dump(new Test) );
47+
Assert::same( "Nette\\Utils\\PhpGenerator\\Helpers::createObject('Test', array(\n\t'a' => 1,\n\t'b' => 2,\n\t'c' => 3,\n))", Helpers::dump(new Test) );

0 commit comments

Comments
 (0)