Completed
Push — master ( f28ea6...b7e6d7 )
by Nate
11:19 queued 09:19
created

OrganizationType::normalizeValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\fields;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use craft\base\Field;
14
use craft\helpers\Db;
15
use flipbox\organizations\Organizations;
16
use flipbox\organizations\records\OrganizationType as OrganizationTypeRecord;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class OrganizationType extends Field
23
{
24
    /**
25
     * @inheritdoc
26
     */
27
    public static function displayName(): string
28
    {
29
        return Craft::t('organizations', 'Organization Type');
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function getInputHtml($value, ElementInterface $element = null): string
36
    {
37
        return Craft::$app->getView()->renderTemplate(
38
            'organizations/_components/fieldtypes/OrganizationType/input',
39
            [
40
                'field' => $this,
41
                'value' => $value,
42
                'element' => $element
43
            ]
44
        );
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function getContentColumnType(): string
51
    {
52
        return Db::getNumericalColumnType();
53
    }
54
55
    /**
56
     * @inheritdoc
57
     *
58
     * @return OrganizationTypeRecord|null
59
     */
60
    public function normalizeValue($value, ElementInterface $element = null)
61
    {
62
        if ($value instanceof OrganizationTypeRecord) {
63
            return $value;
64
        }
65
66
        if (is_numeric($value)) {
67
            return Organizations::getInstance()->getOrganizationTypes()->find($value);
68
        }
69
70
        return null;
71
    }
72
73
    /**
74
     * @param $value
75
     * @param ElementInterface|null $element
76
     * @return array|int|mixed|null|string
77
     */
78
    public function serializeValue($value, ElementInterface $element = null)
79
    {
80
        if ($value instanceof OrganizationTypeRecord) {
81
            return $value->getId();
82
        }
83
84
        return null;
85
    }
86
}
87