1: <?php
2: namespace Yep\Stopwatch;
3:
4: class Stopwatch {
5: /** @var null|string */
6: protected $name;
7:
8: /** @var Event[] */
9: protected $events = [];
10:
11: public function __construct($name = null) {
12: $this->name = $name;
13: }
14:
15: /**
16: * Returns the stopwatch name
17: *
18: * @return null|string
19: */
20: public function getName() {
21: return $this->name;
22: }
23:
24: /**
25: * Starts the new lap in the event
26: *
27: * @param string $name
28: * @param null|string $group
29: * @return Event
30: */
31: public function start($name, $group = null) {
32: if (empty($this->events[$name])) {
33: $this->events[$name] = new Event($name, $group);
34: }
35:
36: return $this->getEvent($name)->startLap();
37: }
38:
39: /**
40: * Stops the latest lap in the event
41: *
42: * @param string $name
43: * @return Event
44: */
45: public function stop($name) {
46: return $this->getEvent($name)->stopLap();
47: }
48:
49: /**
50: * Stops the latest lap in the event and star new one
51: *
52: * @param string $name
53: * @return Event
54: */
55: public function lap($name) {
56: return $this->getEvent($name)->stopLap()->startLap();
57: }
58:
59: /**
60: * Returns exists
61: *
62: * @param string $name
63: * @return Event
64: * @throws StopwatchEventDoesntExistException
65: */
66: public function getEvent($name) {
67: if (empty($this->events[$name])) {
68: throw new StopwatchEventDoesntExistException(sprintf('Event with name "%s" doesn\'t exist.', $name));
69: }
70:
71: return $this->events[$name];
72: }
73:
74: /**
75: * Returns the stopwatch events
76: *
77: * @return Event[]
78: */
79: public function getEvents() {
80: return $this->events;
81: }
82: }
83: