nash-ye /
ebloodbank
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * District entity class file |
||||
| 4 | * |
||||
| 5 | * @package EBloodBank |
||||
| 6 | * @subpackage Models |
||||
| 7 | * @since 1.0 |
||||
| 8 | */ |
||||
| 9 | namespace EBloodBank\Models; |
||||
| 10 | |||||
| 11 | use DateTime; |
||||
| 12 | use DateTimeZone; |
||||
| 13 | use EBloodBank as EBB; |
||||
| 14 | use InvalidArgumentException; |
||||
| 15 | |||||
| 16 | /** |
||||
| 17 | * District entity class |
||||
| 18 | * |
||||
| 19 | * @since 1.0 |
||||
| 20 | * |
||||
| 21 | * @Entity(repositoryClass="EBloodBank\Models\DistrictRepository") |
||||
| 22 | * @Table(name="district") |
||||
| 23 | * @HasLifecycleCallbacks |
||||
| 24 | */ |
||||
| 25 | class District extends Entity |
||||
| 26 | { |
||||
| 27 | /** |
||||
| 28 | * @var int |
||||
| 29 | * @since 1.0 |
||||
| 30 | * |
||||
| 31 | * @Id |
||||
| 32 | * @GeneratedValue |
||||
| 33 | * @Column(type="integer", name="district_id") |
||||
| 34 | */ |
||||
| 35 | protected $id = 0; |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * @var string |
||||
| 39 | * @since 1.0 |
||||
| 40 | * |
||||
| 41 | * @Column(type="string", name="district_name") |
||||
| 42 | */ |
||||
| 43 | protected $name; |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * @var City |
||||
| 47 | * @since 1.0 |
||||
| 48 | * |
||||
| 49 | * @ManyToOne(targetEntity="EBloodBank\Models\City", inversedBy="districts") |
||||
| 50 | * @JoinColumn(name="district_city_id", referencedColumnName="city_id") |
||||
| 51 | */ |
||||
| 52 | protected $city; |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * @var \DateTime |
||||
| 56 | * @since 1.0 |
||||
| 57 | * |
||||
| 58 | * @Column(type="datetime", name="district_created_at") |
||||
| 59 | */ |
||||
| 60 | protected $created_at; |
||||
| 61 | |||||
| 62 | /** |
||||
| 63 | * @var User |
||||
| 64 | * @since 1.0 |
||||
| 65 | * |
||||
| 66 | * @ManyToOne(targetEntity="EBloodBank\Models\User") |
||||
| 67 | * @JoinColumn(name="district_created_by", referencedColumnName="user_id") |
||||
| 68 | */ |
||||
| 69 | protected $created_by; |
||||
| 70 | |||||
| 71 | /** |
||||
| 72 | * @var Donor[] |
||||
| 73 | * @since 1.0 |
||||
| 74 | * |
||||
| 75 | * @OneToMany(targetEntity="EBloodBank\Models\Donor", mappedBy="district") |
||||
| 76 | */ |
||||
| 77 | protected $donors = []; |
||||
| 78 | |||||
| 79 | /** |
||||
| 80 | * @return bool |
||||
| 81 | * @since 1.0 |
||||
| 82 | */ |
||||
| 83 | public function isExists() |
||||
| 84 | { |
||||
| 85 | $id = (int) $this->get('id'); |
||||
| 86 | return ! empty($id); |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | /** |
||||
| 90 | * @return void |
||||
| 91 | * @since 1.6 |
||||
| 92 | * |
||||
| 93 | * @PrePersist |
||||
| 94 | */ |
||||
| 95 | public function doActionOnPrePersist() |
||||
| 96 | { |
||||
| 97 | $this->set('created_at', new DateTime('now', new DateTimeZone('UTC'))); |
||||
| 98 | } |
||||
| 99 | |||||
| 100 | /** |
||||
| 101 | * @return mixed |
||||
| 102 | * @since 1.0 |
||||
| 103 | * @static |
||||
| 104 | */ |
||||
| 105 | public static function sanitize($key, $value) |
||||
| 106 | { |
||||
| 107 | switch ($key) { |
||||
| 108 | case 'id': |
||||
| 109 | $value = EBB\sanitizeInteger($value); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 110 | break; |
||||
| 111 | case 'name': |
||||
| 112 | $value = EBB\sanitizeTitle($value); |
||||
|
0 ignored issues
–
show
The function
sanitizeTitle was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 113 | break; |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | return $value; |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | /** |
||||
| 120 | * @throws \InvalidArgumentException |
||||
| 121 | * @return bool |
||||
| 122 | * @since 1.0 |
||||
| 123 | * @static |
||||
| 124 | */ |
||||
| 125 | public static function validate($key, $value) |
||||
| 126 | { |
||||
| 127 | switch ($key) { |
||||
| 128 | case 'id': |
||||
| 129 | if (! EBB\isValidID($value)) { |
||||
|
0 ignored issues
–
show
The function
isValidID was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 130 | throw new InvalidArgumentException(__('Invalid district ID.')); |
||||
| 131 | } |
||||
| 132 | break; |
||||
| 133 | case 'name': |
||||
| 134 | if (! is_string($value) || empty($value)) { |
||||
| 135 | throw new InvalidArgumentException(__('Invalid district name.')); |
||||
| 136 | } |
||||
| 137 | break; |
||||
| 138 | case 'city': |
||||
| 139 | if (! $value instanceof City || ! $value->isExists()) { |
||||
| 140 | throw new InvalidArgumentException(__('Invalid district city.')); |
||||
| 141 | } |
||||
| 142 | break; |
||||
| 143 | case 'created_by': |
||||
| 144 | if (! $value instanceof User || ! $value->isExists()) { |
||||
| 145 | throw new InvalidArgumentException(__('Invalid district originator.')); |
||||
| 146 | } |
||||
| 147 | break; |
||||
| 148 | } |
||||
| 149 | |||||
| 150 | return true; |
||||
| 151 | } |
||||
| 152 | } |
||||
| 153 |