-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Helper] Add static map styles support (#239)
- Loading branch information
Showing
8 changed files
with
241 additions
and
0 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
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,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ivory Google Map package. | ||
* | ||
* (c) Eric GELOEN <geloen.eric@gmail.com> | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ivory\GoogleMap\Helper\Renderer\Image; | ||
|
||
/** | ||
* @author GeLo <geloen.eric@gmail.com> | ||
*/ | ||
class StyleRenderer | ||
{ | ||
/** | ||
* @param mixed[] $style | ||
* | ||
* @return string | ||
*/ | ||
public function render(array $style) | ||
{ | ||
$result = []; | ||
|
||
if (isset($style['feature'])) { | ||
$result[] = $this->renderStyle('feature', $style['feature']); | ||
} | ||
|
||
if (isset($style['element'])) { | ||
$result[] = $this->renderStyle('element', $style['element']); | ||
} | ||
|
||
foreach ($style['rules'] as $rule => $value) { | ||
$result[] = $this->renderStyle($rule, $value); | ||
} | ||
|
||
return implode('|', $result); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param string $value | ||
* | ||
* @return string | ||
*/ | ||
private function renderStyle($name, $value) | ||
{ | ||
return $name.':'.$value; | ||
} | ||
} |
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,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ivory Google Map package. | ||
* | ||
* (c) Eric GELOEN <geloen.eric@gmail.com> | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ivory\GoogleMap\Helper\Subscriber\Image; | ||
|
||
use Ivory\GoogleMap\Helper\Event\StaticMapEvent; | ||
use Ivory\GoogleMap\Helper\Event\StaticMapEvents; | ||
use Ivory\GoogleMap\Helper\Renderer\Image\StyleRenderer; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* @author GeLo <geloen.eric@gmail.com> | ||
*/ | ||
class StyleSubscriber implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var StyleRenderer | ||
*/ | ||
private $styleRenderer; | ||
|
||
/** | ||
* @param StyleRenderer $styleRenderer | ||
*/ | ||
public function __construct(StyleRenderer $styleRenderer) | ||
{ | ||
$this->styleRenderer = $styleRenderer; | ||
} | ||
|
||
/** | ||
* @param StaticMapEvent $event | ||
*/ | ||
public function handleMap(StaticMapEvent $event) | ||
{ | ||
$map = $event->getMap(); | ||
|
||
if (!$map->hasMapOption('styles')) { | ||
return; | ||
} | ||
|
||
$styles = []; | ||
|
||
foreach ($map->getStaticOption('styles') as $style) { | ||
$styles[] = $this->styleRenderer->render($style); | ||
} | ||
|
||
if (!empty($styles)) { | ||
$event->setParameter('style', $styles); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [StaticMapEvents::STYLE => 'handleMap']; | ||
} | ||
} |
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,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ivory Google Map package. | ||
* | ||
* (c) Eric GELOEN <geloen.eric@gmail.com> | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ivory\Tests\GoogleMap\Helper\Renderer\Image; | ||
|
||
use Ivory\GoogleMap\Helper\Renderer\Image\StyleRenderer; | ||
|
||
/** | ||
* @author GeLo <geloen.eric@gmail.com> | ||
*/ | ||
class StyleRendererTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var StyleRenderer | ||
*/ | ||
private $styleRenderer; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->styleRenderer = new StyleRenderer(); | ||
} | ||
|
||
/** | ||
* @param string $expected | ||
* @param mixed[] $style | ||
* | ||
* @dataProvider renderProvider | ||
*/ | ||
public function testRender($expected, array $style) | ||
{ | ||
$this->assertSame($expected, $this->styleRenderer->render($style)); | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function renderProvider() | ||
{ | ||
return [ | ||
['color:0x00ff00', ['rules' => ['color' => '0x00ff00']]], | ||
['color:0x00ff00|lightness:50', ['rules' => ['color' => '0x00ff00', 'lightness' => '50']]], | ||
['feature:road|color:0x00ff00|lightness:50', [ | ||
'feature' => 'road', | ||
'rules' => ['color' => '0x00ff00', 'lightness' => '50'], | ||
]], | ||
['element:geometry|color:0x00ff00|lightness:50', [ | ||
'element' => 'geometry', | ||
'rules' => ['color' => '0x00ff00', 'lightness' => '50'], | ||
]], | ||
['feature:road.local|element:geometry|color:0x00ff00|lightness:50', [ | ||
'feature' => 'road.local', | ||
'element' => 'geometry', | ||
'rules' => ['color' => '0x00ff00', 'lightness' => '50'], | ||
]], | ||
]; | ||
} | ||
} |