Completed
Push — master ( ae5b26...262c7c )
by Evan
10s
created

LabelsCollection::replaceWithForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Silk\Support;
4
5
use Illuminate\Support\Collection;
6
7
class LabelsCollection extends Collection
8
{
9
    /**
10
     * The label form (singular, plural, or otherwise)
11
     * @var string
12
     */
13
    protected $form;
14
15
    /**
16
     * Set the form.
17
     *
18
     * @param string $label  The label form to use for this collection
19
     */
20
    public function setForm($label)
21
    {
22
        $this->form = $label;
23
24
        return $this;
25
    }
26
27
    /**
28
     * Get a new collection with replaced placeholders in items using the provided form.
29
     *
30
     * @return $this
31
     */
32
    public function replaced()
33
    {
34
        return $this->map(function ($label) {
35
            return $this->replaceWithForm($label);
36
        });
37
    }
38
39
    /**
40
     * Replace all placeholders in the label with the given label form.
41
     *
42
     * @param  string $label The label to make replacements in
43
     *
44
     * @return string   The label after replacements have been made
45
     */
46
    protected function replaceWithForm($label)
47
    {
48
        return sprintf($label, $this->form);
49
    }
50
51
    /**
52
     * Get a specific label, after replacements have been made.
53
     *
54
     * @param  string $key     The label key
55
     * @param  string $default The default value, if no label if found for key
56
     *
57
     * @return string The label after replacements have been made
58
     */
59
    public function get($key, $default = null)
60
    {
61
        $label = parent::get($key, $default);
62
63
        return $this->replaceWithForm($label);
64
    }
65
66
    /**
67
     * Make replacements and return the collection as an array.
68
     *
69
     * @return array
70
     */
71
    public function toArray()
72
    {
73
        return $this->replaced()->all();
74
    }
75
}
76