Completed
Push — master ( 638416...694299 )
by Stefano
03:09
created

Text   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 4
Bugs 2 Features 1
Metric Value
c 4
b 2
f 1
dl 0
loc 62
rs 10
wmc 4
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 5 1
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
    return preg_replace_callback("(\{\{([^}]+)\}\})S",function($c) use ($v){
32
      return Object::fetch(trim($c[1]),$v);
33
    },$t);
34
  }
35
36
  /**
37
   * Create a "slug", an url-safe sanitized string.
38
   *
39
   * @example
40
   *   echo Text::slugify("Thîs îs --- à vêry wrong séntènce!");
41
   *   // this-is-a-very-wrong-sentence
42
   *
43
   * @access public
44
   * @static
45
   * @param  string $text The text to slugify
46
   * @return string       The slug.
47
   */
48
  public static function slugify($text){
49
    return preg_replace(
50
      ['(\s+)','([^a-z0-9-])i','(-+)'],['-','','-'],
51
      strtolower(self::removeAccents($text)));
52
  }
53
54
  /**
55
   * Translit accented characters to neutral ones
56
   *
57
   * @example
58
   *   echo Text::removeAccents("Thîs îs à vêry wrong séntènce!");
59
   *   // This is a very wrong sentence!
60
   *
61
   * @access public
62
   * @static
63
   * @param  string $text The text to translit
64
   * @return string       The translited text
65
   */
66
  public static function removeAccents($text){
67
    static $diac;
68
    return strtr(
69
      utf8_decode($text),
70
      $diac ? $diac : $diac = utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'),
71
      'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
72
  }
73
74
} /* End of class */
75