Encode::filter()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Filter\FilterRule;
10
11
use Particle\Filter\FilterRule;
12
13
/**
14
 * Class Encode
15
 *
16
 * @package Particle\Filter\FilterRule
17
 */
18
class Encode extends FilterRule
19
{
20
    /**
21
     * @var string
22
     */
23
    protected $toEncoding;
24
25
    /**
26
     * @var string|null
27
     */
28
    protected $fromEncoding;
29
30
    /**
31
     * @param string|null $toEncoding
32
     * @param string|null $fromEncoding
33
     */
34 5
    public function __construct($toEncoding = null, $fromEncoding = null)
35
    {
36 5
        if ($toEncoding === null) {
37 1
            $toEncoding = $this->encodingFormat;
38 1
        }
39
40 5
        $this->toEncoding = $toEncoding;
41 5
        $this->fromEncoding = $fromEncoding;
42 5
    }
43
44
    /**
45
     * Changes encoding of the value
46
     *
47
     * @param mixed $value
48
     * @return string
49
     */
50 5
    public function filter($value)
51
    {
52 5
        if ($this->toEncoding === null) {
53 1
            return $value;
54
        }
55
56 4
        if ($this->fromEncoding === null) {
57 2
            return mb_convert_encoding($value, $this->toEncoding);
58
        }
59
60 2
        return mb_convert_encoding($value, $this->toEncoding, $this->fromEncoding);
61
    }
62
}
63