CharacterSetEci.php 3.86 KB
Newer Older
Hamza Arfaoui's avatar
Hamza Arfaoui committed
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
<?php
/**
 * BaconQrCode
 *
 * @link      http://github.com/Bacon/BaconQrCode For the canonical source repository
 * @copyright 2013 Ben 'DASPRiD' Scholzen
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
 */

namespace BaconQrCode\Common;

/**
 * Encapsulates a Character Set ECI, according to "Extended Channel
 * Interpretations" 5.3.1.1 of ISO 18004.
 */
class CharacterSetEci extends AbstractEnum
{
    /**#@+
     * Character set constants.
     */
    const CP437                = 0;
    const ISO8859_1            = 1;
    const ISO8859_2            = 4;
    const ISO8859_3            = 5;
    const ISO8859_4            = 6;
    const ISO8859_5            = 7;
    const ISO8859_6            = 8;
    const ISO8859_7            = 9;
    const ISO8859_8            = 10;
    const ISO8859_9            = 11;
    const ISO8859_10           = 12;
    const ISO8859_11           = 13;
    const ISO8859_12           = 14;
    const ISO8859_13           = 15;
    const ISO8859_14           = 16;
    const ISO8859_15           = 17;
    const ISO8859_16           = 18;
    const SJIS                 = 20;
    const CP1250               = 21;
    const CP1251               = 22;
    const CP1252               = 23;
    const CP1256               = 24;
    const UNICODE_BIG_UNMARKED = 25;
    const UTF8                 = 26;
    const ASCII                = 27;
    const BIG5                 = 28;
    const GB18030              = 29;
    const EUC_KR               = 30;
    /**#@-*/

    /**
     * Map between character names and their ECI values.
     *
     * @var array
     */
    protected static $nameToEci = array(
        'ISO-8859-1'   => self::ISO8859_1,
        'ISO-8859-2'   => self::ISO8859_2,
        'ISO-8859-3'   => self::ISO8859_3,
        'ISO-8859-4'   => self::ISO8859_4,
        'ISO-8859-5'   => self::ISO8859_5,
        'ISO-8859-6'   => self::ISO8859_6,
        'ISO-8859-7'   => self::ISO8859_7,
        'ISO-8859-8'   => self::ISO8859_8,
        'ISO-8859-9'   => self::ISO8859_9,
        'ISO-8859-10'  => self::ISO8859_10,
        'ISO-8859-11'  => self::ISO8859_11,
        'ISO-8859-12'  => self::ISO8859_12,
        'ISO-8859-13'  => self::ISO8859_13,
        'ISO-8859-14'  => self::ISO8859_14,
        'ISO-8859-15'  => self::ISO8859_15,
        'ISO-8859-16'  => self::ISO8859_16,
        'SHIFT-JIS'    => self::SJIS,
        'WINDOWS-1250' => self::CP1250,
        'WINDOWS-1251' => self::CP1251,
        'WINDOWS-1252' => self::CP1252,
        'WINDOWS-1256' => self::CP1256,
        'UTF-16BE'     => self::UNICODE_BIG_UNMARKED,
        'UTF-8'        => self::UTF8,
        'ASCII'        => self::ASCII,
        'GBK'          => self::GB18030,
        'EUC-KR'       => self::EUC_KR,
    );

    /**
     * Additional possible values for character sets.
     *
     * @var array
     */
    protected $additionalValues = array(
        self::CP437 => 2,
        self::ASCII => 170,
    );

    /**
     * Gets character set ECI by value.
     *
     * @param  string $name
     * @return CharacterSetEci|null
     */
    public static function getCharacterSetECIByValue($value)
    {
        if ($value < 0 || $value >= 900) {
            throw new Exception\InvalidArgumentException('Value must be between 0 and 900');
        }

        if (false !== ($key = array_search($value, self::$additionalValues))) {
            $value = $key;
        }

        try {
            return new self($value);
        } catch (Exception\UnexpectedValueException $e) {
            return null;
        }
    }

    /**
     * Gets character set ECI by name.
     *
     * @param  string $name
     * @return CharacterSetEci|null
     */
    public static function getCharacterSetECIByName($name)
    {
        $name = strtoupper($name);

        if (isset(self::$nameToEci[$name])) {
            return new self(self::$nameToEci[$name]);
        }

        return null;
    }
}