Completed
Pull Request — 2.x (#93)
by
unknown
02:09
created

Mustache   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceFromFile() 0 4 1
A replace() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\EasyExtendsBundle\Generator;
13
14
class Mustache
15
{
16
    /**
17
     * @param string $string
18
     * @param array  $parameters
19
     *
20
     * @return mixed
21
     */
22
    public static function replace($string, array $parameters)
23
    {
24
        $replacer = function ($match) use ($parameters) {
25
            return isset($parameters[$match[1]]) ? $parameters[$match[1]] : $match[0];
26
        };
27
28
        return preg_replace_callback('/{{\s*(.+?)\s*}}/', $replacer, $string);
29
    }
30
31
    /**
32
     * @param string $file
33
     * @param array  $parameters
34
     *
35
     * @return mixed
36
     */
37
    public static function replaceFromFile($file, array $parameters)
38
    {
39
        return self::replace(file_get_contents($file), $parameters);
40
    }
41
}
42