Excel2003XmlEncoder::supportsEncoding()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Pim\Bundle\ExcelConnectorBundle\Encoder;
4
5
use Symfony\Component\Serializer\Encoder\EncoderInterface;
6
7
/**
8
 * Excel 2003 Xml Encoder
9
 *
10
 * @author    Antoine Guigan <[email protected]>
11
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
12
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
13
 */
14
class Excel2003XmlEncoder implements EncoderInterface
15
{
16
    /** @const string The name of the format */
17
    const FORMAT_NAME = 'excel_2003_xml';
18
19
    /** @const string XML template for one cell */
20
    const CELL_TEMPLATE='<Cell><Data ss:Type="{{type}}">{{data}}</Data></Cell>';
21
22
    /** @const string XML template for one row */
23
    const ROW_TEMPLATE='<Row>{{cells}}</Row>';
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function encode($data, $format, array $context = array())
29
    {
30
        $cells = '';
31
        foreach ($data as $value) {
32
            $cells .= strtr(
33
                static::CELL_TEMPLATE,
34
                [
35
                    '{{type}}' => is_numeric($value) ? 'Number' : 'String',
36
                    '{{data}}' => $value
37
                ]
38
            );
39
        }
40
41
        return strtr(static::ROW_TEMPLATE, [ '{{cells}}' => $cells ]);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function supportsEncoding($format)
48
    {
49
        return static::FORMAT_NAME === $format;
50
    }
51
}
52