Skip to content

Instantly share code, notes, and snippets.

@skrosoft
Created November 21, 2018 05:26
Show Gist options
  • Save skrosoft/aa66f3747f333ab21a504483f966720c to your computer and use it in GitHub Desktop.
Save skrosoft/aa66f3747f333ab21a504483f966720c to your computer and use it in GitHub Desktop.
Symfony service that allow you to generate an URI string of ezplatform view parameters
<?php
/**
* Created by PhpStorm.
* User: vguyard
* Date: 11/21/18
* Time: 1:11 AM
*/
namespace AppBundle\Service;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class ViewParametersGenerator
{
/** @var \Symfony\Component\HttpFoundation\RequestStack */
protected $requestStack;
/**
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
*/
public function __construct(
RequestStack $requestStack
) {
$this->requestStack = $requestStack;
}
/**
* @return string the view parameters url string
*/
public function getFromCurrentRequest(){
return $this->getFromRequest($this->requestStack->getCurrentRequest());
}
/**
* @param Request $request
* @return string the view parameters url string
*/
public function getFromRequest(Request $request){
return $this->generate($request->get('viewParameters', []));
}
/**
* @param array $attributes
* @return string
*/
public function generate(array $attributes){
$dataAttributes = array_map(function($value, $key) {
return sprintf('/(%s)/%s', $key, $value);
}, array_values($attributes), array_keys($attributes));
return implode('', $dataAttributes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment