src/Cms/CoreBundle/Util/ModalsController.php line 27

Open in your IDE?
  1. <?php
  2. namespace Cms\CoreBundle\Util;
  3. /**
  4.  * Class ModalsController
  5.  * @package Cms\CoreBundle\Util
  6.  */
  7. abstract class ModalsController extends Controller
  8. {
  9.     const DEFAULTS__PARAMS = [];
  10.     /**
  11.      * @var array
  12.      */
  13.     protected $modalParams null;
  14.     /**
  15.      * @throws \Exception
  16.      */
  17.     private function parseModalParams()
  18.     {
  19.         // process if not already processed and cached
  20.         if ($this->modalParams === null) {
  21.             // get the request
  22.             $request $this->getRequest();
  23.             // try to get params from the header
  24.             $header $request->headers->get('X-CAMPUSSUITE-MODALS-PARAMS');
  25.             if (empty($header)) {
  26.                 throw new \Exception();
  27.             }
  28.             // this should be a json encoded string, decode it, also uri encoded
  29.             $decoded json_decode(urldecode($header), true);
  30.             if ( ! is_array($decoded)) {
  31.                 throw new \Exception();
  32.             }
  33.             // merge in the defaults
  34.             $decoded array_merge(static::DEFAULTS__PARAMS$decoded);
  35.             // set to cache
  36.             $this->modalParams $decoded;
  37.         }
  38.     }
  39.     /**
  40.      * @return array
  41.      */
  42.     protected function getModalParams()
  43.     {
  44.         // ensure that modal params have been parsed
  45.         $this->parseModalParams();
  46.         // return the whole array
  47.         return $this->modalParams;
  48.     }
  49.     /**
  50.      * @param string $key
  51.      * @return mixed
  52.      */
  53.     protected function getModalParam($key)
  54.     {
  55.         // ensure that modal params have been parsed
  56.         $this->parseModalParams();
  57.         // see if we have this, return null if not
  58.         if ( ! $this->hasModalParam($key)) {
  59.             return null;
  60.         }
  61.         // we do have it, just return it from the array
  62.         return $this->modalParams[$key];
  63.     }
  64.     /**
  65.      * @param string $key
  66.      * @return bool
  67.      */
  68.     protected function hasModalParam($key)
  69.     {
  70.         // ensure that modal params have been parsed
  71.         $this->parseModalParams();
  72.         // return whether we are set or not
  73.         return (array_key_exists($key$this->modalParams) === true);
  74.     }
  75. }