Passed
Push — master ( 7dbefe...3850c0 )
by Jakub
02:00
created

MenuItem::setRawLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Menu;
5
6
/**
7
 * Menu item
8
 *
9
 * @author Jakub Konečný
10
 * @property string $link
11
 * @property string $rawLink
12
 * @property string $text
13
 * @property-read bool $allowed
14
 */
15 1
class MenuItem extends Collection {
16
  /** @var string */
17
  protected $text;
18
  /** @var string */
19
  protected $link;
20
  /** @var string */
21
  protected $rawLink;
22
  /** @var array[] of [IMenuItemCondition, string] */
23
  protected $conditions = [];
24
  /** @var IMenuItemLinkRender[] */
25
  protected $linkRenders = [];
26
  
27
  public function __construct(string $link, string $text) {
28 1
    parent::__construct();
29 1
    $this->setRawLink($link);
30 1
    $this->text = $text;
31 1
  }
32
33
  /**
34
   * @deprecated Access the property directly
35
   */
36
  public function getLink(): string {
37 1
    $link = $this->link;
38 1
    foreach($this->linkRenders as $render) {
39 1
      if($render->isApplicable($this->link)) {
40 1
        $link =  $render->renderLink($this->link);
41 1
        break;
42
      }
43
    }
44 1
    return $link;
45
  }
46
47
  /**
48
   * @deprecated Access the property directly
49
   */
50
  public function setLink(string $link): void {
51 1
    $this->setRawLink($link);
52 1
  }
53
54
  /**
55
   * @deprecated Access the property directly
56
   */
57
  public function getRawLink(): string {
58 1
    return $this->link;
59
  }
60
61
  protected function setRawLink(string $rawLink): void {
62 1
    $this->rawLink = $this->link = $rawLink;
63 1
  }
64
65
  /**
66
   * @deprecated Access the property directly
67
   */
68
  public function getText(): string {
69 1
    return $this->text;
70
  }
71
72
  /**
73
   * @deprecated Access the property directly
74
   */
75
  public function setText(string $text): void {
76 1
    $this->text = $text;
77 1
  }
78
  
79
  /**
80
   * @param mixed $parameter
81
   */
82
  public function addCondition(IMenuItemCondition $condition, $parameter): void {
83 1
    $this->conditions[$condition->getName()] = [$condition, $parameter];
84 1
  }
85
  
86
  public function addLinkRender(IMenuItemLinkRender $render): void {
87 1
    $this->linkRenders[$render->getName()] = $render;
88 1
  }
89
90
  /**
91
   * @deprecated Access the property directly
92
   */
93
  public function isAllowed(): bool {
94 1
    foreach($this->conditions as $condition) {
95 1
      if(!$condition[0]->isAllowed($condition[1])) {
96 1
        return false;
97
      }
98
    }
99 1
    return true;
100
  }
101
}
102
?>