Share Pastebin
Guest
Public paste!

Ignace Knops

By: a guest | Jun 6th, 2009 | Syntax: PHP | Size: 9.94 KB | Hits: 9 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php
  2.  
  3. /**
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  *
  17.  * @author Ignace Knops <developer.ignace@gmail.com>
  18.  * @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
  19.  */
  20. class ContentNegotiation
  21. {
  22.     /**
  23.      *
  24.      * @var <array>
  25.      */
  26.     protected $_mimeTypes = array();
  27.  
  28.     /**
  29.      *
  30.      * @var <array>
  31.      */
  32.     protected $_languages = array();
  33.  
  34.     /**
  35.      *
  36.      * @var <array>
  37.      */
  38.     protected $_encodings = array();
  39.  
  40.     /**
  41.      *
  42.      * @var <array>
  43.      */
  44.     protected $_charsets = array();
  45.  
  46.     /**
  47.      * Class constructor
  48.      */
  49.     public function __construct() {
  50.         $this->_parseAcceptHeader();
  51.         $this->_parseAcceptLanguageHeader();
  52.         $this->_parseAcceptEncodingHeader();
  53.         $this->_parseAcceptCharsetHeader();
  54.     }
  55.  
  56.     /**
  57.      *
  58.      * @return <array>
  59.      */
  60.     public function getMimeTypes() {
  61.         return $this->_mimeTypes;
  62.     }
  63.  
  64.     /**
  65.      *
  66.      * @return <array>
  67.      */
  68.     public function getLanguages() {
  69.         return $this->_languages;
  70.     }
  71.  
  72.     /**
  73.      *
  74.      * @return <array>
  75.      */
  76.     public function getEncodings() {
  77.         return $this->_encodings;
  78.     }
  79.  
  80.     /**
  81.      *
  82.      * @return <array>
  83.      */
  84.     public function getCharsets() {
  85.         return $this->_charsets;
  86.     }
  87.  
  88.     /**
  89.      * Returns the optimal mime-type
  90.      *
  91.      * @param <string> $mimeTypesString
  92.      * @return <string>
  93.      */
  94.     public function returnOptimalMimeType($mimeTypesString) {
  95.         return $this->_returnOptimal($mimeTypesString, $this->getMimeTypes());
  96.     }
  97.  
  98.     /**
  99.      * Returns the optimal language
  100.      *
  101.      * @param <string> $languagesString
  102.      * @return <string>
  103.      */
  104.     public function returnOptimalLanguage($languagesString) {
  105.         return $this->_returnOptimal($languagesString, $this->getLanguages());
  106.     }
  107.  
  108.     /**
  109.      * Returns the optimal encoding
  110.      *
  111.      * @param <string> $encodingsString
  112.      * @return <string>
  113.      */
  114.     public function returnOptimalEncoding($encodingsString) {
  115.         return $this->_returnOptimal($encodingsString, $this->getEncodings());
  116.     }
  117.  
  118.     /**
  119.      * Returns the optimal charset
  120.      *
  121.      * @param <string> $charsetsString
  122.      * @return <string>
  123.      */
  124.     public function returnOptimalCharset($charsetsString) {
  125.         return $this->_returnOptimal($charsetsString, $this->getCharsets());
  126.     }
  127.  
  128.     /**
  129.      * Returns the item with the best quality value
  130.      *
  131.      * @param <string> $str
  132.      * @param <array> $array
  133.      * @return <string>
  134.      */
  135.     protected function _returnOptimal($str, $array) {
  136.         $optimalItem = '';
  137.         foreach (explode(',', $str) as $item) {
  138.             $item = trim($item);
  139.             if (empty($optimalItem)) {
  140.                 $optimalItem = $item;
  141.                 continue;
  142.             }
  143.             if ($this->_compareQuality($item, $optimalItem, $array) < 0) {
  144.                 $optimalItem = $item;
  145.             }
  146.         }
  147.         return $optimalItem;
  148.     }
  149.  
  150.     /**
  151.      * Returns -1 if $mt1 is greater, 0 if they are equal and 1 if $mt2 is greater
  152.      *
  153.      * @param <string> $mt1
  154.      * @param <string> $mt2
  155.      */
  156.     public function compareMimeTypeQuality($mt1, $mt2) {
  157.         return $this->_compareQuality($mt1, $mt2, $this->getMimeTypes());
  158.     }
  159.  
  160.     /**
  161.      * Returns -1 if $mt1 is greater, 0 if they are equal and 1 if $mt2 is greater
  162.      *
  163.      * @param <string> $lang1
  164.      * @param <string> $lang2
  165.      */
  166.     public function compareLanguageQuality($lang1, $lang2) {
  167.         return $this->_compareQuality($lang1, $lang2, $this->getLanguages());
  168.     }
  169.  
  170.     /**
  171.      * Returns -1 if $mt1 is greater, 0 if they are equal and 1 if $mt2 is greater
  172.      *
  173.      * @param <string> $enc1
  174.      * @param <string> $enc2
  175.      */
  176.     public function compareEncodingQuality($enc1, $enc2) {
  177.         return $this->_compareQuality($enc1, $enc2, $this->getEncodings());
  178.     }
  179.  
  180.     /**
  181.      * Returns -1 if $mt1 is greater, 0 if they are equal and 1 if $mt2 is greater
  182.      *
  183.      * @param <string> $char1
  184.      * @param <string> $char2
  185.      */
  186.     public function compareCharsetQuality($char1, $char2) {
  187.         return $this->_compareQuality($char1, $char2, $this->getCharsets());
  188.     }
  189.  
  190.     /**
  191.      *
  192.      * @param <string> $i1
  193.      * @param <string> $i2
  194.      * @param <array> $array
  195.      * @return <int>
  196.      */
  197.     protected function _compareQuality($i1, $i2, $array) {
  198.         $q1 = $this->_getQuality($i1, $array);
  199.         $q2 = $this->_qetQuality($i2, $array);
  200.         return $this->_compare($q1, $q2);
  201.     }
  202.  
  203.     /**
  204.      * Compares and returns results in a unified format
  205.      *
  206.      * @param <int> $qualityValue1
  207.      * @param <int> $qualityValue2
  208.      * @return <int>
  209.      */
  210.     protected function _compare($qualityValue1, $qualityValue2) {
  211.         if ($qualityValue1 > $qualityValue2) {
  212.             return -1;
  213.         } else if ($qualityValue2 > $qualityValue1) {
  214.             return 1;
  215.         } else {
  216.             return 0;
  217.         }
  218.     }
  219.  
  220.     /**
  221.      * Returns the mime-type quality
  222.      *
  223.      * @param <string> $mimeType
  224.      * @return <int>
  225.      */
  226.     protected function _getMimeTypeQuality($mimeType) {
  227.         return $this->_getQuality($mimeType, $this->getMimeTypes());
  228.     }
  229.  
  230.     /**
  231.      * Returns the language quality
  232.      *
  233.      * @param <string> $language
  234.      * @return <int>
  235.      */
  236.     protected function _getLanguageQuality($language) {
  237.         return $this->_getQuality($language, $this->getLanguages());
  238.     }
  239.  
  240.     /**
  241.      * Returns the encoding quality
  242.      *
  243.      * @param <string> $encoding
  244.      * @return <int>
  245.      */
  246.     protected function _getEncodingQuality($encoding) {
  247.         return $this->_getQuality($encoding, $this->getEncodings());
  248.     }
  249.  
  250.     /**
  251.      * Returns the charset quality
  252.      *
  253.      * @param <string> $charset
  254.      * @return <int>
  255.      */
  256.     protected function _getCharsetQuality($charset) {
  257.         return $this->_getQuality($charset, $this->getCharsets());
  258.     }
  259.  
  260.     /**
  261.      * Retrieves the quality from $search and returns $default otherwise
  262.      *
  263.      * @param <string> $item
  264.      * @param <array> $search
  265.      * @param <mixed> $default
  266.      * @return <int>
  267.      */
  268.     protected function _getQuality($item, $search, $default = 0) {
  269.         $item = strtoupper($item);
  270.         return ((null !== @$search[$item]) ? $search[$item] : $default);
  271.     }
  272.  
  273.     /**
  274.      * Parses the HTTP_ACCEPT header and merges it with $this->_mimeTypes
  275.      */
  276.     protected function _parseAcceptHeader() {
  277.         $this->_mergeMimeTypes($this->_parse($_SERVER['HTTP_ACCEPT']));
  278.     }
  279.  
  280.     /**
  281.      * Parses the HTTP_ACCEPT_LANGUAGE header and merges it with $this->_languages
  282.      */
  283.     protected function _parseAcceptLanguageHeader() {
  284.         $this->_mergeLanguages($this->_parse($_SERVER['HTTP_ACCEPT_LANGUAGE']));
  285.     }
  286.  
  287.     /**
  288.      * Parses the HTTP_ACCEPT_ENCODING header and merges it with $this->_encodings
  289.      */
  290.     protected function _parseAcceptEncodingHeader() {
  291.         $this->_mergeEncodings($this->_parse($_SERVER['HTTP_ACCEPT_ENCODING']));
  292.     }
  293.  
  294.     /**
  295.      * Parses the HTTP_ACCEPT_CHARSET header and merges it with $this->_charsets
  296.      */
  297.     protected function _parseAcceptCharsetHeader() {
  298.         $this->_mergeCharsets($this->_parse($_SERVER['HTTP_ACCEPT_CHARSET']));
  299.     }
  300.  
  301.     /**
  302.      * Performs the actual parsing of accept headers
  303.      *
  304.      * @param <string> $str
  305.      * @return <array>
  306.      */
  307.     protected function _parse($str) {
  308.         $mimeTypesQuality = array();
  309.         foreach (explode(',', $str) as $mimeType) {
  310.             $mimeType = trim($mimeType);
  311.             if (strpos($mimeType, ';')) {
  312.                 list($mimeType, $qualityValue) = explode(';', $mimeType);
  313.                 list(, $qualityValue) = explode('=', $qualityValue);
  314.             } else {
  315.                 $qualityValue = 1;
  316.             }
  317.             if (strpos($mimeType, '*')) {
  318.                 continue;
  319.             }
  320.             $mimeTypesQuality[strtoupper($mimeType)] = $qualityValue;
  321.         }
  322.         return $mimeTypesQuality;
  323.     }
  324.  
  325.     /**
  326.      * Merges $mimeTypes with $this->_mimeTypes
  327.      *
  328.      * @param <array> $mimeTypes
  329.      */
  330.     protected function _mergeMimeTypes($mimeTypes) {
  331.         $this->_mimeTypes = $this->_merge($this->getMimeTypes(), $mimeTypes);
  332.     }
  333.  
  334.     /**
  335.      * Merges $languages with $this->_languages
  336.      *
  337.      * @param <array> $languages
  338.      */
  339.     protected function _mergeLanguages($languages) {
  340.         $this->_languages = $this->_merge($this->getLanguages(), $languages);
  341.     }
  342.  
  343.     /**
  344.      * Merges $encodings with $this->_encodings
  345.      *
  346.      * @param <array> $encodings
  347.      */
  348.     protected function _mergeEncodings($encodings) {
  349.         $this->_encodings = $this->_merge($this->getEncodings(), $encodings);
  350.     }
  351.  
  352.     /**
  353.      * Merges $charsets with $this->_charsets
  354.      *
  355.      * @param <array> $charsets
  356.      */
  357.     protected function _mergeCharsets($charsets) {
  358.         $this->_charsets = $this->_merge($this->getCharsets(), $charsets);
  359.     }
  360.  
  361.     /**
  362.      * Merges two arrays
  363.      *
  364.      * @param <array> $array1
  365.      * @param <array> $array2
  366.      * @return <array>
  367.      */
  368.     protected function _merge($array1, $array2) {
  369.         return array_merge($array1, $array2);
  370.     }
  371. }