Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: PHP_Compat

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /phpxmlrpc/compat/version_compare.php

Documentation is available at version_compare.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2004 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 3.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/3_0.txt.                                  |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | [email protected] so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Philippe Jausions <[email protected]>          |
  16. // |          Aidan Lister <[email protected]>                                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: version_compare.php,v 1.1 2005/07/11 16:34:36 ggiunta Exp $
  20.  
  21.  
  22. /**
  23.  * Replace version_compare()
  24.  *
  25.  * @category    PHP
  26.  * @package     PHP_Compat
  27.  * @link        http://php.net/function.version_compare
  28.  * @author      Philippe Jausions <[email protected]>
  29.  * @author      Aidan Lister <[email protected]>
  30.  * @version     $Revision: 1.1 $
  31.  * @since       PHP 4.1.0
  32.  * @require     PHP 4.0.0 (user_error)
  33.  */
  34. if (!function_exists('version_compare')) {
  35.     function version_compare($version1, $version2, $operator = '<')
  36.     {
  37.         // Check input
  38.         if (!is_scalar($version1)) {
  39.             user_error('version_compare() expects parameter 1 to be string, ' .
  40.                 gettype($version1) . ' given', E_USER_WARNING);
  41.             return;
  42.         }
  43.  
  44.         if (!is_scalar($version2)) {
  45.             user_error('version_compare() expects parameter 2 to be string, ' .
  46.                 gettype($version2) . ' given', E_USER_WARNING);
  47.             return;
  48.         }
  49.  
  50.         if (!is_scalar($operator)) {
  51.             user_error('version_compare() expects parameter 3 to be string, ' .
  52.                 gettype($operator) . ' given', E_USER_WARNING);
  53.             return;
  54.         }
  55.  
  56.         // Standardise versions
  57.         $v1 = explode('.',
  58.             str_replace('..', '.',
  59.                 preg_replace('/([^0-9\.]+)/', '.$1.',
  60.                     str_replace(array('-', '_', '+'), '.',
  61.                         trim($version1)))));
  62.  
  63.         $v2 = explode('.',
  64.             str_replace('..', '.',
  65.                 preg_replace('/([^0-9\.]+)/', '.$1.',
  66.                     str_replace(array('-', '_', '+'), '.',
  67.                         trim($version2)))));
  68.  
  69.         // Replace empty entries at the start of the array
  70.         while (empty($v1[0]) && array_shift($v1)) {}
  71.         while (empty($v2[0]) && array_shift($v2)) {}
  72.  
  73.         // Release state order
  74.         // '#' stands for any number
  75.         $versions = array(
  76.             'dev'   => 0,
  77.             'alpha' => 1,
  78.             'a'     => 1,
  79.             'beta'  => 2,
  80.             'b'     => 2,
  81.             'RC'    => 3,
  82.             '#'     => 4,
  83.             'p'     => 5,
  84.             'pl'    => 5);
  85.  
  86.         // Loop through each segment in the version string
  87.         $compare = 0;
  88.         for ($i = 0, $x = min(count($v1), count($v2)); $i < $x; $i++) {
  89.             if ($v1[$i] == $v2[$i]) {
  90.                 continue;
  91.             }
  92.             $i1 = $v1[$i];
  93.             $i2 = $v2[$i];
  94.             if (is_numeric($i1) && is_numeric($i2)) {
  95.                 $compare = ($i1 < $i2) ? -1 : 1;
  96.                 break;
  97.             }
  98.             // We use the position of '#' in the versions list
  99.             // for numbers... (so take care of # in original string)
  100.             if ($i1 == '#') {
  101.                 $i1 = '';
  102.             } elseif (is_numeric($i1)) {
  103.                 $i1 = '#';
  104.             }
  105.             if ($i2 == '#') {
  106.                 $i2 = '';
  107.             } elseif (is_numeric($i2)) {
  108.                 $i2 = '#';
  109.             }
  110.             if (isset($versions[$i1]) && isset($versions[$i2])) {
  111.                 $compare = ($versions[$i1] < $versions[$i2]) ? -1 : 1;
  112.             } elseif (isset($versions[$i1])) {
  113.                 $compare = 1;
  114.             } elseif (isset($versions[$i2])) {
  115.                 $compare = -1;
  116.             } else {
  117.                 $compare = 0;
  118.             }
  119.  
  120.             break;
  121.         }
  122.  
  123.         // If previous loop didn't find anything, compare the "extra" segments
  124.         if ($compare == 0) {
  125.             if (count($v2) > count($v1)) {
  126.                 if (isset($versions[$v2[$i]])) {
  127.                     $compare = ($versions[$v2[$i]] < 4) ? 1 : -1;
  128.                 } else {
  129.                     $compare = -1;
  130.                 }
  131.             } elseif (count($v2) < count($v1)) {
  132.                 if (isset($versions[$v1[$i]])) {
  133.                     $compare = ($versions[$v1[$i]] < 4) ? -1 : 1;
  134.                 } else {
  135.                     $compare = 1;
  136.                 }
  137.             }
  138.         }
  139.  
  140.         // Compare the versions
  141.         if (func_num_args() > 2) {
  142.             switch ($operator) {
  143.                 case '>':
  144.                 case 'gt':
  145.                     return (bool) ($compare > 0);
  146.                     break;
  147.                 case '>=':
  148.                 case 'ge':
  149.                     return (bool) ($compare >= 0);
  150.                     break;
  151.                 case '<=':
  152.                 case 'le':
  153.                     return (bool) ($compare <= 0);
  154.                     break;
  155.                 case '==':
  156.                 case '=':
  157.                 case 'eq':
  158.                     return (bool) ($compare == 0);
  159.                     break;
  160.                 case '<>':
  161.                 case '!=':
  162.                 case 'ne':
  163.                     return (bool) ($compare != 0);
  164.                     break;
  165.                 case '':
  166.                 case '<':
  167.                 case 'lt':
  168.                     return (bool) ($compare < 0);
  169.                     break;
  170.                 default:
  171.                     return;
  172.             }
  173.         }
  174.  
  175.         return $compare;
  176.     }
  177. }
  178.  
  179. ?>

Documentation generated on Mon, 05 Mar 2007 21:31:18 +0000 by phpDocumentor 1.3.1