Created
November 21, 2018 05:26
-
-
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
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
<?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