<?php
namespace Cms\CoreBundle\Util;
/**
* Class ModalsController
* @package Cms\CoreBundle\Util
*/
abstract class ModalsController extends Controller
{
const DEFAULTS__PARAMS = [];
/**
* @var array
*/
protected $modalParams = null;
/**
* @throws \Exception
*/
private function parseModalParams()
{
// process if not already processed and cached
if ($this->modalParams === null) {
// get the request
$request = $this->getRequest();
// try to get params from the header
$header = $request->headers->get('X-CAMPUSSUITE-MODALS-PARAMS');
if (empty($header)) {
throw new \Exception();
}
// this should be a json encoded string, decode it, also uri encoded
$decoded = json_decode(urldecode($header), true);
if ( ! is_array($decoded)) {
throw new \Exception();
}
// merge in the defaults
$decoded = array_merge(static::DEFAULTS__PARAMS, $decoded);
// set to cache
$this->modalParams = $decoded;
}
}
/**
* @return array
*/
protected function getModalParams()
{
// ensure that modal params have been parsed
$this->parseModalParams();
// return the whole array
return $this->modalParams;
}
/**
* @param string $key
* @return mixed
*/
protected function getModalParam($key)
{
// ensure that modal params have been parsed
$this->parseModalParams();
// see if we have this, return null if not
if ( ! $this->hasModalParam($key)) {
return null;
}
// we do have it, just return it from the array
return $this->modalParams[$key];
}
/**
* @param string $key
* @return bool
*/
protected function hasModalParam($key)
{
// ensure that modal params have been parsed
$this->parseModalParams();
// return whether we are set or not
return (array_key_exists($key, $this->modalParams) === true);
}
}