ForTranslation::translate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 19
rs 9.9332
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * @package Mailcode
4
 * @subpackage Translator
5
 */
6
7
declare(strict_types=1);
8
9
namespace Mailcode\Translator\Syntax\ApacheVelocity;
10
11
use Mailcode\Mailcode_Commands_Command_For;
12
use Mailcode\Mailcode_Translator_Command_For;
13
use Mailcode\Translator\Syntax\BaseApacheVelocityCommandTranslation;
14
15
/**
16
 * Translates the {@see Mailcode_Commands_Command_For} command to Apache Velocity.
17
 *
18
 * @package Mailcode
19
 * @subpackage Translator
20
 * @author Sebastian Mordziol <[email protected]>
21
 */
22
class ForTranslation extends BaseApacheVelocityCommandTranslation implements Mailcode_Translator_Command_For
23
{
24
    public function translate(Mailcode_Commands_Command_For $command): string
25
    {
26
        $loopBreak = '';
27
        if ($command->isBreakAtEnabled()) {
28
            $token = $command->getBreakAtToken();
29
            if ($token !== null) {
30
                $loopBreak = sprintf('#if($foreach.count > %s)#{break}#{end}', $token->getMatchedText());
31
            }
32
        }
33
34
        // Using $source.list() here to ensure that Velocity always treats
35
        // the variable as a list, even if there is only a single entry
36
        // in the list (it would otherwise iterate over the keys in the
37
        // single entry).
38
        return sprintf(
39
            '#{foreach}(%s in %s.list())%s',
40
            $command->getLoopVariable()->getFullName(),
41
            $command->getSourceVariable()->getFullName(),
42
            $loopBreak
43
        );
44
    }
45
}
46