-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61cf568
commit b39da4a
Showing
6 changed files
with
156 additions
and
14 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Ertuo; | ||
|
||
use Ertuo\RouteAbstract; | ||
use Ertuo\ExportInterface; | ||
|
||
/** | ||
* Route class that stores the complete unfolded tree inside it | ||
*/ | ||
class UnfoldedRoute extends RouteAbstract implements ExportInterface | ||
{ | ||
/** | ||
* @var array the completely unfolded route tree | ||
*/ | ||
protected $unfolded; | ||
|
||
/** | ||
* Creates a new {@link UnfoldedRoute} from $tree | ||
* | ||
* @param array $tree routes tree array, like the output | ||
* from {@link ExportInterface::toArray()} | ||
* @return self | ||
* | ||
* @see ExportInterface::toArray() | ||
*/ | ||
static function fromArray(array $tree) : self | ||
{ | ||
static $prototype; | ||
if (!$prototype) | ||
{ | ||
$prototype = new self; | ||
} | ||
|
||
// cloning is faster than instantiating, | ||
// check Ertuo\Lab\New_vs_Clone | ||
// | ||
$route = clone $prototype; | ||
$route->unfolded = $tree; | ||
|
||
$route->key = $route->unfolded['key']; | ||
$route->attributes = $route->unfolded['attributes']; | ||
$route->rule = $route->unfolded['rule']; | ||
$route->default = $route->unfolded['default']; | ||
|
||
return $route; | ||
} | ||
|
||
/** | ||
* Read a nested route identified by $step | ||
* | ||
* @param string $step value of current step from the source array | ||
* @return null|RouteAbstract | ||
*/ | ||
function readRoute(string $step) : ?RouteAbstract | ||
{ | ||
if (empty( $this->unfolded['routes'][ $step ] )) | ||
{ | ||
return null; | ||
} | ||
|
||
return static::fromArray( $this->unfolded['routes'][ $step ] ); | ||
} | ||
|
||
/** | ||
* @see ExportInterface::toArray() | ||
*/ | ||
function toArray() : array | ||
{ | ||
return $this->unfolded; | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Ertuo\Tests; | ||
|
||
use Ertuo\RouteAbstract; | ||
use Ertuo\Tests\Unabridged\Web_Test; | ||
use Ertuo\UnfoldedRoute; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function file_put_contents; | ||
use function sys_get_temp_dir; | ||
use function tempnam; | ||
use function unlink; | ||
|
||
class UnfoldedRouteTest extends Web_Test | ||
{ | ||
function getRoutes() : RouteAbstract | ||
{ | ||
file_put_contents( | ||
$unfolded = tempnam(sys_get_temp_dir(), 'unfolded'), | ||
'<?php return ' | ||
. var_export(parent::getRoutes()->toArray(), 1) | ||
. ';' | ||
); | ||
|
||
$routes = UnfoldedRoute::fromArray(include $unfolded); | ||
unlink($unfolded); | ||
|
||
return $routes; | ||
} | ||
|
||
function testCompareExportedArrays() | ||
{ | ||
$parent = parent::getRoutes()->toArray(); | ||
$unfolded = $this->getRoutes()->toArray(); | ||
|
||
$this->assertEquals($parent, $unfolded); | ||
} | ||
} |