Skip to content

Commit

Permalink
Merge 8917b5c into f6f8a95
Browse files Browse the repository at this point in the history
  • Loading branch information
adbario committed Mar 25, 2022
2 parents f6f8a95 + 8917b5c commit 94c3375
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/Dot.php
Expand Up @@ -125,7 +125,6 @@ public function delete($keys)
*
* @param array $array Array to validate
* @param int|string $key The key to look for
*
* @return bool
*/
protected function exists($array, $key)
Expand Down Expand Up @@ -496,6 +495,18 @@ public function toJson($key = null, $options = 0)
return json_encode($this->items, $options);
}

/**
* Output or return a parsable string representation of the
* given array when exported by var_export()
*
* @param array $items
* @return object
*/
public static function __set_state(array $items): object
{
return new static($items);
}

/*
* --------------------------------------------------------------
* ArrayAccess interface
Expand Down
34 changes: 29 additions & 5 deletions tests/DotTest.php
Expand Up @@ -182,7 +182,7 @@ public function testFlatten()
$this->assertEquals('xyz', $flatten['foo.abc']);
$this->assertEquals('baz', $flatten['foo.bar.0']);
}

public function testFlattenWithCustomDelimiter()
{
$dot = new Dot(['foo' => ['abc' => 'xyz', 'bar' => ['baz']]]);
Expand Down Expand Up @@ -636,10 +636,10 @@ public function testToJsonAll()

public function testToJsonAllWithOption()
{
$dot = new Dot(['foo' => "'bar'"]);
$dot = new Dot(['foo' => 'bar']);

$this->assertJsonStringEqualsJsonString(
json_encode(['foo' => "'bar'"], JSON_HEX_APOS),
json_encode(['foo' => 'bar'], JSON_HEX_APOS),
$dot->toJson(JSON_HEX_APOS)
);
}
Expand All @@ -656,14 +656,38 @@ public function testToJsonKey()

public function testToJsonKeyWithOptions()
{
$dot = new Dot(['foo' => ['bar' => "'value'"]]);
$dot = new Dot(['foo' => ['bar' => 'value']]);

$this->assertEquals(
json_encode(['bar' => "'value'"], JSON_HEX_APOS),
json_encode(['bar' => 'value'], JSON_HEX_APOS),
$dot->toJson('foo', JSON_HEX_APOS)
);
}

/*
* --------------------------------------------------------------
* Export
* --------------------------------------------------------------
*/

public function testVarExport()
{
$dot = new Dot(['foo' => ['bar' => 'baz']]);

$this->assertEquals(
"Adbar\Dot::__set_state(array(\n" .
" 'items' => \n" .
" array (\n" .
" 'foo' => \n" .
" array (\n" .
" 'bar' => 'baz',\n" .
" ),\n" .
" ),\n" .
"))",
var_export($dot, true)
);
}

/*
* --------------------------------------------------------------
* Countable interface
Expand Down

0 comments on commit 94c3375

Please sign in to comment.