Fibinger Ádám
2019-10-10 888906fd2d4b12b1e5fd315092a7c8d0ef47a098
commit | author | age
888906 1 <?php
2
3 namespace EOG\Models;
4
5 class Stripe
6 {
7     const TEAM_BLUE = 'blue';
8     const TEAM_ORANGE = 'orange';
9
10     private $allowedColors = [self::TEAM_BLUE, self::TEAM_ORANGE];
11     private $allowedStripeClasses = ['team', 'simple'];
12     private $allowedOperators = [
13         'alibi', 'amaru', 'ash', 'bandit', 'blackbeard', 'blitz', 'buck', 'capitao', 'castle',
14         'caveira', 'clash', 'doc', 'dokkaebi', 'echo', 'ela', 'finka', 'frost', 'fuze',
15         'glaz', 'goyo', 'gridlock', 'hibana', 'iq', 'jackal', 'jager', 'kaid', 'kapkan',
16         'lesion', 'lion', 'maestro', 'maverick', 'mira', 'montagne', 'mozzie', 'mute', 'nokk',
17         'nomad', 'pulse', 'rook', 'sledge', 'smoke', 'tachanka', 'thatcher', 'thermite', 'twitch',
18         'valkyrie', 'vigil', 'warden', 'ying', 'zofia'];
19     protected $state = [
20         'stripe' => [
21             'class' => 'team',
22         ],
23         'cup' => [
24             'number' => '#75',
25             'name' => '5on5 Open Cup',
26         ],
27         'team' => [
28             'orange' =>
29                 [
30                     'name' => 'Narancs Csapat',
31                     'ban' => [
32                         'mira',
33                         'jackal'
34                     ]
35                 ],
36             'blue' => [
37                 'name' => 'Kék csapat',
38                 'ban' => [
39                     'rook',
40                     'blitz'
41                 ]
42             ]
43         ]
44     ];
45
46     public function setClass(string $class): self
47     {
48         if (!in_array($class, $this->allowedStripeClasses)) {
49             throw new \InvalidArgumentException("Given class " . $class . " not allowed. Allowed classes: " . implode(', ', $this->allowedStripeClasses));
50         }
51
52         if ($class === 'simple') {
53             $this->setSimpleStripe();
54         }
55
56         $this->state['stripe']['class'] = $class;
57
58         return $this;
59     }
60
61     private function setSimpleStripe()
62     {
63         unset($this->state['team']);
64     }
65
66     public function setCup(string $number, string $name)
67     {
68         $this->state['cup']['number'] = $number;
69         $this->state['cup']['name'] = $name;
70     }
71
72     /**
73      * @param string $color Csapat szín validáció
74      *
75      * Dob egy Exception-t ha nem orange / blue
76      */
77     private function testColor(string $color)
78     {
79         if (!in_array($color, $this->allowedColors)) {
80             throw new \InvalidArgumentException("Given colour " . $team_color . " not allowed. Allowed colours: " . implode(', ', $this->allowedColors));
81         }
82     }
83
84     public function setTeamName(string $team_color = self::TEAM_BLUE, $name)
85     {
86         $this->testColor($team_color);
87         $this->state['team'][$team_color]['name'] = $name;
88     }
89
90     public function addTeamBan(string $team_color = self::TEAM_BLUE, $operator)
91     {
92         $this->testColor($team_color);
93         if (!in_array($operator, $this->allowedOperators)) {
94             //FIXME: védő és támadó operátorok külön
95             throw new \InvalidArgumentException("Given operator not allowed:  " . $operator . " Allowed operators: " . implode(', ', $this->allowedOperators));
96         }
97     }
98
99     public function __get($name)
100     {
101         if ($this->__isset($name)) {
102             return $this->state[$name];
103         }
104
105         return null;
106     }
107
108     public function __isset($name)
109     {
110         return isset($this->state[$name]);
111     }
112
113     public function loadJson(string $json)
114     {
115         $state = json_decode($json);
116
117         if (!empty($state['stripe']["class"])) {
118             $this->setClass($state['stripe']["class"]);
119         }
120
121         if (!empty($state['cup']['name']) && !empty($state['cup']['number'])) {
122             $this->setCup($state['cup']['number'], $state['cup']['name']);
123         }
124
125     }
126 }