Completed
Push — master ( c8e891...facd1d )
by Jens
08:13
created

SuggestionResult::fromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Product;
7
8
use Commercetools\Core\Model\Common\JsonObject;
9
10
/**
11
 * @package Commercetools\Core\Model\Product
12
 *
13
 * @method LocalizedSuggestionCollection getSearchKeywords()
14
 * @method SuggestionResult setSearchKeywords(LocalizedSuggestionCollection $searchKeywords = null)
15
 */
16
class SuggestionResult extends JsonObject
17
{
18 1
    public function fieldDefinitions()
19
    {
20
        return [
21 1
            'searchKeywords' => [static::TYPE => '\Commercetools\Core\Model\Product\LocalizedSuggestionCollection']
22
        ];
23
    }
24
25 2
    public static function fromArray(array $data, $context = null)
26
    {
27 2
        $result = [];
28 2
        foreach ($data as $key => $value) {
29 1
            $parts = explode('.', $key, 2);
30 1
            if ($parts[0] == 'searchKeywords') {
31 1
                $result['searchKeywords'][$parts[1]] = $value;
32
            } else {
33 1
                $result[$key] = $value;
34
            }
35
        }
36
37 2
        return parent::fromArray($result, $context);
38
    }
39
40 2
    public function toArray()
41
    {
42 2
        $data = parent::toArray();
43
        
44 2
        if (isset($data['searchKeywords']) && is_array($data['searchKeywords'])) {
45 1
            foreach ($data['searchKeywords'] as $locale => $keywords) {
46 1
                $data['searchKeywords.' . $locale] = $keywords;
47
            }
48 1
            unset($data['searchKeywords']);
49
        }
50
51 2
        return $data;
52
    }
53
}
54