Completed
Push — master ( 22fb87...ee9a58 )
by Rory
01:10 queued 11s
created

CustomTaxonomy::getArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Brightoak\WordPressTools;
4
5
use Illuminate\Support\Str;
6
use Brightoak\WordPressTools\Exceptions\InvalidArgumentException;
7
8
class CustomTaxonomy
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $taxonomy;
14
    /**
15
     * @var Str
16
     */
17
    protected $stringHelper;
18
19
    protected $objectTypes = [];
20
21
    protected $options = [
22
        'public' => true,
23
        'show_ui' => true,
24
    ];
25
26
    protected $singularLabel = null;
27
28
    private function __construct(string $name)
29
    {
30
        $this->stringHelper = new Str;
31
        $this->taxonomy = $this->stringHelper::singular($name);
32
    }
33
34 View Code Duplication
    public static function init(string $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        if (strlen($name) >= 1 && strlen($name) < 20) {
37
            return new self($name);
38
        }
39
        throw new InvalidArgumentException('A Custom Taxonomy name must be between 1 and 20 characters.');
40
    }
41
42
    public function setObjectTypes(...$objects)
43
    {
44
        $this->objectTypes = $objects;
45
46
        return $this;
47
    }
48
49
50
    public function getObjectTypes()
51
    {
52
        return $this->objectTypes;
53
    }
54
55
56
    public function setSingularLabel(string $value)
57
    {
58
        $this->singularLabel = $value;
59
60
        return $this;
61
    }
62
63
    protected function getSingularLabel()
64
    {
65
        return $this->singularLabel;
66
    }
67
68
69
70
71
    public function getOptions()
72
    {
73
        return $this->options;
74
    }
75
76
77
78
    protected function calculateLabels($calculatedLabel)
79
    {
80 View Code Duplication
        if($this->getSingularLabel() === null){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
            $singularLabel = str_replace('_', ' ', $calculatedLabel);
82
            $singularLabel = str_replace('-', ' ', $singularLabel);
83
            $plural = $this->stringHelper::title($this->stringHelper::plural($singularLabel));
84
            $singularLabel = $this->stringHelper::title($singularLabel);
85
        } else {
86
            $singularLabel = $this->getSingularLabel();
87
            $plural = $this->stringHelper::plural($singularLabel);
88
        }
89
90
        return [
91
            'name' => $plural,
92
            'singular_name' => __($singularLabel, 'brightoak'),
93
            'search_items'               => __( "Search $plural", 'brightoak' ),
94
            'popular_items'              => __( "Popular $plural", 'brightoak' ),
95
            'all_items'                  => __( "All $plural", 'brightoak' ),
96
            'parent_item'                => null,
97
            'parent_item_colon'          => null,
98
            'edit_item'                  => __( "Edit $singularLabel", 'brightoak' ),
99
            'update_item'                => __( "Update $singularLabel", 'brightoak' ),
100
            'add_new_item'               => __( "Add New $singularLabel", 'brightoak' ),
101
            'new_item_name'              => __( "New $singularLabel Name", 'brightoak' ),
102
            'separate_items_with_commas' => __( "Separate $plural with commas", 'brightoak' ),
103
            'add_or_remove_items'        => __( "Add or remove $plural", 'brightoak' ),
104
            'choose_from_most_used'      => __( "Choose from the most used $plural", 'brightoak' ),
105
            'not_found'                  => __( "No $plural found.", 'brightoak' ),
106
            'menu_name'                  => __( "$plural", 'brightoak' ),
107
        ];
108
    }
109
110
    public function setOptions(array $options = [])
111
    {
112
        if (! empty($options)) {
113
            $this->options = array_merge($this->options, $options);
114
        }
115
116
        return $this;
117
    }
118
119
    protected function getArgs() : array
120
    {
121
        $args = [];
122
        $args['labels'] = $this->calculateLabels($this->taxonomy);
123
        // This is the default
124
        $args['rewrite'] = ['slug' => str_replace('_', '-', $this->taxonomy)];
125
        // Then we over write it from user provided settings
126
        $args = array_merge($args, $this->options);
127
128
        return $args;
129
    }
130
131
    public function register()
132
    {
133
        // function_exists wrapper here simply to make phpunit testing easier
134
        if (function_exists('register_post_type')) {
135
            register_taxonomy($this->taxonomy, $this->objectTypes, $this->getArgs());
136
        }
137
138
        return [$this->taxonomy, $this->objectTypes, $this->getArgs()];
139
    }
140
}
141