-
Notifications
You must be signed in to change notification settings - Fork 0
/
Marking.php
65 lines (56 loc) · 1.17 KB
/
Marking.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace ziguss\petrinet;
/**
* @author ziguss <yudoujia@163.com>
*/
class Marking
{
protected $places;
public function __construct()
{
$this->places = new \SplObjectStorage();
}
/**
* @return Place[]
*/
public function getPlaces()
{
$places = [];
foreach ($this->places as $place) {
$places[] = $place;
}
return $places;
}
/**
* @param Place $place
*
* @return int
*/
public function getTokens(Place $place)
{
if ($this->places->contains($place)) {
return $this->places[$place];
} else {
return 0;
}
}
/**
* @param Place $place
* @param int $tokens
*
* @return $this
*/
public function setTokens(Place $place, $tokens)
{
$tokens = (int) $tokens;
if ($tokens < 0) {
throw new \InvalidArgumentException('The tokens must be a non-negative integer.');
}
if ($tokens === 0) {
$this->places->detach($place);
} else {
$this->places[$place] = $tokens;
}
return $this;
}
}