JsonDataSource   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 24
c 1
b 0
f 0
dl 0
loc 60
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataSourceReportParams() 0 3 1
A getOptions() 0 3 1
A setOptions() 0 4 1
A __construct() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Jasper report integration for PHP
7
 *
8
 * @link      https://github.com/belgattitude/soluble-jasper
9
 * @author    Vanvelthem Sébastien
10
 * @copyright Copyright (c) 2017-2019 Vanvelthem Sébastien
11
 * @license   MIT
12
 */
13
14
namespace Soluble\Jasper\DataSource;
15
16
use Soluble\Jasper\DataSource\Contract\JRDataSourceFromReportParamsInterface;
17
use Soluble\Jasper\DataSource\Contract\JRDataSourceFromReportParamsTrait;
18
use Soluble\Jasper\DataSource\Contract\ReportParametrableInterface;
19
use Soluble\Jasper\ReportParams;
20
21
class JsonDataSource implements JRDataSourceFromReportParamsInterface, ReportParametrableInterface
22
{
23
    use JRDataSourceFromReportParamsTrait;
24
25
    public const PARAM_JSON_SOURCE         = 'net.sf.jasperreports.json.source';
26
    public const PARAM_JSON_DATE_PATTERN   = 'net.sf.jasperreports.json.date.pattern';
27
    public const PARAM_JSON_NUMBER_PATTERN = 'net.sf.jasperreports.json.number.pattern';
28
    public const PARAM_JSON_LOCALE_CODE    = 'net.sf.jasperreports.json.locale.code';
29
    public const PARAM_JSON_TIMEZONE_ID    = 'net.sf.jasperreports.json.timezone.id';
30
31
    public const DEFAULT_DATE_PATTERN   = 'yyyy-MM-dd';
32
    public const DEFAULT_NUMBER_PATTERN = '0.####';
33
    public const DEFAULT_LOCALE_CODE    = 'en_US';
34
    public const DEFAULT_TIMEZONE_ID    = 'UTC';
35
36
    /**
37
     * @var string
38
     */
39
    private $jsonSource;
40
41
    /**
42
     * @var array
43
     */
44
    private $options;
45
46
    /**
47
     * @param string $jsonSource  file or url for the json source
48
     * @param string $datePattern json date pattern in java style (i.e. 'yyyy-MM-dd')
49
     */
50 5
    public function __construct(
51
        string $jsonSource,
52
        string $datePattern = self::DEFAULT_DATE_PATTERN,
53
        string $numberPattern = self::DEFAULT_NUMBER_PATTERN,
54
        string $timezoneId = self::DEFAULT_TIMEZONE_ID,
55
        string $localeCode = self::DEFAULT_LOCALE_CODE
56
    ) {
57 5
        $this->jsonSource = $jsonSource;
58 5
        $this->setOptions([
59 5
                self::PARAM_JSON_SOURCE         => $jsonSource,
60 5
                self::PARAM_JSON_DATE_PATTERN   => $datePattern,
61 5
                self::PARAM_JSON_NUMBER_PATTERN => $numberPattern,
62 5
                self::PARAM_JSON_TIMEZONE_ID    => $timezoneId,
63 5
                self::PARAM_JSON_LOCALE_CODE    => $localeCode
64
        ]);
65 5
    }
66
67 5
    public function setOptions(array $options): void
68
    {
69 5
        $options[self::PARAM_JSON_SOURCE] = $this->jsonSource;
70 5
        $this->options                    = $options;
71 5
    }
72
73 1
    public function getOptions(): array
74
    {
75 1
        return $this->options;
76
    }
77
78 4
    public function getDataSourceReportParams(): ReportParams
79
    {
80 4
        return new ReportParams($this->options);
81
    }
82
}
83