Punic APIs
  • Namespace
  • Class
  • Tree
  • Todo

Namespaces

  • Punic
    • Exception

Classes

  • Punic\Calendar
  • Punic\Comparer
  • Punic\Currency
  • Punic\Data
  • Punic\Language
  • Punic\Misc
  • Punic\Number
  • Punic\Phone
  • Punic\Plural
  • Punic\Territory
  • Punic\Unit

Exceptions

  • Punic\Exception
  • Punic\Exception\BadArgumentType
  • Punic\Exception\BadDataFileContents
  • Punic\Exception\DataFileNotFound
  • Punic\Exception\DataFileNotReadable
  • Punic\Exception\DataFolderNotFound
  • Punic\Exception\InvalidDataFile
  • Punic\Exception\InvalidLocale
  • Punic\Exception\NotImplemented
  • Punic\Exception\ValueNotInList
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 
<?php

namespace Punic;

use Collator;
use Exception as PHPException;

/**
 * Various helper stuff.
 */
class Comparer
{
    /**
     * @var array
     */
    private $cache;

    /**
     * @var bool
     */
    private $caseSensitive;

    /**
     * @var Collator|null
     */
    private $collator;

    /**
     * @var bool
     */
    private $iconv;

    /**
     * Initializes the instance.
     *
     * @param string $locale
     * @param bool $caseSensitive
     */
    public function __construct($locale = null, $caseSensitive = false)
    {
        $this->cache = array();
        $this->locale = isset($locale) ? $locale : \Punic\Data::getDefaultLocale();
        $this->caseSensitive = (bool) $caseSensitive;
        $this->collator = null;
        if (class_exists('\Collator')) {
            try {
                $this->collator = new Collator($this->locale);
            } catch (PHPException $x) {
            }
        }
        $this->iconv = function_exists('iconv');
    }

    /**
     * Compare two strings.
     *
     * @param string $a
     * @param string $b
     *
     * @return int
     */
    public function compare($a, $b)
    {
        $result = null;
        if (isset($this->collator)) {
            try {
                $a = (string) $a;
                $b = (string) $b;
                if ($this->caseSensitive) {
                    $result = $this->collator->compare($a, $b);
                } else {
                    $array = array($a, $b);
                    if ($this->sort($array) === false) {
                        $result = false;
                    } else {
                        $ia = array_search($a, $array);
                        if ($ia === 1) {
                            $result = 1;
                        } else {
                            $ib = array_search($b, $array);
                            if ($ib === 1) {
                                $result = -1;
                            } else {
                                $result = 0;
                            }
                        }
                    }
                }
            } catch (PHPException $x) {
            }
        }
        if ($result === null) {
            $a = $this->normalize($a);
            $b = $this->normalize($b);

            $result = $this->caseSensitive ? strnatcmp($a, $b) : strnatcasecmp($a, $b);
        }

        return $result;
    }

    /**
     * @param array $array
     * @param bool $keepKeys
     *
     * @return array
     */
    public function sort(&$array, $keepKeys = false)
    {
        $me = $this;
        $result = null;
        if (isset($this->collator)) {
            try {
                if ($keepKeys) {
                    $result = $this->collator->asort($array);
                } else {
                    $result = $this->collator->sort($array);
                }
            } catch (PHPException $x) {
            }
        }
        if ($result === null) {
            if ($keepKeys) {
                $result = uasort(
                    $array,
                    function ($a, $b) use ($me) {
                        return $me->compare($a, $b);
                    }
                );
            } else {
                $result = usort(
                    $array,
                    function ($a, $b) use ($me) {
                        return $me->compare($a, $b);
                    }
                );
            }
        }

        return $result;
    }

    /**
     * @param string $str
     *
     * @return string
     */
    private function normalize($str)
    {
        $str = (string) $str;
        if (!isset($this->cache[$str])) {
            $this->cache[$str] = $str;
            if ($str !== '') {
                if ($this->iconv) {
                    $transliterated = @iconv('UTF-8', 'ASCII//TRANSLIT', $str);
                    if ($transliterated !== false) {
                        $this->cache[$str] = $transliterated;
                    }
                }
            }
        }

        return $this->cache[$str];
    }
}
Punic APIs API documentation generated by ApiGen