Advertisement
Guest User

Meitar Moscovitz

a guest
Oct 28th, 2009
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.43 KB | None | 0 0
  1. /**
  2.  *  Copyright 2009 by Meitar Moscovitz  (email : meitarm@gmail.com)
  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 2 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, write to the Free Software
  16.  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17.  *
  18.  */
  19.  
  20. /**
  21.  * Compares two arbitrary version strings.
  22.  *
  23.  * @param mixed $v The version to test. Can be a string or array created with parse_version().
  24.  * @param string $op The comparison operation to perform.
  25.  * @param mixed $n The version number to test the comparison again. Can also be a string or array.
  26.  * @return mixed True or false if comparison is performed, otherwise returns an array of the passed-in version.
  27.  *
  28.  * @see parse_version(string $str, bool $named)
  29.  */
  30. function test_version ($ver, $op, $num) {
  31.     $ver = (is_string($ver)) ? parse_version($ver) : $ver;
  32.     $num = (is_string($num)) ? parse_version($num) : $num;
  33.  
  34.     // Be sure both arrays are the same size.
  35.     if (count($ver) < count($num)) {
  36.         $ver = array_pad($ver, count($num), 0);
  37.     } else if (count($num) < count($ver)) {
  38.         $num = array_pad($num, count($ver), 0);
  39.     }
  40.  
  41.     // Based on $op, compare each element in turn.
  42.     switch ($op) {
  43.         case '>':
  44.         case 'gt':
  45.             for ($i = 0; $i < count($ver); $i++) {
  46.                 if ($ver[$i] > $num[$i]) {
  47.                     return true;
  48.                 } else if ($ver[$i] === $num[$i]) {
  49.                     continue;
  50.                 } else {
  51.                     return false;
  52.                 }
  53.             }
  54.         case '<':
  55.         case 'lt':
  56.             for ($i = 0; $i < count($ver); $i++) {
  57.                 if ($ver[$i] < $num[$i]) {
  58.                     return true;
  59.                 } else if ($ver[$i] === $num[$i]) {
  60.                     continue;
  61.                 } else {
  62.                     return false;
  63.                 }
  64.             }
  65.         case '=':
  66.         case '==':
  67.         case 'eq':
  68.             for ($i = 0; $i < count($ver); $i++) {
  69.                 if ($ver[$i] === $num[$i]) {
  70.                     continue;
  71.                 } else {
  72.                     return false;
  73.                 }
  74.             }
  75.             // All are equal
  76.             return true;
  77.         case '>=':
  78.         case 'gteq':
  79.             for ($i = 0; $i < count($ver); $i++) {
  80.                 if ($ver[$i] >= $num[$i]) {
  81.                     continue;
  82.                 } else {
  83.                     return false;
  84.                 }
  85.             }
  86.             // All are greater than or equal to
  87.             return true;
  88.         case '<=':
  89.         case 'lteq':
  90.             for ($i = 0; $i < count($ver); $i++) {
  91.                 if ($ver[$i] <= $num[$i]) {
  92.                     continue;
  93.                 } else {
  94.                     return false;
  95.                 }
  96.             }
  97.             // All are less than or equal to
  98.             return true;
  99.         default:
  100.             return $ver;
  101.     }
  102. }
  103.  
  104. /**
  105.  * Parses a version number string into an array.
  106.  *
  107.  * @param string $str The version number to parse.
  108.  * @param bool $named If true, adds extra elements to the array named "major," "minor," and "patch" for ease of use. Default is false.
  109.  * @return array An object whose elements correspond to the dot-seperated version numbers.
  110.  */
  111. function parse_version ($str, $named = false) {
  112.     if (!is_string($str)) { return array(); }
  113.     $x = explode('.', $str);
  114.     $r = array();
  115.     for ($i = 0; $i < count($x); $i++) {
  116.         if ($i === 0) {
  117.             $r[0]       = (int)$x[0];
  118.             if ($named) { $r['major'] = (int)$x[0]; }
  119.         } else if ($i === 1) {
  120.             $r[1]       = (int)$x[1];
  121.             if ($named) { $r['minor'] = (int)$x[1]; }
  122.         } else if ($i === 2) {
  123.             $r[2]       = (int)$x[2];
  124.             if ($named) { $r['patch'] = (int)$x[2]; }
  125.         } else {
  126.             $r[] = (int)$x[$i];
  127.         }
  128.     }
  129.     return $r;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement