Completed
Push — master ( 302154...8eae75 )
by Lars
04:23
created

Xml   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 87.88%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 0
cbo 0
dl 0
loc 65
ccs 29
cts 33
cp 0.8788
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C utf8_decode() 0 28 7
B utf8_encode() 0 23 4
1
<?php
2
3
/*
4
 * Copyright (C) 2013 Nicolas Grekas - [email protected]
5
 *
6
 * This library is free software; you can redistribute it and/or modify it
7
 * under the terms of the (at your option):
8
 * Apache License v2.0 (http://apache.org/licenses/LICENSE-2.0.txt), or
9
 * GNU General Public License v2.0 (http://gnu.org/licenses/gpl-2.0.txt).
10
 */
11
12
namespace voku\helper\shim;
13
14
/**
15
 * Class Xml for utf8_encode / utf8_decode
16
 *
17
 * @package voku\helper\shim
18
 */
19
class Xml
20
{
21
  /**
22
   * @param string $s
23
   *
24
   * @return string
25
   */
26 1
  public static function utf8_encode($s)
27
  {
28 1
    $s .= $s;
29 1
    $len = strlen($s);
30
31 1
    for ($i = $len >> 1, $j = 0; $i < $len; ++$i, ++$j) {
32
      switch (true) {
33 1
        case $s[$i] < "\x80":
34 1
          $s[$j] = $s[$i];
35 1
          break;
36 1
        case $s[$i] < "\xC0":
37 1
          $s[$j] = "\xC2";
38 1
          $s[++$j] = $s[$i];
39 1
          break;
40
        default:
41 1
          $s[$j] = "\xC3";
42 1
          $s[++$j] = chr(ord($s[$i]) - 64);
43 1
          break;
44
      }
45
    }
46
47 1
    return substr($s, 0, $j);
48
  }
49
50
  /**
51
   * @param string $s
52
   *
53
   * @return string
54
   */
55 7
  public static function utf8_decode($s)
56
  {
57 7
    $s .= '';
58 7
    $len = strlen($s);
59
60 7
    for ($i = 0, $j = 0; $i < $len; ++$i, ++$j) {
61 7
      switch ($s[$i] & "\xF0") {
62
        case "\xC0":
63
        case "\xD0":
64 7
          $c = (ord($s[$i] & "\x1F") << 6) | ord($s[++$i] & "\x3F");
65 7
          $s[$j] = $c < 256 ? chr($c) : '?';
66 7
          break;
67
68
        /** @noinspection PhpMissingBreakStatementInspection */
69
        case "\xF0":
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
70
          ++$i;
71 7
        case "\xE0":
72 5
          $s[$j] = '?';
73 5
          $i += 2;
74 5
          break;
75
76
        default:
77 7
          $s[$j] = $s[$i];
78
      }
79
    }
80
81 7
    return substr($s, 0, $j);
82
  }
83
}
84