[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/google/Google/ -> Utils.php (source)

   1  <?php
   2  /*
   3   * Copyright 2011 Google Inc.
   4   *
   5   * Licensed under the Apache License, Version 2.0 (the "License");
   6   * you may not use this file except in compliance with the License.
   7   * You may obtain a copy of the License at
   8   *
   9   *     http://www.apache.org/licenses/LICENSE-2.0
  10   *
  11   * Unless required by applicable law or agreed to in writing, software
  12   * distributed under the License is distributed on an "AS IS" BASIS,
  13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14   * See the License for the specific language governing permissions and
  15   * limitations under the License.
  16   */
  17  
  18  /**
  19   * Collection of static utility methods used for convenience across
  20   * the client library.
  21   *
  22   * @author Chirag Shah <[email protected]>
  23   */
  24  class Google_Utils
  25  {
  26    public static function urlSafeB64Encode($data)
  27    {
  28      $b64 = base64_encode($data);
  29      $b64 = str_replace(
  30          array('+', '/', '\r', '\n', '='),
  31          array('-', '_'),
  32          $b64
  33      );
  34      return $b64;
  35    }
  36  
  37    public static function urlSafeB64Decode($b64)
  38    {
  39      $b64 = str_replace(
  40          array('-', '_'),
  41          array('+', '/'),
  42          $b64
  43      );
  44      return base64_decode($b64);
  45    }
  46  
  47    /**
  48     * Misc function used to count the number of bytes in a post body, in the
  49     * world of multi-byte chars and the unpredictability of
  50     * strlen/mb_strlen/sizeof, this is the only way to do that in a sane
  51     * manner at the moment.
  52     *
  53     * This algorithm was originally developed for the
  54     * Solar Framework by Paul M. Jones
  55     *
  56     * @link   http://solarphp.com/
  57     * @link   http://svn.solarphp.com/core/trunk/Solar/Json.php
  58     * @link   http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Json/Decoder.php
  59     * @param  string $str
  60     * @return int The number of bytes in a string.
  61     */
  62    public static function getStrLen($str)
  63    {
  64      $strlenVar = strlen($str);
  65      $d = $ret = 0;
  66      for ($count = 0; $count < $strlenVar; ++ $count) {
  67        $ordinalValue = ord($str{$ret});
  68        switch (true) {
  69          case (($ordinalValue >= 0x20) && ($ordinalValue <= 0x7F)):
  70            // characters U-00000000 - U-0000007F (same as ASCII)
  71            $ret ++;
  72            break;
  73          case (($ordinalValue & 0xE0) == 0xC0):
  74            // characters U-00000080 - U-000007FF, mask 110XXXXX
  75            // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  76            $ret += 2;
  77            break;
  78          case (($ordinalValue & 0xF0) == 0xE0):
  79            // characters U-00000800 - U-0000FFFF, mask 1110XXXX
  80            // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  81            $ret += 3;
  82            break;
  83          case (($ordinalValue & 0xF8) == 0xF0):
  84            // characters U-00010000 - U-001FFFFF, mask 11110XXX
  85            // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  86            $ret += 4;
  87            break;
  88          case (($ordinalValue & 0xFC) == 0xF8):
  89            // characters U-00200000 - U-03FFFFFF, mask 111110XX
  90            // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  91            $ret += 5;
  92            break;
  93          case (($ordinalValue & 0xFE) == 0xFC):
  94            // characters U-04000000 - U-7FFFFFFF, mask 1111110X
  95            // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  96            $ret += 6;
  97            break;
  98          default:
  99            $ret ++;
 100        }
 101      }
 102      return $ret;
 103    }
 104  
 105    /**
 106     * Normalize all keys in an array to lower-case.
 107     * @param array $arr
 108     * @return array Normalized array.
 109     */
 110    public static function normalize($arr)
 111    {
 112      if (!is_array($arr)) {
 113        return array();
 114      }
 115  
 116      $normalized = array();
 117      foreach ($arr as $key => $val) {
 118        $normalized[strtolower($key)] = $val;
 119      }
 120      return $normalized;
 121    }
 122  
 123    /**
 124     * Convert a string to camelCase
 125     * @param  string $value
 126     * @return string
 127     */
 128    public static function camelCase($value)
 129    {
 130      $value = ucwords(str_replace(array('-', '_'), ' ', $value));
 131      $value = str_replace(' ', '', $value);
 132      $value[0] = strtolower($value[0]);
 133      return $value;
 134    }
 135  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1