If this is your first visit, check our documentation. To post, register or login.
Forums / Zen Cart Code Suggestions / New function: zen_version_compare

New function: zen_version_compare

Started by lat9 on 27 Jun 2016, 7:37 PM · Views: 95

Results 1 to 6 of 6
27 Jun 2016, 7:37 PM
#1
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

New function: zen_version_compare

With all the versions floating around in the Zen Cart universe, plugins need the capability to see if that version is, for instance, greater than or equal to some reference version.

As I found with the Edit Orders changes going from Zen Cart 1.5.5 to 1.5.5a, the built-in PHP version_compare function doesn't cut it when there's an alphabetic suffix thrown in the mix. In that case, the version_compare ('1.5.5a', '1.5.5') return value will be -1 -- indicating that 1.5.5a is less than 1.5.5.

Here's a handy-dandy little function that makes use of the PHP version_compare, but adds that alpha-suffix into the mix:

<?php
// -----
// This function "extends" the PHP version_compare function to be usable in checking Zen Cart versions; I placed it into /includes/functions/extra_functions/zen_version_compare.php:
//
// $version1 ... (string) The version to compare, e.g. '1.5.5a' or '1.5.4'
// $version2 ... (mixed)  Identifies he Zen Cart version to compare against, either the explicit (string) version to use in the compare
//                        or (boolean) false, in which case the current Zen Cart version is used in the compare.
// $operator ... (mixed)  Identifies the (optional) operator to use -- in a manner similar to the PHP version_compare function.
//
// Returns values as identified in the PHP version_compare function.
//
function zen_version_compare ($version1, $version2 = false, $operator = false)
{
    if ($version2 === false) {
        $version2 = PROJECT_VERSION_MAJOR . '.' . PROJECT_VERSION_MINOR;
    }
    $version1_suffix = substr ($version1, -1, 1);
    $version2_suffix = substr ($version2, -1, 1);
    $v1_alpha_suffix = ctype_alpha ($version1_suffix);
    $v2_alpha_suffix = ctype_alpha ($version2_suffix);
    if ($v1_alpha_suffix || $v2_alpha_suffix ) {
        if ($v1_alpha_suffix) {
            $version1 = substr ($version1, 0, -1) . '.' . (ord (strtoupper ($version1_suffix)) - ord ('A') + 1);
        } else {
            $version1 .= '.0';
        }
        if ($v2_alpha_suffix) {
            $version2 = substr ($version2, 0, -1) . '.' . (ord (strtoupper ($version2_suffix)) - ord ('A') + 1);
        } else {
            $version2 .= '.0';
        }
    }
    return ($operator === false) ? version_compare ($version1, $version2) : version_compare ($version1, $version2, $operator);
}

Here's also a debug-script that I used to test/validate the output; this one I placed into /test_version_compare.php:

<?php
require ('includes/application_top.php');

$check_array = array (
    array ('1.3.9h', '1.3.9a', false, 1),
    array ('1.5.5a', '1.5.5', false, 1),
    array ('1.5.5', '1.5.5a', false, -1),
    array ('1.5.4', '1.5.5', false, -1),
    array ('1.5.4', '1.5.4', false, 0),
    array ('1.5.5a', '1.5.5', '>=', true),
    array ('1.5.5a', false, false, 1)       //-Assumes running on Zen Cart 1.5.5!
);

foreach ($check_array as $current_values) {
    $result = zen_version_compare ($current_values[0], $current_values[1], $current_values[2]);
    echo "Comparing " . $current_values[0] . (($current_values[2] === false) ? ' to ' : (' ' . $current_values[2] . ' ')) . $current_values[1] . (($result === $current_values[3]) ? ' ✓' : (' ✗ (' . $result . ' != ' . $current_values[3])) . '<br />';
}
27 Jun 2016, 8:55 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: New function: zen_version_compare

I usually just use simple string comparisons.

$a = '1.5.5';
$b = '1.5.5a';

echo $a < $b; // 1 = true
echo $b < $a; // 0 = false

I'm not sure the use of version_compare() is necessary.

Happy to discuss...

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

28 Jun 2016, 11:10 AM
#3
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: New function: zen_version_compare

I was looking through the distributions I've gathered and couldn't find one of the beta's (1.5.2?). If/when a beta- or alpha- suffix would be applied, what format would you use?

28 Jun 2016, 4:42 PM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: New function: zen_version_compare

The addition of "-beta" as a suffix would be a problem for both your and my approaches.

I'm not sure how significantly the handling of "beta" matters in this context though.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

28 Jun 2016, 5:15 PM
#5
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: New function: zen_version_compare

Agreed, but wouldn't the suffix be something like -beta1 (ending in a number)?

28 Jun 2016, 9:40 PM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: New function: zen_version_compare

Hmmmm ... I don't remember numbering our betas, at least not in PROJECT_VERSION_MINOR
I know I don't plan to. :D

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.