Completed
Push — master ( 35e1c2...1a8f32 )
by Jakub
03:14
created

MenuItem::getRawLink()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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-read 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 array[] of [IMenuItemCondition, string] */
21
  protected $conditions = [];
22
  /** @var IMenuItemLinkRender[] */
23
  protected $linkRenders = [];
24
  
25
  public function __construct(string $link, string $text) {
26 1
    parent::__construct();
27 1
    $this->link = $link;
28 1
    $this->text = $text;
29 1
  }
30
  
31
  public function getLink(): string {
32 1
    $link = $this->link;
33 1
    foreach($this->linkRenders as $render) {
34 1
      if($render->isApplicable($this->link)) {
35 1
        $link =  $render->renderLink($this->link);
36 1
        break;
37
      }
38
    }
39 1
    return $link;
40
  }
41
  
42
  public function setLink(string $link) {
43 1
    $this->link = $link;
44 1
  }
45
46
  public function getRawLink(): string {
47 1
    return $this->link;
48
  }
49
50
  public function getText(): string {
51 1
    return $this->text;
52
  }
53
  
54
  public function setText(string $text) {
55 1
    $this->text = $text;
56 1
  }
57
  
58
  /**
59
   * @param mixed $parameter
60
   */
61
  public function addCondition(IMenuItemCondition $condition, $parameter): void {
62 1
    $this->conditions[$condition->getName()] = [$condition, $parameter];
63 1
  }
64
  
65
  public function addLinkRender(IMenuItemLinkRender $render): void {
66 1
    $this->linkRenders[$render->getName()] = $render;
67 1
  }
68
  
69
  public function isAllowed(): bool {
70 1
    foreach($this->conditions as $condition) {
71 1
      if(!$condition[0]->isAllowed($condition[1])) {
72 1
        return false;
73
      }
74
    }
75 1
    return true;
76
  }
77
}
78
?>