/** * Pomůcka pro sestavení adresy s GET parametry. * @param array $params Asociativní pole [název-parametru] => hodnota-parametru. Null pro odstranění daného parametru, byl-li tam. * Hodnota může být array, pak se automaticky převede na několik samostatných parametrů ve tvaru název-parametru[] * @param string $originalUrl Původní adresa * @param bool $clear True = všechny GET parametry z $original URL vyhodit a pouze přidávat ty z $params * @param bool $clearArrays Jak se má chovat, když v $originalUrl je nějaký parametr - pole (s [] na konci) a v $params také. * False = sloučit obě pole. True = smazat to z $originalUrl a nechat jen hodnoty z $params. * @param array $keepEntities Array entit, které se nemají enkódovat (např. %id%) * @return string */ static function builder($params, $originalUrl, $clear = false, $clearArrays = true, $keepEntities = array()) { $origParams = array(); if (!$clear) { $query = parse_url($originalUrl, PHP_URL_QUERY); if ($query) { parse_str($query, $origParams); } if (!$origParams) { $origParams = array(); } } if ($clearArrays) { $finalParams = array_merge($origParams, $params); } else { $finalParams = array_replace_recursive($origParams, $params); } foreach ($finalParams as $i => $r) { if ($r === null) { unset($finalParams[$i]); } } $casti = array(); foreach ($finalParams as $i => $p) { if (is_array($p)) { foreach ($p as $pi => $pp) { $casti[] = $i . "[" . urlencode($pi) . "]=" . urlencode($pp); } } else { $casti[] = $i . "=" . urlencode($p); } } if (Strings::strpos($originalUrl, "?") !== false) { $zaklad = Strings::substr($originalUrl, 0, Strings::strpos($originalUrl, "?")); } else { $zaklad = $originalUrl; } $vrat = $zaklad; if ($casti) { $vrat .= "?" . implode("&", $casti); } if ($keepEntities) { foreach (Arrays::arrayize($keepEntities) as $ent) { $entEncoded = urlencode($ent); $vrat = str_replace($entEncoded, $ent, $vrat); } } return $vrat; }
/** * Validate and normalize an array with attributes. * * This function takes in an associative array with attributes, and parses and validates * this array. On success, it will return a normalized array, where each attribute name * is an index to an array of one or more strings. On failure an exception will be thrown. * This exception will contain an message describing what is wrong. * * @param array $attributes The array containing attributes that we should validate and normalize. * * @return array The normalized attributes array. * @throws \InvalidArgumentException If input is not an array, array keys are not strings or attribute values are * not strings. * * @author Olav Morken, UNINETT AS <*****@*****.**> * @author Jaime Perez, UNINETT AS <*****@*****.**> */ public static function normalizeAttributesArray($attributes) { if (!is_array($attributes)) { throw new \InvalidArgumentException('The attributes array is not an array, it is: ' . print_r($attributes, true) . '".'); } $newAttrs = array(); foreach ($attributes as $name => $values) { if (!is_string($name)) { throw new \InvalidArgumentException('Invalid attribute name: "' . print_r($name, true) . '".'); } $values = Arrays::arrayize($values); foreach ($values as $value) { if (!is_string($value)) { throw new \InvalidArgumentException('Invalid attribute value for attribute ' . $name . ': "' . print_r($value, true) . '".'); } } $newAttrs[$name] = $values; } return $newAttrs; }