Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
#[AllowDynamicProperties]
23
class Google_Utils
24
{
25
  public static function urlSafeB64Encode($data)
26
  {
27
    $b64 = base64_encode($data);
28
    $b64 = str_replace(
29
        array('+', '/', '\r', '\n', '='),
30
        array('-', '_'),
31
        $b64
32
    );
33
    return $b64;
34
  }
35
 
36
  public static function urlSafeB64Decode($b64)
37
  {
38
    $b64 = str_replace(
39
        array('-', '_'),
40
        array('+', '/'),
41
        $b64
42
    );
43
    return base64_decode($b64);
44
  }
45
 
46
  /**
47
   * Misc function used to count the number of bytes in a post body, in the
48
   * world of multi-byte chars and the unpredictability of
49
   * strlen/mb_strlen/sizeof, this is the only way to do that in a sane
50
   * manner at the moment.
51
   *
52
   * This algorithm was originally developed for the
53
   * Solar Framework by Paul M. Jones
54
   *
55
   * @link   http://solarphp.com/
56
   * @link   http://svn.solarphp.com/core/trunk/Solar/Json.php
57
   * @link   http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Json/Decoder.php
58
   * @param  string $str
59
   * @return int The number of bytes in a string.
60
   */
61
  public static function getStrLen($str)
62
  {
63
    $strlenVar = strlen($str);
64
    $d = $ret = 0;
65
    for ($count = 0; $count < $strlenVar; ++ $count) {
66
      $ordinalValue = ord($str[$ret]);
67
      switch (true) {
68
        case (($ordinalValue >= 0x20) && ($ordinalValue <= 0x7F)):
69
          // characters U-00000000 - U-0000007F (same as ASCII)
70
          $ret ++;
71
          break;
72
        case (($ordinalValue & 0xE0) == 0xC0):
73
          // characters U-00000080 - U-000007FF, mask 110XXXXX
74
          // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
75
          $ret += 2;
76
          break;
77
        case (($ordinalValue & 0xF0) == 0xE0):
78
          // characters U-00000800 - U-0000FFFF, mask 1110XXXX
79
          // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
80
          $ret += 3;
81
          break;
82
        case (($ordinalValue & 0xF8) == 0xF0):
83
          // characters U-00010000 - U-001FFFFF, mask 11110XXX
84
          // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
85
          $ret += 4;
86
          break;
87
        case (($ordinalValue & 0xFC) == 0xF8):
88
          // characters U-00200000 - U-03FFFFFF, mask 111110XX
89
          // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
90
          $ret += 5;
91
          break;
92
        case (($ordinalValue & 0xFE) == 0xFC):
93
          // characters U-04000000 - U-7FFFFFFF, mask 1111110X
94
          // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
95
          $ret += 6;
96
          break;
97
        default:
98
          $ret ++;
99
      }
100
    }
101
    return $ret;
102
  }
103
 
104
  /**
105
   * Normalize all keys in an array to lower-case.
106
   * @param array $arr
107
   * @return array Normalized array.
108
   */
109
  public static function normalize($arr)
110
  {
111
    if (!is_array($arr)) {
112
      return array();
113
    }
114
 
115
    $normalized = array();
116
    foreach ($arr as $key => $val) {
117
      $normalized[strtolower($key)] = $val;
118
    }
119
    return $normalized;
120
  }
121
 
122
  /**
123
   * Convert a string to camelCase
124
   * @param  string $value
125
   * @return string
126
   */
127
  public static function camelCase($value)
128
  {
129
    $value = ucwords(str_replace(array('-', '_'), ' ', $value));
130
    $value = str_replace(' ', '', $value);
131
    $value[0] = strtolower($value[0]);
132
    return $value;
133
  }
134
}