1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JhFlexiTime\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as ORM; |
6
|
|
|
use ZfcUser\Entity\UserInterface; |
7
|
|
|
use DateTime; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class RunningBalance |
12
|
|
|
* @package JhFlexiTime\Entity |
13
|
|
|
* @author Aydin Hassan <[email protected]> |
14
|
|
|
* @ORM\Entity |
15
|
|
|
* @ORM\Table(name="running_balance") |
16
|
|
|
*/ |
17
|
|
|
class RunningBalance implements JsonSerializable |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
* |
22
|
|
|
* @ORM\Id |
23
|
|
|
* @ORM\Column(type="integer") |
24
|
|
|
* @ORM\GeneratedValue(strategy="AUTO") |
25
|
|
|
*/ |
26
|
|
|
protected $id = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @ORM\ManyToOne(targetEntity="JhUser\Entity\User") |
30
|
|
|
*/ |
31
|
|
|
protected $user = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
* |
36
|
|
|
* @ORM\Column(type="string", length=255, nullable=false) |
37
|
|
|
*/ |
38
|
|
|
protected $balance = 0; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set Defaults |
42
|
|
|
*/ |
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$this->date = new DateTime(); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return int |
50
|
|
|
*/ |
51
|
|
|
public function getId() |
52
|
|
|
{ |
53
|
|
|
return $this->id; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* |
58
|
|
|
* @param \ZfcUser\Entity\UserInterface $user |
59
|
|
|
* @return \JhFlexiTime\Entity\RunningBalance |
60
|
|
|
*/ |
61
|
|
|
public function setUser(UserInterface $user) |
62
|
|
|
{ |
63
|
|
|
$this->user = $user; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return \ZfcUser\Entity\UserInterface |
69
|
|
|
*/ |
70
|
|
|
public function getUser() |
71
|
|
|
{ |
72
|
|
|
return $this->user; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
|
|
public function getBalance() |
79
|
|
|
{ |
80
|
|
|
return $this->balance; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param int $balance |
85
|
|
|
* @return \JhFlexiTime\Entity\RunningBalance |
86
|
|
|
*/ |
87
|
|
|
public function setBalance($balance) |
88
|
|
|
{ |
89
|
|
|
$this->balance = $balance; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param float $balance |
95
|
|
|
*/ |
96
|
|
|
public function addBalance($balance) |
97
|
|
|
{ |
98
|
|
|
$this->balance += (float) $balance; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param float $balance |
103
|
|
|
*/ |
104
|
|
|
public function subtractBalance($balance) |
105
|
|
|
{ |
106
|
|
|
$this->balance -= (float) $balance; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array |
111
|
|
|
* @throws \Exception |
112
|
|
|
*/ |
113
|
|
|
public function jsonSerialize() |
114
|
|
|
{ |
115
|
|
|
if (!$this->user instanceof UserInterface) { |
116
|
|
|
throw new \Exception('User Must be an instance of \ZfcUser\Entity\UserInterface'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return [ |
120
|
|
|
'id' => $this->id, |
121
|
|
|
'user' => $this->user->getId(), |
122
|
|
|
'balance' => $this->balance, |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: