Completed
Push — master ( 3e605b...8e8897 )
by Nassif
07:14
created

Spreadsheet::setLocales()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Nikaia\TranslationSheet;
4
5
use Illuminate\Support\Str;
6
use Nikaia\TranslationSheet\Util;
7
use Nikaia\TranslationSheet\Client\Api;
8
use Nikaia\TranslationSheet\Sheet\Styles;
9
use Nikaia\TranslationSheet\Sheet\TranslationsSheetCoordinates;
10
11
class Spreadsheet
12
{
13
    /** @var string */
14
    protected $id;
15
16
    /** @var array */
17
    protected $locales;
18
19
    /** @var array */
20
    protected $translations;
21
22
    /** @var Api */
23
    protected $api;
24
25 18
    public function __construct($id, $locales, $api = null)
26
    {
27 18
        $this->id = $id;
28 18
        $this->locales = $locales;
29 18
        $this->api = $api ?: app(Api::class);
30 18
    }
31
32 3
    public function setLocales($locales)
33
    {
34 3
        $this->locales = $locales;
35
36 3
        return $this;
37
    }
38
39 17
    public function setTranslations($translations)
40
    {
41 17
        $this->translations = $translations;
42
43 17
        return $this;
44
    }
45
46 1
    public function getTranslations()
47
    {
48 1
        return $this->translations;
49
    }
50
51 3
    public function getTranslationsCount()
52
    {
53 3
        return count($this->translations);
54
    }
55
56 3
    public function getId()
57
    {
58 3
        return $this->id;
59
    }
60
61 1
    public function getUrl()
62
    {
63 1
        return 'https://docs.google.com/spreadsheets/d/'.$this->id;
64
    }
65
66 10
    public function getLocales()
67
    {
68 10
        return $this->locales;
69
    }
70
71 4
    public function getLocalesCount()
72
    {
73 4
        return count($this->locales);
74
    }
75
76 7
    public function getHeader()
77
    {
78 7
        return array_merge(
79 7
            array_merge(['Full key'], $this->getLocales()),
80 7
            ['Namespace', 'Group', 'Key', 'Source file']
81 7
        );
82
    }
83
84
    public function getCamelizedHeader()
85
    {
86 2
        return array_map(function ($item) {
87 2
            if (in_array($item, Util::asArray($this->getLocales()))) {
88 2
                return $item;
89
            }
90
91 2
            return Str::camel($item);
92 2
        }, $this->getHeader());
93
    }
94
95 4
    public function getHeaderColumnsCount()
96
    {
97 4
        $header = $this->getHeader();
98
99 4
        return count($header);
100
    }
101
102 3
    public function getHeaderRowsCount()
103
    {
104 3
        return 1;
105
    }
106
107
    /**
108
     * @param $sheetId
109
     * @param $sheetTitle
110
     *
111
     * @return TranslationsSheetCoordinates
112
     */
113 1
    public function translationsEmptySheetCoordinates($sheetId, $sheetTitle)
114
    {
115 1
        return TranslationsSheetCoordinates::emptySheet(
116 1
            $this->getHeaderColumnsCount(),
117 1
            $this->getLocalesCount(),
118 1
            $this->getHeaderColumnsCount()
119 1
        )->setSheetId($sheetId)->setSheetTitle($sheetTitle);
120
    }
121
122
    /**
123
     * @param $sheetId
124
     * @param $sheetTitle
125
     * @return TranslationsSheetCoordinates
126
     */
127 2
    public function translationsSheetCoordinates($sheetId, $sheetTitle)
128
    {
129 2
        return TranslationsSheetCoordinates::sheetWithData(
130 2
            $this->getTranslationsCount(),
131 2
            $this->getHeaderColumnsCount(),
132 2
            $this->getLocalesCount(),
133 2
            $this->getHeaderRowsCount()
134 2
        )->setSheetId($sheetId)->setSheetTitle($sheetTitle);
135
    }
136
137 1
    public function sheetStyles()
138
    {
139 1
        return new Styles;
140
    }
141
142
    /**
143
     * Return api instance initialized with the spreadsheet ID.
144
     *
145
     * @return Api
146
     */
147 1
    public function api()
148
    {
149 1
        return $this->api->setSpreadsheetId($this->getId());
150
    }
151
}
152