summaryrefslogtreecommitdiff
path: root/extlib/Net/IDNA.php
blob: 987a37ef19dd8031138190189c570b25a8f6d5f7 (plain)
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
<?php

// {{{ license

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
//
// +----------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU Lesser General Public License as       |
// | published by the Free Software Foundation; either version 2.1 of the |
// | License, or (at your option) any later version.                      |
// |                                                                      |
// | This library is distributed in the hope that it will be useful, but  |
// | WITHOUT ANY WARRANTY; without even the implied warranty of           |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// |                                                                      |
// | You should have received a copy of the GNU Lesser General Public     |
// | License along with this library; if not, write to the Free Software  |
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 |
// | USA.                                                                 |
// +----------------------------------------------------------------------+
//

// }}}


/**
 * Encode/decode Internationalized Domain Names.
 * Factory class to get correct implementation either for php4 or php5.
 *
 * @author  Markus Nix <mnix@docuverse.de>
 * @author  Matthias Sommerfeld <mso@phlylabs.de>
 * @package Net
 * @version $Id: IDNA.php 284681 2009-07-24 04:24:27Z clockwerx $
 */

class Net_IDNA
{
    // {{{ factory
    /**
     * Attempts to return a concrete IDNA instance for either php4 or php5.
     *
     * @param  array  $params   Set of paramaters
     * @return object IDNA      The newly created concrete Log instance, or an
     *                          false on an error.
     * @access public
     */
    function getInstance($params = array())
    {
        $version   = explode( '.', phpversion() );
        $handler   = ((int)$version[0] > 4) ? 'php5' : 'php4';
        $class     = 'Net_IDNA_' . $handler;
        $classfile = 'Net/IDNA/' . $handler . '.php';

        /*
         * Attempt to include our version of the named class, but don't treat
         * a failure as fatal.  The caller may have already included their own
         * version of the named class.
         */
        @include_once $classfile;

        /* If the class exists, return a new instance of it. */
        if (class_exists($class)) {
            return new $class($params);
        }

        return false;
    }
    // }}}

    // {{{ singleton
    /**
     * Attempts to return a concrete IDNA instance for either php4 or php5,
     * only creating a new instance if no IDNA instance with the same
     * parameters currently exists.
     *
     * @param  array  $params   Set of paramaters
     * @return object IDNA      The newly created concrete Log instance, or an
     *                          false on an error.
     * @access public
     */
    function singleton($params = array())
    {
        static $instances;
        if (!isset($instances)) {
            $instances = array();
        }

        $signature = serialize($params);
        if (!isset($instances[$signature])) {
            $instances[$signature] = Net_IDNA::getInstance($params);
        }

        return $instances[$signature];
    }
    // }}}
}

?>