deblan.tv/vendor/trinity/src/Trinity/Bundle/ContentManagerBundle/Routing/NodeRoute.php
2015-03-02 21:57:49 +01:00

84 lines
2.1 KiB
PHP

<?php
namespace Trinity\Bundle\ContentManagerBundle\Routing;
use \Symfony\Component\Routing\Route;
class NodeRoute extends Route
{
public function setController($controller)
{
$this->addDefaults(array('_controller' => $controller));
}
public function getNode()
{
$defaults = $this->getDefaults();
if (array_key_exists('_node',$defaults)) {
return $defaults['_node'];
}
return null;
}
public function addNode($node_id)
{
$defaults = $this->getDefaults();
if (!array_key_exists('_node',$defaults)) {
$defaults['_node'] = $node_id;
return $this->setDefaults($defaults);
}
$know_node = $defaults['_node'];
if (is_array($know_node)) {
$know_node[] = $node_id;
$defaults['node'] = $know_node;
} else {
$defaults['_node'] = array($know_node,$node_id);
}
return $this->setDefaults($defaults);
}
/**
* Add _local parameter to url
* @param $locales regex of acceptable locales (ie: en|fr|de|it)
* @return void
*/
public function addLocale($locales)
{
if (0 === preg_match('/\{_locale\}/', $this->getPath())) {
$this->setPath(sprintf('/{_locale}/%s', ltrim($this->getPath(), '/')));
}
$this->addRequirements(array('_locale' => $locales));
}
public function removeLocale()
{
if (0 !== preg_match('/\{_locale\}/', $this->getPath())) {
$parts = explode('/', $this->getPath());
foreach ($parts as $k => $v) {
if ($v === '{_locale}') {
unset($parts[$k]);
}
}
$this->setPath(implode('/', $parts));
}
}
public function addPage()
{
$this->addDefaults(array('page' => '1'));
$this->addRequirements(array('page' => '\d+'));
if (0 === preg_match('/\{page\}/', $this->getPath())) {
$this->setPath(sprintf('%s{page}', substr($this->getPath(), strlen($this->getPath())-1) == '/' ? $this->getPath() : $this->getPath().'/'));
}
}
}