From 219ab5809cd978639260f2b29dd86750502961e7 Mon Sep 17 00:00:00 2001 From: kokspflanze Date: Mon, 6 Apr 2015 20:03:33 +0200 Subject: [PATCH] added new formatter to generate links from value changed phpunit version from ~3.7 to ~4.0 --- autoload_classmap.php | 2 + composer.json | 2 +- .../Column/Formatter/GenerateLink.php | 154 ++++++++++++++++++ .../Column/Formatter/GenerateLinkTest.php | 50 ++++++ 4 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 src/ZfcDatagrid/Column/Formatter/GenerateLink.php create mode 100644 tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php diff --git a/autoload_classmap.php b/autoload_classmap.php index 1a1f0ba2..72d19ad9 100644 --- a/autoload_classmap.php +++ b/autoload_classmap.php @@ -19,6 +19,7 @@ 'ZfcDatagrid\Column\Formatter\FileSize' => __DIR__ . '/src/ZfcDatagrid/Column/Formatter/FileSize.php', 'ZfcDatagrid\Column\Formatter\Image' => __DIR__ . '/src/ZfcDatagrid/Column/Formatter/Image.php', 'ZfcDatagrid\Column\Formatter\Link' => __DIR__ . '/src/ZfcDatagrid/Column/Formatter/Link.php', + 'ZfcDatagrid\Column\Formatter\GenerateLink' => __DIR__ . '/src/ZfcDatagrid/Column/Formatter/GenerateLink.php', 'ZfcDatagrid\Column\Select' => __DIR__ . '/src/ZfcDatagrid/Column/Select.php', 'ZfcDatagrid\Column\Style\AbstractColor' => __DIR__ . '/src/ZfcDatagrid/Column/Style/AbstractColor.php', 'ZfcDatagrid\Column\Style\AbstractStyle' => __DIR__ . '/src/ZfcDatagrid/Column/Style/AbstractStyle.php', @@ -77,6 +78,7 @@ 'ZfcDatagridTest\Column\Formatter\FileSizeTest' => __DIR__ . '/tests/ZfcDatagridTest/Column/Formatter/FileSizeTest.php', 'ZfcDatagridTest\Column\Formatter\ImageTest' => __DIR__ . '/tests/ZfcDatagridTest/Column/Formatter/ImageTest.php', 'ZfcDatagridTest\Column\Formatter\LinkTest' => __DIR__ . '/tests/ZfcDatagridTest/Column/Formatter/LinkTest.php', + 'ZfcDatagridTest\Column\Formatter\GenerateLinkTest' => __DIR__ . '/tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php', 'ZfcDatagridTest\Column\SelectTest' => __DIR__ . '/tests/ZfcDatagridTest/Column/SelectTest.php', 'ZfcDatagridTest\Column\StandardTest' => __DIR__ . '/tests/ZfcDatagridTest/Column/StandardTest.php', 'ZfcDatagridTest\Column\Style\AbstractStyleTest' => __DIR__ . '/tests/ZfcDatagridTest/Column/Style/AbstractStyleTest.php', diff --git a/composer.json b/composer.json index 6f8bc90d..df162244 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ }, "require-dev": { - "phpunit/PHPUnit": "~3.7", + "phpunit/PHPUnit": "~4.0", "satooshi/php-coveralls": "dev-master", "fabpot/php-cs-fixer": "*@dev", diff --git a/src/ZfcDatagrid/Column/Formatter/GenerateLink.php b/src/ZfcDatagrid/Column/Formatter/GenerateLink.php new file mode 100644 index 00000000..c66b33d6 --- /dev/null +++ b/src/ZfcDatagrid/Column/Formatter/GenerateLink.php @@ -0,0 +1,154 @@ +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('%s', $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; + } + + +} \ No newline at end of file diff --git a/tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php b/tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php new file mode 100644 index 00000000..2c5184df --- /dev/null +++ b/tests/ZfcDatagridTest/Column/Formatter/GenerateLinkTest.php @@ -0,0 +1,50 @@ +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('bar', $generateLink->getFormattedValue($col)); + } + +}