Skip to content

Commit

Permalink
[FEATURE] Add default options to configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Neirda24 committed Aug 18, 2019
1 parent c9bb0f4 commit 9d2a019
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->cannotBeEmpty()
->info('Google map Api key')
->end()
->arrayNode('default_options')
->prototype('variable')
->end()
->end()
->end()
->end()
;
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/GoogleMapExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function load(array $configs, ContainerBuilder $container): void
$container
->getDefinition(GoogleExtension::class)
->setArgument('$googleApiKey', $config['api_key'])
->setArgument('$defaultOptions', true === empty($config['default_options']) ? null : $config['default_options'])
;
}
}
31 changes: 16 additions & 15 deletions src/Twig/GoogleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace RocIT\GoogleMapBundle\Twig;

use League\Uri\Uri;
use League\Uri\UriInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use function array_keys;
Expand All @@ -25,14 +26,23 @@ class GoogleExtension extends AbstractExtension
*/
private $googleApiKey;

/**
* @var array|null
*/
private $defaultOptions;

/**
* GoogleExtension constructor.
*
* @param string $googleApiKey
* @param string $googleApiKey
* @param array|null $defaultOptions
*/
public function __construct(string $googleApiKey)
{
$this->googleApiKey = $googleApiKey;
public function __construct(
string $googleApiKey,
?array $defaultOptions = null
) {
$this->googleApiKey = $googleApiKey;
$this->defaultOptions = $defaultOptions;
}

/**
Expand All @@ -59,7 +69,7 @@ public function staticMap(
): string {
static $baseUrl = 'https://maps.googleapis.com/maps/api/staticmap';

$defaultOptions = [
$defaultOptions = $this->defaultOptions ?: [
'maptype' => 'roadmap',
'size' => '512x176',
];
Expand Down Expand Up @@ -100,19 +110,10 @@ public function staticMap(

$url = merge_query(Uri::createFromString($baseUrl), build_query($options));

$url = array_reduce($markers, static function (Uri $url, string $markerConfiguration) {
$url = array_reduce($markers, static function (Uri $url, string $markerConfiguration): UriInterface {
return append_query($url, "markers=${markerConfiguration}");
}, $url);

// $url = merge_query(Uri::createFromString($baseUrl), build_query([
// 'center' => urlencode($center),
// 'maptype' => 'roadmap',
// 'key' => $this->googleApiKey,
// 'size' => '512x176',
// 'markers' => urlencode("color:red|size:tiny|${center}")
// // zoom=13&size=600x300
// ]));

return (string) $url;
}
}

0 comments on commit 9d2a019

Please sign in to comment.