Completed
Push — master ( 25ba12...d7e08b )
by Jakub
02:28
created

CharacterEffect::getAllowedStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nexendrie\Utils\Constants,
7
    Symfony\Component\OptionsResolver\OptionsResolver;
8
9
/**
10
 * Data structure for effect on character
11
 *
12
 * @author Jakub Konečný
13
 * @property-read string $id
14
 * @property-read string $type
15
 * @property-read string $stat
16
 * @property-read int $value
17
 * @property-read string $source
18
 * @property int|string $duration
19
 */
20 1
class CharacterEffect {
21 1
  use \Nette\SmartObject;
22
  
23
  public const SOURCE_PET = "pet";
24
  public const SOURCE_SKILL = "skill";
25
  public const SOURCE_EQUIPMENT = "equipment";
26
  public const DURATION_COMBAT = "combat";
27
  public const DURATION_FOREVER = "forever";
28
  
29
  /** @var string */
30
  protected $id;
31
  /** @var string */
32
  protected $type;
33
  /** @var string */
34
  protected $stat = "";
35
  /** @var int */
36
  protected $value = 0;
37
  /** @var string */
38
  protected $source;
39
  /** @var int|string */
40
  protected $duration;
41
  
42
  public function __construct(array $effect) {
43 1
    $allStats = ["id", "type", "source", "value", "duration", "stat",];
44 1
    $resolver = new OptionsResolver();
45 1
    $resolver->setDefined($allStats);
46 1
    $resolver->setRequired($allStats);
47 1
    $resolver->setAllowedTypes("id", "string");
48 1
    $resolver->setAllowedTypes("type", "string");
49 1
    $resolver->setAllowedValues("type", function(string $value) {
50 1
      return in_array($value, $this->getAllowedTypes(), true);
51 1
    });
52 1
    $resolver->setAllowedTypes("stat", "string");
53 1
    $resolver->setDefault("stat", "");
54 1
    $resolver->setAllowedValues("stat", function(string $value) {
55 1
      return $value === "" OR in_array($value, $this->getAllowedStats(), true);
56 1
    });
57 1
    $resolver->setAllowedTypes("source", "string");
58 1
    $resolver->setAllowedValues("source", function(string $value) {
59 1
      return in_array($value, $this->getAllowedSources(), true);
60 1
    });
61 1
    $resolver->setAllowedTypes("value", "integer");
62 1
    $resolver->setDefault("value", 0);
63 1
    $resolver->setAllowedTypes("duration", ["string", "integer"]);
64 1
    $resolver->setAllowedValues("duration", function($value) {
65 1
      return (in_array($value, $this->getDurations(), true)) OR ($value > 0);
66 1
    });
67 1
    $effect = $resolver->resolve($effect);
68 1
    if(!in_array($effect["type"], SkillSpecial::NO_STAT_TYPES, true) AND $effect["stat"] === "") {
69
      throw new \InvalidArgumentException("The option stat with value '' is invalid.");
70
    }
71 1
    $this->id = $effect["id"];
72 1
    $this->type = $effect["type"];
73 1
    $this->stat = $effect["stat"];
74 1
    $this->value = $effect["value"];
75 1
    $this->source = $effect["source"];
76 1
    $this->duration = $effect["duration"];
77 1
  }
78
  
79
  protected function getAllowedStats(): array {
80 1
    $stats = Constants::getConstantsValues(SkillSpecial::class, "STAT_");
81 1
    $stats = array_merge($stats, ["strength", "dexterity", "constitution", "intelligence", "charisma",]);
82 1
    return $stats;
83
  }
84
  
85
  /**
86
   * @return string[]
87
   */
88
  protected function getAllowedSources(): array {
89 1
    return Constants::getConstantsValues(static::class, "SOURCE_");
90
  }
91
  
92
  /**
93
   * @return string[]
94
   */
95
  protected function getAllowedTypes(): array {
96 1
    return Constants::getConstantsValues(SkillSpecial::class, "TYPE_");
97
  }
98
  
99
  /**
100
   * @return string[]
101
   */
102
  protected function getDurations(): array {
103 1
    return Constants::getConstantsValues(static::class, "DURATION_");
104
  }
105
  
106
  public function getId(): string {
107 1
    return $this->id;
108
  }
109
  
110
  public function getType(): string {
111 1
    return $this->type;
112
  }
113
  
114
  public function getStat(): string {
115 1
    return $this->stat;
116
  }
117
  
118
  public function getValue(): int {
119 1
    return $this->value;
120
  }
121
  
122
  public function getSource(): string {
123 1
    return $this->source;
124
  }
125
  
126
  /**
127
   * @return int|string
128
   */
129
  public function getDuration() {
130 1
    return $this->duration;
131
  }
132
  
133
  /**
134
   * @param string|int $value
135
   * @throws \InvalidArgumentException
136
   */
137
  public function setDuration($value) {
138
    if(!is_int($value) AND !in_array($value, $this->getDurations(), true)) {
139
      throw new \InvalidArgumentException("Invalid value set to CharacterEffect::\$duration. Expected string or integer.");
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 123 characters
Loading history...
140
    }
141
    $this->duration = $value;
142
  }
143
}
144
?>