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

Commit

Permalink
Added path builder
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Jun 2, 2015
1 parent c767c0f commit 759af89
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
78 changes: 78 additions & 0 deletions lib/PathBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Sulu\Component\DocumentManager;

/**
* The path builder provides a way to create paths from templates
*/
class PathBuilder
{
/**
* @var PathSegmentRegistry
*/
private $registry;

/**
* @param PathSegmentRegistry $registry
*/
public function __construct(PathSegmentRegistry $registry)
{
$this->registry = $registry;
}

/**
* Build a path from an array of path segments.
*
* Segments demarcated by "%" characters will be interpreted as path
* segment *names* and their value will be resolved from the PathSegmentRegistry.
*
* Other segments will be interpreted literally.
*
* The following:
*
* ````
* $path = $pathBuilder->build(array('%base%', 'hello', '%articles%'));
* ````
*
* Will yield `/cms/hello/articleDirectory` where `%base%` is "cms" and
* `%articles` is "articleDirectory"
*
* @see Sulu\Component\DocumentManager\PathSegmentRegistry
*
* @param array $segments
* @return string
*/
public function build($segments)
{
$results = array();
foreach ($segments as $segment) {
$result = $this->buildSegment($segment);

if (null === $result) {
continue;
}

$results[] = $result;
}

return '/' . implode('/', $results);
}

/**
* @param string $segment
*/
private function buildSegment($segment)
{
if (empty($segment) || $segment == '/') {
return null;
}

if (substr($segment, 0, 1) == '%') {
if (substr($segment, -1) == '%') {
return $this->registry->getPathSegment(substr($segment, 1, -1));
}
}

return $segment;
}
}
44 changes: 44 additions & 0 deletions tests/Bench/PathBuilderBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Sulu\Component\DocumentManager\Tests\Bench;

use Sulu\Component\DocumentManager\PathBuilder;
use Sulu\Component\DocumentManager\PathSegmentRegistry;
use PhpBench\Benchmark;

class PathBuilderBench implements Benchmark
{
private $pathBuilder;

public function setUp()
{
$registry = new PathSegmentRegistry(array(
'one' => 'hello',
'two' => 'goodbye',
));
$this->pathBuilder = new PathBuilder($registry);
}
/**
* @description Build path 1000 times
* @revs 1000
* @beforeMethod setUp
* @paramProvider provideElements
*/
public function benchBuild($iteration)
{
$this->pathBuilder->build($iteration->getParameter('elements'));
}

public function provideElements()
{
return array(
array(
'elements' => array('one', 'two', 'three')
),
array(
'elements' => array('%one', '%two%', 'three'),
),
);
}

}
65 changes: 65 additions & 0 deletions tests/Unit/PathBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Sulu\Component\DocumentManager\Tests\Unit;

use Sulu\Component\DocumentManager\PathSegmentRegistry;
use Sulu\Component\DocumentManager\PathBuilder;
use PhpBench\Benchmark;

class PathBuilderTest extends \PHPUnit_Framework_TestCase implements Benchmark
{
private $pathBuilder;

public function setUp()
{
$pathRegistry = new PathSegmentRegistry(array(
'one' => 'one',
'two' => 'two',
));
$this->pathBuilder = new PathBuilder($pathRegistry);
}

/**
* @description Build path 1000 times
* @revs 1000
* @beforeMethod setUp
*/
public function benchBuild()
{
$this->pathBuilder->build(array('%one%', '%two%', 'four'));
}

/**
* It should build a path
* Using a combination of tokens and literal values
*/
public function testBuild()
{
$result = $this->pathBuilder->build(array('%one%', '%two%', 'four'));
$this->assertEquals('/one/two/four', $result);
}

/**
* It should build "/" for an empty array
*/
public function testBuildEmpty()
{
$this->assertEquals('/', $this->pathBuilder->build(array()));
}

/**
* It should build "/" for an array with "/"
*/
public function testBuildSingleSlash()
{
$this->assertEquals('/', $this->pathBuilder->build(array('/')));
}

/**
* It should replace "//" with "/"
*/
public function testBuildNoDoubleSlash()
{
$this->assertEquals('/hello/world', $this->pathBuilder->build(array('hello', '', '', 'world')));
}
}

0 comments on commit 759af89

Please sign in to comment.