TermChecklist::defineDataManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Leonidas\Library\Admin\Field;
4
5
use Leonidas\Library\Admin\Field\Data\PostTermDataManager;
6
use Leonidas\Library\Admin\Field\Formatting\TermsToIdsDataFormatter;
7
use Leonidas\Library\Admin\Field\Selection\TaxonomyChecklistItems;
8
use WebTheory\Saveyour\Contracts\Controller\FormFieldControllerInterface;
9
use WebTheory\Saveyour\Contracts\Data\FieldDataManagerInterface;
10
use WebTheory\Saveyour\Contracts\Field\FormFieldInterface;
11
use WebTheory\Saveyour\Contracts\Field\Selection\ChecklistItemsProviderInterface;
12
use WebTheory\Saveyour\Contracts\Formatting\DataFormatterInterface;
13
use WebTheory\Saveyour\Controller\Abstracts\AbstractField;
14
use WebTheory\Saveyour\Field\Type\Checklist;
15
16
class TermChecklist extends AbstractField implements FormFieldControllerInterface
17
{
18
    protected $taxonomy;
19
20
    protected $options;
21
22
    public function __construct(string $taxonomy, string $requestVar, array $options = [])
23
    {
24
        $this->taxonomy = $taxonomy;
25
        $this->options = $this->defineOptions($options);
26
27
        parent::__construct($requestVar);
28
    }
29
30
    protected function defineOptions(array $options)
31
    {
32
        return [
33
            'id' => $options['id'] ?? "wts--{$this->taxonomy}-checklist",
34
            'class' => $options['class'] ?? [],
35
        ];
36
    }
37
38
    protected function defineFormField(): ?FormFieldInterface
39
    {
40
        $options = $this->options;
41
42
        $checklist = new Checklist();
43
        $checklist->setSelectionProvider($this->createSelection())
44
            ->setId($options['id'])
45
            ->setClasslist($options['class'])
46
            ->addClass('thing');
47
48
        return $checklist;
49
    }
50
51
    protected function createSelection(): ChecklistItemsProviderInterface
52
    {
53
        return new TaxonomyChecklistItems($this->taxonomy);
54
    }
55
56
    protected function defineDataManager(): ?FieldDataManagerInterface
57
    {
58
        return new PostTermDataManager($this->taxonomy);
59
    }
60
61
    protected function defineDataFormatter(): ?DataFormatterInterface
62
    {
63
        return new TermsToIdsDataFormatter();
64
    }
65
66
    protected function defineFilters(): ?array
67
    {
68
        return ['sanitize_text_field'];
69
    }
70
}
71