Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge f4050dd into c584380
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Jan 9, 2019
2 parents c584380 + f4050dd commit cec420e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/Resolver/ValueInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
namespace Zend\Di\Resolver;

use Psr\Container\ContainerInterface;
use ReflectionObject;
use ReflectionMethod;
use Zend\Di\Exception\LogicException;

use function is_array;
use function is_object;
use function is_scalar;
use function method_exists;
use function trigger_error;
use function var_export;

use const E_USER_DEPRECATED;

/**
Expand Down Expand Up @@ -68,15 +74,36 @@ public function export() : string
*/
public function isExportable() : bool
{
if (is_scalar($this->value) || ($this->value === null)) {
return $this->isExportableRecursive($this->value);
}

/**
* Check if the provided value is exportable.
* For arrays it uses recursion.
*
* @param mixed $value
* @return bool
*/
private function isExportableRecursive($value) : bool
{
if (is_scalar($value) || $value === null) {
return true;
}

if (is_array($value)) {
foreach ($value as $item) {
if (! $this->isExportableRecursive($item)) {
return false;
}
}

return true;
}

if (is_object($this->value) && method_exists($this->value, '__set_state')) {
$reflection = new ReflectionObject($this->value);
$method = $reflection->getMethod('__set_state');
if (is_object($value) && method_exists($value, '__set_state')) {
$method = new ReflectionMethod($value, '__set_state');

return ($method->isStatic() && $method->isPublic());
return $method->isStatic() && $method->isPublic();
}

return false;
Expand Down
19 changes: 19 additions & 0 deletions test/Resolver/ValueInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ public function provideExportableValues()
'null' => [null],
'float' => [microtime(true)],
'object' => [new TestAsset\Resolver\ExportableValue()],
'array' => [[]],
'array-string' => [['TestValue', 'OtherValue']],
'array-int' => [[123, 456]],
'array-mixed' => [
[
new TestAsset\Resolver\ExportableValue(),
[1],
null,
false,
true,
time(),
microtime(true),
[[], []],
uniqid(),
[],
],
],
];
}

Expand All @@ -111,6 +128,8 @@ public function provideUnexportableItems()
'stream' => [$this->streamFixture],
'noSetState' => [new TestAsset\Resolver\UnexportableValue1()],
'privateSetState' => [new TestAsset\Resolver\UnexportableValue2()],
'arrayNoSetState' => [[new TestAsset\Resolver\UnexportableValue1()]],
'arrayPrivateSetState' => [[new TestAsset\Resolver\UnexportableValue2()]],
];
}

Expand Down

0 comments on commit cec420e

Please sign in to comment.