OrganizationType   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 0
loc 90
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A displayName() 0 4 1
A getInputHtml() 0 11 1
A getSettingsHtml() 0 9 1
A getContentColumnType() 0 4 1
A normalizeValue() 0 12 3
A serializeValue() 0 8 2
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\records\OrganizationType as OrganizationTypeRecord;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class OrganizationType extends Field
22
{
23
    /**
24
     * @var string
25
     */
26
    public $defaultSelectionLabel = '-- Select Organization Type --';
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public static function displayName(): string
32
    {
33
        return Craft::t('organizations', 'Organization Type');
34
    }
35
36
    /**
37
     * @inheritdoc
38
     * @throws \Twig\Error\LoaderError
39
     * @throws \Twig\Error\RuntimeError
40
     * @throws \Twig\Error\SyntaxError
41
     */
42
    public function getInputHtml($value, ElementInterface $element = null): string
43
    {
44
        return Craft::$app->getView()->renderTemplate(
45
            'organizations/_components/fieldtypes/OrganizationType/input',
46
            [
47
                'field' => $this,
48
                'value' => $value,
49
                'element' => $element
50
            ]
51
        );
52
    }
53
54
    /**
55
     * @inheritdoc
56
     * @throws \Twig\Error\LoaderError
57
     * @throws \Twig\Error\RuntimeError
58
     * @throws \Twig\Error\SyntaxError
59
     */
60
    public function getSettingsHtml()
61
    {
62
        return Craft::$app->getView()->renderTemplate(
63
            'organizations/_components/fieldtypes/OrganizationType/settings',
64
            [
65
                'field' => $this
66
            ]
67
        );
68
    }
69
70
    /**
71
     * @inheritdoc
72
     * @throws \yii\base\Exception
73
     */
74
    public function getContentColumnType(): string
75
    {
76
        return Db::getNumericalColumnType();
77
    }
78
79
    /**
80
     * @inheritdoc
81
     *
82
     * @return OrganizationTypeRecord|null
83
     */
84
    public function normalizeValue($value, ElementInterface $element = null)
85
    {
86
        if ($value instanceof OrganizationTypeRecord) {
87
            return $value;
88
        }
89
90
        if (is_numeric($value)) {
91
            return OrganizationTypeRecord::findOne($value);
92
        }
93
94
        return null;
95
    }
96
97
    /**
98
     * @param $value
99
     * @param ElementInterface|null $element
100
     * @return array|int|mixed|null|string
101
     */
102
    public function serializeValue($value, ElementInterface $element = null)
103
    {
104
        if ($value instanceof OrganizationTypeRecord) {
105
            return $value->getId();
106
        }
107
108
        return null;
109
    }
110
}
111