Completed
Push — master ( da0657...3702d4 )
by Stefano
03:41
created

Text::render()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 14
rs 8.8571
cc 5
eloc 10
nc 5
nop 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
} /* End of class */
46