InlineKeyboardMarkup::addRow()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
4
namespace Sysbot\Telegram\ExtendedTypes;
5
6
7
use Sysbot\Telegram\Exceptions\InvalidArgumentException;
8
use Sysbot\Telegram\Types\InlineKeyboardButton;
9
10
11
/**
12
 * Trait InlineKeyboardMarkup
13
 * @package Sysbot\Telegram\ExtendedTypes
14
 */
15
trait InlineKeyboardMarkup
16
{
17
18
    /**
19
     * @param int $row
20
     * @param InlineKeyboardButton $button
21
     * @param bool $first
22
     * @return $this
23
     */
24
    public function addButton(int $row, InlineKeyboardButton $button, bool $first = false): static
25
    {
26
        if (empty($this->inlineKeyboard[$row])) {
27
            throw new InvalidArgumentException('Row not found');
28
        }
29
        if ($first) {
30
            array_unshift($this->inlineKeyboard[$row], $button);
31
            return $this;
32
        }
33
        array_push($this->inlineKeyboard[$row], $button);
34
        return $this;
35
    }
36
37
    /**
38
     * @param int $row
39
     * @param int $index
40
     * @return $this
41
     */
42
    public function removeButton(int $row, int $index): static
43
    {
44
        if (empty($this->inlineKeyboard[$row][$index] ?? null)) {
45
            return $this;
46
        }
47
        unset($this->inlineKeyboard[$row][$index]);
48
        $this->inlineKeyboard[$row] = array_values($this->inlineKeyboard[$row]);
49
        return $this;
50
    }
51
52
    /**
53
     * @param InlineKeyboardButton[] $buttons
54
     * @param bool $first
55
     * @return $this
56
     */
57
    public function addRow(array $buttons, bool $first = false): static
58
    {
59
        if ($first) {
60
            array_unshift($this->inlineKeyboard, $buttons);
61
            return $this;
62
        }
63
        array_push($this->inlineKeyboard, $buttons);
64
        return $this;
65
    }
66
67
    /**
68
     * @param int $index
69
     * @return $this
70
     */
71
    public function removeRow(int $index): static
72
    {
73
        if (empty($this->inlineKeyboard[$index])) {
74
            return $this;
75
        }
76
        unset($this->inlineKeyboard[$index]);
77
        return $this;
78
    }
79
80
}