Completed
Push — master ( 0cd027...19e79a )
by Stefano
03:00
created

Text   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 8
c 3
b 1
f 1
lcom 0
cbo 2
dl 0
loc 71
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 14 5
A slugify() 0 5 1
A removeAccents() 0 7 2
1
<?php
2
3
/**
4
 * Text
5
 *
6
 * A module of string related utility.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015 - http://caffeina.it
11
 */
12
13
class Text {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
  use Module;
15
16
  /**
17
   * Fast string templating.
18
   * Uses a Twig-like syntax.
19
   *
20
   * @example
21
   *    echo Text::render('Your IP is : {{ server.REMOTE_HOST }}',array('server' => $_SERVER));
22
   *
23
   * @author Stefano Azzolini <[email protected]>
24
   * @access public
25
   * @static
26
   * @param mixed $t  The text template
27
   * @param mixed $v (default: null)  The array of values exposed in template.
28
   * @return string
29
  */
30
  public static function render($t,$v=null){
31
    if (empty($v)) return preg_replace('/{{[^}]+}}/','',$t);
32
    for(  // Init
33
          $r = $ox = $x = false;
34
          // While
35
          false !== ($x = $y = strpos($t,'{{',$x));
36
          // Do
37
          $r .= substr($t, $ox, $x-$ox),
38
          $c  = substr($t, $x += 2, $l = ( $y = strpos($t,'}}', $x) ) - $x),
39
          $ox = $x += $l + 2,
40
          $r .= Object::fetch(trim($c),$v)?:''
41
    );
42
    return $r===false ? $t : $r.substr($t,$ox);
43
  }
44
45
  /**
46
   * Create a "slug", an url-safe sanitized string.
47
   *
48
   * @example
49
   *   echo Text::slugify("Thîs îs --- à vêry wrong séntènce!");
50
   *   // this-is-a-very-wrong-sentence
51
   *
52
   * @access public
53
   * @static
54
   * @param  string $text The text to slugify
55
   * @return string       The slug.
56
   */
57
  public static function slugify($text){
58
    return preg_replace(
59
      ['(\s+)','([^a-z0-9-])i','(-+)'],['-','','-'],
60
      strtolower(self::removeAccents($text)));
61
  }
62
63
  /**
64
   * Translit accented characters to neutral ones
65
   *
66
   * @example
67
   *   echo Text::removeAccents("Thîs îs à vêry wrong séntènce!");
68
   *   // This is a very wrong sentence!
69
   *
70
   * @access public
71
   * @static
72
   * @param  string $text The text to translit
73
   * @return string       The translited text
74
   */
75
  public static function removeAccents($text){
76
    static $diac;
77
    return strtr(
78
      utf8_decode($text),
79
      $diac ? $diac : $diac = utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'),
80
      'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
81
  }
82
83
} /* End of class */
84