Passed
Push — trunk ( 780f52...305828 )
by SuperNova.WS
04:36
created

AllianceTitleList::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 9.4285
ccs 0
cts 4
cp 0
crap 6
1
<?php
2
/**
3
 * Created by Gorlum 28.11.2017 7:15
4
 */
5
6
namespace Alliance;
7
8
9
class AllianceTitleList {
10
11
  /**
12
   * @var Alliance $alliance
13
   */
14
  protected $alliance;
15
16
  /**
17
   * @var AllianceTitle[] $titles
18
   */
19
  protected $titles = [];
20
21
  public function __construct(Alliance $alliance) {
22
    $this->alliance = $alliance;
23
24
    if (!empty($this->alliance->titleList)) {
25
      $this->extractTitles();
26
    }
27
  }
28
29
  /**
30
   * @return AllianceTitle[]
31
   */
32
  public function getTitles() {
33
    return $this->titles;
34
  }
35
36
  /**
37
   * Compact all titles into one string
38
   *
39
   * @return string
40
   */
41
  public function __toString() {
42
    $result = [];
43
    foreach ($this->titles as $index => $title) {
44
      if ($index == Alliance::OWNER_INDEX) {
45
        continue;
46
      }
47
48
      $result[] = (string)$title;
49
    }
50
51
    return implode(';', $result);
52
  }
53
54
  /**
55
   * @return int[]
56
   */
57
  public function getWeights() {
58
    $result = [];
59
    foreach ($this->titles as $index => $title) {
60
      $result[$index] = $title->getWeight();
61
    }
62
63
    return $result;
64
  }
65
66
  /**
67
   * Updates title names and access rights with current ones
68
   */
69
  public function updateTitles() {
70
    $this->alliance->titleList = (string)$this;
71
  }
72
73
  protected function extractTitles() {
74
    $this->titles = [];
75
76
    $this->titles[Alliance::OWNER_INDEX] = new AllianceTitle($this->alliance);
77
    $this->titles[Alliance::OWNER_INDEX]->fromString(Alliance::OWNER_INDEX, '');
78
79
    $titleList = explode(';', $this->alliance->titleList);
80
    foreach ($titleList as $titleIndex => $titleString) {
81
      if (empty($titleString)) {
82
        continue;
83
      }
84
      $this->titles[$titleIndex] = new AllianceTitle($this->alliance);
85
      $this->titles[$titleIndex]->fromString($titleIndex, $titleString);
86
    }
87
88
    return $this->titles;
89
  }
90
91
  /**
92
   * @param $titleIndex
93
   *
94
   * @return AllianceTitle|null
95
   */
96
  public function getTitle($titleIndex) {
97
    return !empty($this->titles[$titleIndex]) ? $this->titles[$titleIndex] : null;
98
  }
99
100
101
  /**
102
   * @return array
103
   */
104
  public function asPtl() {
105
    $result = [];
106
    foreach ($this->titles as $title) {
107
      $result[] = [
108
        'INDEX'       => $title->index,
109
        'NAME'        => $title->name,
110
        'NAME_SAFE'   => \HelperString::htmlSafe($title->name),
111
        'RIGHTS'      => $title->rightsAsString(),
112
        'RIGHTS_TEXT' => \HelperString::htmlSafe($title->rightsAsString()),
113
      ];
114
    }
115
116
    return $result;
117
  }
118
119
}
120