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

Mustache::replace()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 1
nop 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