CampURI::QueryArrayToString PHP Method

QueryArrayToString() protected static method

Builds a URI query string from the given query array.
protected static QueryArrayToString ( array $p_queryArray, $p_HTMLEscape = true ) : string
$p_queryArray array An array of query variables
return string $queryString The generated query string
    protected static function QueryArrayToString(array $p_queryArray, $p_HTMLEscape = true)
    {
        if (!is_array($p_queryArray) || sizeof($p_queryArray) < 1) {
            return false;
        }
        $queryString = '';
        $queryVars = array();
        foreach ($p_queryArray as $var => $value) {
            if (is_array($value)) {
                foreach ($value as $item) {
                    $queryVars[] = $var . '[]=' . urlencode($item);
                }
            } else {
                $queryVars[] = $var . '=' . urlencode($value);
            }
        }
        $separator = $p_HTMLEscape ? '&amp;' : '&';
        $queryString = implode($separator, $queryVars);
        return $queryString;
    }

Usage Example

 /**
  * Sets the URI path and query values based on given parameters.
  *
  * @param array   $p_params
  *                           An array of valid URL parameters
  * @param boolean $p_preview
  *                           If true, will keep the preview parameters in the URL
  *
  * @return void
  */
 protected function buildURI(array &$p_params = array(), $p_preview = false)
 {
     if ($this->isValidCache()) {
         return;
     }
     $parameter = count($p_params) > 0 ? strtolower(array_shift($p_params)) : null;
     switch ($parameter) {
         case 'language':
         case 'publication':
             $this->m_buildPath = $this->getURILanguage();
             if ($p_preview) {
                 $this->m_buildQueryArray = $this->getQueryArray(CampURI::$m_previewParameters);
             } else {
                 $this->m_buildQueryArray = array();
             }
             $p_params = array();
             break;
         case 'issue':
             $this->m_buildPath = $this->getURIIssue();
             if ($p_preview) {
                 $this->m_buildQueryArray = $this->getQueryArray(CampURI::$m_previewParameters);
             } else {
                 $this->m_buildQueryArray = array();
             }
             $p_params = array();
             break;
         case 'section':
             $this->m_buildPath = $this->getURISection();
             if ($p_preview) {
                 $this->m_buildQueryArray = $this->getQueryArray(CampURI::$m_previewParameters);
             } else {
                 $this->m_buildQueryArray = array();
             }
             $p_params = array();
             break;
         case 'article':
             $this->m_buildPath = $this->getURIArticle();
             if ($p_preview) {
                 $this->m_buildQueryArray = $this->getQueryArray(CampURI::$m_previewParameters);
             } else {
                 $this->m_buildQueryArray = array();
             }
             $p_params = array();
             break;
         case 'template':
         case 'id':
             $option = isset($p_params[0]) ? array_shift($p_params) : null;
             if (is_null($option)) {
                 break;
             }
             if (is_null($this->_themePath)) {
                 $themesService = \Zend_Registry::get('container')->getService('newscoop_newscoop.themes_service');
                 $this->_themePath = $themesService->getThemePath();
             }
             $pathRsc = new Resource();
             $pathRsc->setName('buildPage');
             $pathRsc->setPath($this->_themePath . $option);
             $resourceId = new ResourceId('template_engine/classes/CampURIShortNames');
             $pathRsc = $resourceId->getService(ISyncResourceService::NAME)->getSynchronized($pathRsc);
             if (!is_null($pathRsc) && $pathRsc->exists()) {
                 $this->m_buildQueryArray[CampRequest::TEMPLATE_ID] = $pathRsc->getId();
             }
             break;
         default:
             if (!empty($parameter)) {
                 array_unshift($p_params, $parameter);
                 $count = count($p_params);
                 parent::buildURI($p_params, $p_preview);
                 if (count($p_params) == $count) {
                     array_shift($p_params);
                 }
             }
     }
     if (count($p_params) > 0) {
         $this->buildURI($p_params);
     }
     if (!is_null($this->m_language) && $this->m_language->defined() && is_null($this->m_buildPath)) {
         $this->m_buildPath = '/' . $this->m_language->code . '/';
         if (!is_null($this->m_issue) && $this->m_issue->defined()) {
             $this->m_buildPath .= $this->m_issue->url_name . '/';
             if (!is_null($this->m_section) && $this->m_section->defined()) {
                 $this->m_buildPath .= $this->m_section->url_name . '/';
                 if (!is_null($this->m_article) && $this->m_article->defined()) {
                     $this->m_buildPath = $this->getURIArticle();
                 }
             }
         }
     }
     if (is_null($this->m_buildQuery)) {
         $this->m_buildQuery = CampURI::QueryArrayToString($this->m_buildQueryArray);
     }
     $this->validateCache(true);
 }
All Usage Examples Of CampURI::QueryArrayToString