Prevent skill sets from sharing their skills

Each SkillSet should provide his own skills and should not share the
skills with other skill sets.
Therefore use an instance property instead of static.
This commit is contained in:
Daniel Siepmann 2020-10-19 15:35:58 +02:00
parent 195d37986a
commit 59f1ffab4a
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
2 changed files with 21 additions and 6 deletions

View file

@ -30,6 +30,11 @@ class SkillSet
*/ */
private $data; private $data;
/**
* @var array
*/
private $skills = [];
private function __construct(array $data) private function __construct(array $data)
{ {
$this->data = $data; $this->data = $data;
@ -55,17 +60,15 @@ class SkillSet
*/ */
public function getSkills(): array public function getSkills(): array
{ {
static $skills = []; if ($this->skills !== []) {
return $this->skills;
if ($skills !== []) {
return $skills;
} }
foreach ($this->data['skills'] as $skill) { foreach ($this->data['skills'] as $skill) {
$skills[] = Skill::createFromJson(json_encode($skill)); $this->skills[] = Skill::createFromJson(json_encode($skill));
} }
return $skills; return $this->skills;
} }
public function toArray(): array public function toArray(): array

View file

@ -101,6 +101,18 @@ class SkillSetTest extends TestCase
static::assertSame($skillsFirstCall, $skillsSecondCall); static::assertSame($skillsFirstCall, $skillsSecondCall);
} }
/**
* @test
*/
public function twoDifferentInstancesProvideTheirOwnSkills()
{
$subject1 = SkillSet::createFromJson('{"skills":[{"uid":90,"title":"Example title 1"},{"goals":"<p>Example goals</p>","uid":91,"title":"Example title 2"}]}');
$subject2 = SkillSet::createFromJson('{"skills":[{"uid":80,"title":"Example title 10"},{"goals":"<p>Example goals</p>","uid":81,"title":"Example title 11"}]}');
static::assertSame(90, $subject1->getSkills()[0]->getId());
static::assertSame(80, $subject2->getSkills()[0]->getId());
}
/** /**
* @test * @test
*/ */