-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from kokspflanze/formatter-generate-link
added new formatter to generate links from value
- Loading branch information
Showing
4 changed files
with
207 additions
and
1 deletion.
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
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,154 @@ | ||
<?php | ||
|
||
|
||
namespace ZfcDatagrid\Column\Formatter; | ||
|
||
|
||
use Zend\ServiceManager\ServiceManager; | ||
use Zend\ServiceManager\ServiceManagerAwareInterface; | ||
use ZfcDatagrid\Column\AbstractColumn; | ||
|
||
class GenerateLink extends AbstractFormatter implements ServiceManagerAwareInterface | ||
{ | ||
protected $validRenderers = [ | ||
'jqGrid', | ||
'bootstrapTable', | ||
]; | ||
|
||
/** @var string */ | ||
protected $route; | ||
/** @var array */ | ||
protected $routeParams; | ||
/** @var string|null */ | ||
protected $routeKey; | ||
/** @var ServiceManager */ | ||
protected $serviceManager; | ||
/** @var \Zend\View\Renderer\PhpRenderer */ | ||
protected $viewRenderer; | ||
|
||
/** | ||
* @param ServiceManager $sm | ||
* @param $route | ||
* @param null $key | ||
* @param array $params | ||
*/ | ||
public function __construct( ServiceManager $sm, $route, $key = null, $params = array() ) | ||
{ | ||
$this->setServiceManager($sm); | ||
$this->setRoute($route); | ||
$this->setRouteParams($params); | ||
$this->setRouteKey($key); | ||
} | ||
|
||
/** | ||
* @param AbstractColumn $columnUniqueId | ||
* @return string | ||
*/ | ||
public function getFormattedValue( AbstractColumn $column ) | ||
{ | ||
$row = $this->getRowData(); | ||
$value = $row[$column->getUniqueId()]; | ||
|
||
$routeKey = !is_null($this->getRouteKey())? | ||
$this->getRouteKey() | ||
: | ||
$column->getUniqueId(); | ||
|
||
$params = $this->getRouteParams(); | ||
$params[$routeKey] = $value; | ||
|
||
$url = (string) $this->getViewRenderer()->url($this->getRoute(), $params); | ||
|
||
return sprintf('<a href="%s">%s</a>', $url, $value); | ||
} | ||
|
||
/** | ||
* Set service manager | ||
* @param ServiceManager $serviceManager | ||
*/ | ||
public function setServiceManager( ServiceManager $serviceManager ) | ||
{ | ||
$this->serviceManager = $serviceManager; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return ServiceManager | ||
*/ | ||
public function getServiceManager() | ||
{ | ||
return $this->serviceManager; | ||
} | ||
|
||
/** | ||
* @return \Zend\View\Renderer\PhpRenderer | ||
*/ | ||
public function getViewRenderer() | ||
{ | ||
if (! $this->viewRenderer) { | ||
$this->viewRenderer = $this->getServiceManager()->get('ViewRenderer'); | ||
} | ||
|
||
return $this->viewRenderer; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getRoute() | ||
{ | ||
return $this->route; | ||
} | ||
|
||
/** | ||
* @param string $route | ||
* @return GenerateLink | ||
*/ | ||
public function setRoute( $route ) | ||
{ | ||
$this->route = $route; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getRouteParams() | ||
{ | ||
return $this->routeParams; | ||
} | ||
|
||
/** | ||
* @param array $routeParams | ||
* @return GenerateLink | ||
*/ | ||
public function setRouteParams( $routeParams ) | ||
{ | ||
$this->routeParams = $routeParams; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return null|string | ||
*/ | ||
public function getRouteKey() | ||
{ | ||
return $this->routeKey; | ||
} | ||
|
||
/** | ||
* @param null|string $routeKey | ||
* @return GenerateLink | ||
*/ | ||
public function setRouteKey( $routeKey ) | ||
{ | ||
$this->routeKey = $routeKey; | ||
|
||
return $this; | ||
} | ||
|
||
|
||
} |
50 changes: 50 additions & 0 deletions
50
tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php
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,50 @@ | ||
<?php | ||
|
||
|
||
namespace ZfcDatagridTest\Column\Formatter; | ||
|
||
|
||
use Zend\ServiceManager\ServiceManager; | ||
use ZfcDatagrid\Column\Formatter\GenerateLink; | ||
|
||
/** | ||
* @group Column | ||
* @covers ZfcDatagrid\Column\Formatter\GenerateLink | ||
*/ | ||
class GenerateLinkTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testConstructor() | ||
{ | ||
$generateLink = new GenerateLink(new ServiceManager(), 'route'); | ||
|
||
$this->assertInstanceOf('Zend\ServiceManager\ServiceManager', $generateLink->getServiceManager()); | ||
$this->assertEquals('route', $generateLink->getRoute()); | ||
$this->assertEmpty($generateLink->getRouteKey()); | ||
$this->assertEmpty($generateLink->getRouteParams()); | ||
} | ||
|
||
public function testGetFormattedValue() | ||
{ | ||
$col = $this->getMockForAbstractClass('ZfcDatagrid\Column\AbstractColumn'); | ||
$col->setUniqueId('foo'); | ||
|
||
$phpRenderer = $this->getMockBuilder('Zend\View\Renderer\PhpRenderer') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$phpRenderer->expects($this->any()) | ||
->method('url') | ||
->will($this->returnValue('')); | ||
|
||
$serviceManager = new ServiceManager(); | ||
$serviceManager->setService('ViewRenderer', $phpRenderer); | ||
|
||
$generateLink = new GenerateLink($serviceManager, 'route'); | ||
$generateLink->setRowData([ | ||
'foo' => 'bar', | ||
]); | ||
|
||
$this->assertEquals('<a href="">bar</a>', $generateLink->getFormattedValue($col)); | ||
} | ||
|
||
} |