<?php
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Ignace Knops <developer.ignace@gmail.com>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
*/
class ContentNegotiation
{
/**
*
* @var <array>
*/
protected
$_mimeTypes = array();
/**
*
* @var <array>
*/
protected
$_languages = array();
/**
*
* @var <array>
*/
protected
$_encodings = array();
/**
*
* @var <array>
*/
protected
$_charsets = array();
/**
* Class constructor
*/
public function __construct() {
$this->_parseAcceptHeader();
$this->_parseAcceptLanguageHeader();
$this->_parseAcceptEncodingHeader();
$this->_parseAcceptCharsetHeader();
}
/**
*
* @return <array>
*/
public function getMimeTypes() {
return $this->_mimeTypes;
}
/**
*
* @return <array>
*/
public function getLanguages() {
return $this->_languages;
}
/**
*
* @return <array>
*/
public function getEncodings() {
return $this->_encodings;
}
/**
*
* @return <array>
*/
public function getCharsets() {
return $this->_charsets;
}
/**
* Returns the optimal mime-type
*
* @param <string> $mimeTypesString
* @return <string>
*/
public function returnOptimalMimeType($mimeTypesString) {
return $this->_returnOptimal($mimeTypesString, $this->getMimeTypes());
}
/**
* Returns the optimal language
*
* @param <string> $languagesString
* @return <string>
*/
public function returnOptimalLanguage($languagesString) {
return $this->_returnOptimal($languagesString, $this->getLanguages());
}
/**
* Returns the optimal encoding
*
* @param <string> $encodingsString
* @return <string>
*/
public function returnOptimalEncoding($encodingsString) {
return $this->_returnOptimal($encodingsString, $this->getEncodings());
}
/**
* Returns the optimal charset
*
* @param <string> $charsetsString
* @return <string>
*/
public function returnOptimalCharset($charsetsString) {
return $this->_returnOptimal($charsetsString, $this->getCharsets());
}
/**
* Returns the item with the best quality value
*
* @param <string> $str
* @param <array> $array
* @return <string>
*/
protected function _returnOptimal($str, $array) {
$optimalItem = '';
foreach (explode(',', $str) as $item) {
if (empty($optimalItem)) {
$optimalItem = $item;
continue;
}
if ($this->_compareQuality($item, $optimalItem, $array) < 0) {
$optimalItem = $item;
}
}
return $optimalItem;
}
/**
* Returns -1 if $mt1 is greater, 0 if they are equal and 1 if $mt2 is greater
*
* @param <string> $mt1
* @param <string> $mt2
*/
public function compareMimeTypeQuality($mt1, $mt2) {
return $this->_compareQuality($mt1, $mt2, $this->getMimeTypes());
}
/**
* Returns -1 if $mt1 is greater, 0 if they are equal and 1 if $mt2 is greater
*
* @param <string> $lang1
* @param <string> $lang2
*/
public function compareLanguageQuality($lang1, $lang2) {
return $this->_compareQuality($lang1, $lang2, $this->getLanguages());
}
/**
* Returns -1 if $mt1 is greater, 0 if they are equal and 1 if $mt2 is greater
*
* @param <string> $enc1
* @param <string> $enc2
*/
public function compareEncodingQuality($enc1, $enc2) {
return $this->_compareQuality($enc1, $enc2, $this->getEncodings());
}
/**
* Returns -1 if $mt1 is greater, 0 if they are equal and 1 if $mt2 is greater
*
* @param <string> $char1
* @param <string> $char2
*/
public function compareCharsetQuality($char1, $char2) {
return $this->_compareQuality($char1, $char2, $this->getCharsets());
}
/**
*
* @param <string> $i1
* @param <string> $i2
* @param <array> $array
* @return <int>
*/
protected function _compareQuality($i1, $i2, $array) {
$q1 = $this->_getQuality($i1, $array);
$q2 = $this->_qetQuality($i2, $array);
return $this->_compare($q1, $q2);
}
/**
* Compares and returns results in a unified format
*
* @param <int> $qualityValue1
* @param <int> $qualityValue2
* @return <int>
*/
protected function _compare($qualityValue1, $qualityValue2) {
if ($qualityValue1 > $qualityValue2) {
return -1;
} else if ($qualityValue2 > $qualityValue1) {
return 1;
} else {
return 0;
}
}
/**
* Returns the mime-type quality
*
* @param <string> $mimeType
* @return <int>
*/
protected function _getMimeTypeQuality($mimeType) {
return $this->_getQuality($mimeType, $this->getMimeTypes());
}
/**
* Returns the language quality
*
* @param <string> $language
* @return <int>
*/
protected function _getLanguageQuality($language) {
return $this->_getQuality($language, $this->getLanguages());
}
/**
* Returns the encoding quality
*
* @param <string> $encoding
* @return <int>
*/
protected function _getEncodingQuality($encoding) {
return $this->_getQuality($encoding, $this->getEncodings());
}
/**
* Returns the charset quality
*
* @param <string> $charset
* @return <int>
*/
protected function _getCharsetQuality($charset) {
return $this->_getQuality($charset, $this->getCharsets());
}
/**
* Retrieves the quality from $search and returns $default otherwise
*
* @param <string> $item
* @param <array> $search
* @param <mixed> $default
* @return <int>
*/
protected function _getQuality($item, $search, $default = 0) {
return ((null !== @$search[$item]) ? $search[$item] : $default);
}
/**
* Parses the HTTP_ACCEPT header and merges it with $this->_mimeTypes
*/
protected function _parseAcceptHeader() {
$this->_mergeMimeTypes($this->_parse($_SERVER['HTTP_ACCEPT']));
}
/**
* Parses the HTTP_ACCEPT_LANGUAGE header and merges it with $this->_languages
*/
protected function _parseAcceptLanguageHeader() {
$this->_mergeLanguages($this->_parse($_SERVER['HTTP_ACCEPT_LANGUAGE']));
}
/**
* Parses the HTTP_ACCEPT_ENCODING header and merges it with $this->_encodings
*/
protected function _parseAcceptEncodingHeader() {
$this->_mergeEncodings($this->_parse($_SERVER['HTTP_ACCEPT_ENCODING']));
}
/**
* Parses the HTTP_ACCEPT_CHARSET header and merges it with $this->_charsets
*/
protected function _parseAcceptCharsetHeader() {
$this->_mergeCharsets($this->_parse($_SERVER['HTTP_ACCEPT_CHARSET']));
}
/**
* Performs the actual parsing of accept headers
*
* @param <string> $str
* @return <array>
*/
protected function _parse($str) {
$mimeTypesQuality = array();
foreach (explode(',', $str) as $mimeType) {
$mimeType = trim($mimeType);
list($mimeType, $qualityValue) = explode(';', $mimeType);
} else {
$qualityValue = 1;
}
continue;
}
$mimeTypesQuality[strtoupper($mimeType)] = $qualityValue;
}
return $mimeTypesQuality;
}
/**
* Merges $mimeTypes with $this->_mimeTypes
*
* @param <array> $mimeTypes
*/
protected function _mergeMimeTypes($mimeTypes) {
$this->_mimeTypes = $this->_merge($this->getMimeTypes(), $mimeTypes);
}
/**
* Merges $languages with $this->_languages
*
* @param <array> $languages
*/
protected function _mergeLanguages($languages) {
$this->_languages = $this->_merge($this->getLanguages(), $languages);
}
/**
* Merges $encodings with $this->_encodings
*
* @param <array> $encodings
*/
protected function _mergeEncodings($encodings) {
$this->_encodings = $this->_merge($this->getEncodings(), $encodings);
}
/**
* Merges $charsets with $this->_charsets
*
* @param <array> $charsets
*/
protected function _mergeCharsets($charsets) {
$this->_charsets = $this->_merge($this->getCharsets(), $charsets);
}
/**
* Merges two arrays
*
* @param <array> $array1
* @param <array> $array2
* @return <array>
*/
protected function _merge($array1, $array2) {
}
}