Posted by Ignace Knops on Sat 6 Jun 11:42
report abuse | View followups from Ignace Knops | download | new post
- <?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>
- */
- /**
- *
- * @var <array>
- */
- /**
- *
- * @var <array>
- */
- /**
- *
- * @var <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 = '';
- $optimalItem = $item;
- continue;
- }
- $q1 = $this->_getQuality($item, $array);
- $q2 = $this->_getQuality($optimalItem, $array);
- if ($this->_compare($q1, $q2) < 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) {
- $q1 = $this->_getMimeTypeQuality($mt1);
- $q2 = $this->_getMimeTypeQuality($mt2);
- return $this->_compare($q1, $q2);
- }
- /**
- * 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) {
- $q1 = $this->_getLanguageQuality($lang1);
- $q2 = $this->_getLanguageQuality($lang2);
- return $this->_compare($q1, $q2);
- }
- /**
- * 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) {
- $q1 = $this->_getEncodingQuality($enc1);
- $q2 = $this->_getEncodingQuality($enc2);
- return $this->_compare($q1, $q2);
- }
- /**
- * 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) {
- $q1 = $this->_getCharsetQuality($char1);
- $q2 = $this->_getCharsetQuality($char2);
- 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) {
- }
- /**
- * 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) {
- foreach ($mimeTypes as $mimeType) {
- } else {
- $qualityValue = 1;
- }
- continue;
- }
- }
- 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) {
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.