1<?php
2/*
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22namespace Thrift\Factory;
23
24use Thrift\StringFunc\Core;
25use Thrift\StringFunc\Mbstring;
26use Thrift\StringFunc\TStringFunc;
27
28class TStringFuncFactory
29{
30    private static $_instance;
31
32    /**
33     * Get the Singleton instance of TStringFunc implementation that is
34     * compatible with the current system's mbstring.func_overload settings.
35     *
36     * @return TStringFunc
37     */
38    public static function create()
39    {
40        if (!self::$_instance) {
41            self::_setInstance();
42        }
43
44        return self::$_instance;
45    }
46
47    private static function _setInstance()
48    {
49        /**
50         * Cannot use str* functions for byte counting because multibyte
51         * characters will be read a single bytes.
52         *
53         * See: http://php.net/manual/en/mbstring.overload.php
54         */
55        if (ini_get('mbstring.func_overload') & 2) {
56            self::$_instance = new Mbstring();
57        } else {
58            /**
59             * mbstring is not installed or does not have function overloading
60             * of the str* functions enabled so use PHP core str* functions for
61             * byte counting.
62             */
63            self::$_instance = new Core();
64        }
65    }
66}
67