Completed
Pull Request — master (#114)
by Bart
01:45
created

Matrix::getRecordDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Fields;
4
5
use Craft;
6
use craft\base\Model;
7
use NerdsAndCompany\Schematic\Schematic;
8
use NerdsAndCompany\Schematic\Converters\Base\Field;
9
10
/**
11
 * Schematic Matrix Converter.
12
 *
13
 * Sync Craft Setups.
14
 *
15
 * @author    Nerds & Company
16
 * @copyright Copyright (c) 2015-2018, Nerds & Company
17
 * @license   MIT
18
 *
19
 * @see      http://www.nerds.company
20
 */
21
class Matrix extends Field
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function getRecordDefinition(Model $record): array
27
    {
28
        $definition = parent::getRecordDefinition($record);
29
        $definition['blockTypes'] = Craft::$app->controller->module->modelMapper->export($record->getBlockTypes());
30
31
        return $definition;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function saveRecord(Model $record, array $definition): bool
38
    {
39
        if (parent::saveRecord($record, $definition)) {
40
            if (array_key_exists('blockTypes', $definition)) {
41
                Craft::$app->controller->module->modelMapper->import($definition['blockTypes'], $record->getBlockTypes(), ['fieldId' => $record->id]);
42
            }
43
44
            return true;
45
        }
46
47
        return false;
48
    }
49
}
50