-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for an $each operator
- Loading branch information
Showing
5 changed files
with
222 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
/** | ||
* @author Todd Burry <[email protected]> | ||
* @copyright 2009-2019 Vanilla Forums Inc. | ||
* @license Proprietary | ||
* @license MIT | ||
*/ | ||
|
||
namespace Garden\JSON; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
/** | ||
* @author Todd Burry <[email protected]> | ||
* @copyright 2009-2019 Vanilla Forums Inc. | ||
* @license Proprietary | ||
* @license MIT | ||
*/ | ||
|
||
namespace Garden\JSON\Tests; | ||
|
@@ -111,4 +111,10 @@ public function testNumericRelativeArray() { | |
$actual = $t->transform(['a', 'b']); | ||
$this->assertSame(['b', 'a'], $actual); | ||
} | ||
|
||
public function testNonArrayContext() { | ||
$t = new Transformer('/foo'); | ||
$actual = $t('baz'); | ||
$this->assertSame(null, $actual); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
/** | ||
* @author Todd Burry <[email protected]> | ||
* @copyright 2009-2019 Vanilla Forums Inc. | ||
* @license MIT | ||
*/ | ||
|
||
namespace Garden\JSON\Tests; | ||
|
||
use Garden\JSON\Transformer; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class EachTest extends TestCase { | ||
/** | ||
* Arrays can be iterated over with the `$each` expression. | ||
*/ | ||
public function testBasicEach() { | ||
$t = new Transformer([ | ||
'$each' => '/', | ||
'$item' => [ | ||
'id' => 'ID', | ||
], | ||
]); | ||
|
||
$actual = $t->transform([ | ||
'a' => ['ID' => 1], | ||
'b' => ['ID' => 2] | ||
]); | ||
|
||
|
||
$this->assertSame(['a' => ['id' => 1], 'b' => ['id' => 2]], $actual); | ||
} | ||
|
||
/** | ||
* Strip string keys. | ||
*/ | ||
public function testIndexEach() { | ||
$t = new Transformer([ | ||
'$each' => '/', | ||
'$item' => '', | ||
'$key' => '$index', | ||
]); | ||
|
||
$actual = $t(['a' => 'a', 'b' => 'b']); | ||
$this->assertSame(['a', 'b'], $actual); | ||
} | ||
|
||
/** | ||
* The key can also be a spec. | ||
*/ | ||
public function testKeySpec() { | ||
$t = new Transformer([ | ||
'$each' => '/', | ||
'$item' => 'id', | ||
'$key' => 'name' | ||
]); | ||
|
||
$actual = $t([['id' => 1, 'name' => 'foo']]); | ||
$this->assertSame(['foo' => 1], $actual); | ||
} | ||
|
||
/** | ||
* An `$item` without `$each` is an exception. | ||
* | ||
* @expectedException \Garden\JSON\InvalidSpecException | ||
* @expectedExceptionMessageRegExp `^Missing key \$each at /` | ||
*/ | ||
public function testMissingEach() { | ||
$t = new Transformer(['$item' => 'b']); | ||
$t([]); | ||
} | ||
|
||
/** | ||
* An `$item` without `$each` is an exception. | ||
* | ||
* @expectedException \Garden\JSON\InvalidSpecException | ||
* @expectedExceptionMessageRegExp `^Missing key \$item at /` | ||
*/ | ||
public function testMissingItem() { | ||
$t = new Transformer(['$each' => 'b']); | ||
$t([]); | ||
} | ||
|
||
/** | ||
* An each reference that doesn't resolve should return null. | ||
*/ | ||
public function testEachNotFound() { | ||
$t = new Transformer(['$each' => 'a', '$item' => 'b']); | ||
$actual = $t(['fff']); | ||
$this->assertSame(null, $actual); | ||
} | ||
} |