<?  // vim:set et ts=4 sw=4 sts=4:
# Copyright (C) 2003-2008 Axentra Corporation.
#
# This is UNPUBLISHED PROPRIETARY SOURCE CODE OF Axentra Corporation.
# The copyright notice above does not evidence any actual or intended
# publication of such source code. The contents of this file may not
# be disclosed to third parties, copied or duplicated in any form, in
# whole or in part, without the prior written permission of
# Axentra Corporation.
#
namespace HipPlayConnect\FileSystem;


class AxFileComparison {

    const ORDER_ASC = "asc";
    const ORDER_DESC = "desc";

    protected static $COMPARATORS = array(
        'caption' => array(
                array('\\HipPlayConnect\\FileSystem\\AxFileComparison', 'compareCaptionReverse'),
                array('\\HipPlayConnect\\FileSystem\\AxFileComparison', 'compareCaption'),
        ),

        'name' => array(
                array('\\HipPlayConnect\\FileSystem\\AxFileComparison', 'strcasecmpReverse'),
                'strcasecmp'
        ),

        'size' => array(
                array('\\HipPlayConnect\\FileSystem\\AxFileComparison', 'compareSizeReverse'),
                array('\\HipPlayConnect\\FileSystem\\AxFileComparison', 'compareSize'),
        ),

        'mtime' => array(
                array('\\HipPlayConnect\\FileSystem\\AxFileComparison', 'compareMtimeReverse'),
                array('\\HipPlayConnect\\FileSystem\\AxFileComparison', 'compareMtime'),
        ),

        'mimeType' => array(
                array('\\HipPlayConnect\\FileSystem\\AxFileComparison', 'compareMimeTypeReverse'),
                array('\\HipPlayConnect\\FileSystem\\AxFileComparison', 'compareMimeType'),
        ),

        'tags' => array(
                array('\\HipPlayConnect\\FileSystem\\AxFileComparison', 'compareTagsReverse'),
                array('\\HipPlayConnect\\FileSystem\\AxFileComparison', 'compareTags'),
        )
    );

    public static function getComparator($field, $order=self::ORDER_ASC) {
        $comparators = self::$COMPARATORS[$field];
        return ($order == self::ORDER_DESC)
                ? $comparators[0]
                : $comparators[1];
    }


    public static function strcasecmpReverse( $b, $a )
    {
        return strcasecmp( $a, $b );
    }

    public static function compareSize( $a, $b )
    {
        return
                ( $cmp = bccomp( $a[ 'size' ], $b[ 'size' ] ) ) == 0
                        ? strcasecmp( $a[ 'name' ], $b[ 'name' ] )
                        : $cmp;
    }

    public static function compareSizeReverse( $b, $a )
    {
        return
                ( $cmp = bccomp( $a[ 'size' ], $b[ 'size'] ) ) == 0
                        ? strcasecmp( $a[ 'name' ], $b[ 'name' ] )
                        : $cmp;
    }

    // sort by caption (no caption last), then by name
    public static function compareCaptions( $a, $b )
    {
        return
                ( $cmp = strcasecmp(
                        isset( $a[ 'caption' ] ) && $a[ 'caption' ] != '' ? '0' . $a[ 'caption' ] : '1',
                        isset( $b[ 'caption' ] ) && $b[ 'caption' ] != '' ? '0' . $b[ 'caption' ] : '1'
                ) ) == 0
                        ? strcasecmp( $a[ 'name' ], $b[ 'name' ] )
                        : $cmp;
    }

    public static function compareCaptionsReverse( $b, $a )
    {
        return
                ( $cmp = strcasecmp(
                        isset( $a[ 'caption' ] ) && $a[ 'caption' ] != '' ? '0' . $a[ 'caption' ] : '1',
                        isset( $b[ 'caption' ] ) && $b[ 'caption' ] != '' ? '0' . $b[ 'caption' ] : '1'
                ) ) == 0
                        ? strcasecmp( $a[ 'name' ], $b[ 'name' ] )
                        : $cmp;
    }

    // sort by existence of tags (no tag last), then by name
    public static function compareTags( $a, $b )
    {
        return
                ( $cmp = strcasecmp( isset( $a[ 'tags' ] ) ? 0 : 1, isset( $b[ 'tags' ] ) ? 0 : 1 ) ) == 0
                        ? strcasecmp( $a[ 'name' ], $b[ 'name' ] )
                        : $cmp;
    }

    public static function compareTagsReverse( $b, $a )
    {
        return
                ( $cmp = strcasecmp( isset( $a[ 'tags' ] ) ? 0 : 1, isset( $b[ 'tags' ] ) ? 0 : 1 ) ) == 0
                        ? strcasecmp( $a[ 'name' ], $b[ 'name' ] )
                        : $cmp;
    }

    // sort by mime type, then by name
    public static function compareMimeType( $a, $b )
    {
        return
                ( $cmp = strcasecmp( $a[ 'mimeType' ], $b[ 'mimeType'] ) ) == 0
                        ? strcasecmp( $a[ 'name' ], $b[ 'name' ] )
                        : $cmp;
    }

    public static function compareMimeTypeReverse( $b, $a )
    {
        return
                ( $cmp = strcasecmp( $a[ 'mimeType' ], $b[ 'mimeType'] ) ) == 0
                        ? strcasecmp( $a[ 'name' ], $b[ 'name' ] )
                        : $cmp;
    }

    public static function compareMtime( $a, $b )
    {
        return
                ( $cmp = ( $a[ 'mtime' ] - $b[ 'mtime' ] ) ) == 0
                        ? strcasecmp( $a[ 'name' ], $b[ 'name' ] )
                        : $cmp;
    }

    public static function compareMtimeReverse( $b, $a )
    {
        return
                ( $cmp = ( $a[ 'mtime' ] - $b[ 'mtime'] ) ) == 0
                        ? strcasecmp( $a[ 'name' ], $b[ 'name' ] )
                        : $cmp;
    }

}
