PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
BanEntry.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
25
27use function array_shift;
28use function count;
29use function explode;
30use function implode;
31use function strlen;
32use function strtolower;
33use function trim;
34
36 public static string $format = "Y-m-d H:i:s O";
37
38 private string $name;
39 private \DateTime $creationDate;
40 private string $source = "(Unknown)";
41 private ?\DateTime $expirationDate = null;
42 private string $reason = "Banned by an operator.";
43
44 public function __construct(string $name){
45 $this->name = strtolower($name);
47 $this->creationDate = new \DateTime();
48 }
49
50 public function getName() : string{
51 return $this->name;
52 }
53
54 public function getCreated() : \DateTime{
55 return $this->creationDate;
56 }
57
61 public function setCreated(\DateTime $date) : void{
62 self::validateDate($date);
63 $this->creationDate = $date;
64 }
65
66 public function getSource() : string{
67 return $this->source;
68 }
69
70 public function setSource(string $source) : void{
71 $this->source = $source;
72 }
73
74 public function getExpires() : ?\DateTime{
75 return $this->expirationDate;
76 }
77
81 public function setExpires(?\DateTime $date) : void{
82 if($date !== null){
83 self::validateDate($date);
84 }
85 $this->expirationDate = $date;
86 }
87
88 public function hasExpired() : bool{
90 $now = new \DateTime();
91
92 return $this->expirationDate === null ? false : $this->expirationDate < $now;
93 }
94
95 public function getReason() : string{
96 return $this->reason;
97 }
98
99 public function setReason(string $reason) : void{
100 $this->reason = $reason;
101 }
102
103 public function getString() : string{
104 return implode("|", [
105 $this->getName(),
106 $this->getCreated()->format(self::$format),
107 $this->getSource(),
108 $this->getExpires() === null ? "Forever" : $this->getExpires()->format(self::$format),
109 $this->getReason()
110 ]);
111 }
112
121 private static function validateDate(\DateTime $dateTime) : void{
122 try{
123 self::parseDate($dateTime->format(self::$format));
124 }catch(\RuntimeException $e){
125 throw new \InvalidArgumentException($e->getMessage(), 0, $e);
126 }
127 }
128
132 private static function parseDate(string $date) : \DateTime{
133 $datetime = \DateTime::createFromFormat(self::$format, $date);
134 if(!($datetime instanceof \DateTime)){
135 $lastErrors = Utils::assumeNotFalse(\DateTime::getLastErrors(), "DateTime::getLastErrors() should not be returning false in here");
136 throw new \RuntimeException("Corrupted date/time: " . implode(", ", $lastErrors["errors"]));
137 }
138
139 return $datetime;
140 }
141
145 public static function fromString(string $str) : ?BanEntry{
146 if(strlen($str) < 2){
147 return null;
148 }
149
150 $parts = explode("|", trim($str));
151 $entry = new BanEntry(trim(array_shift($parts)));
152 if(count($parts) > 0){
153 $entry->setCreated(self::parseDate(array_shift($parts)));
154 }
155 if(count($parts) > 0){
156 $entry->setSource(trim(array_shift($parts)));
157 }
158 if(count($parts) > 0){
159 $expire = trim(array_shift($parts));
160 if($expire !== "" && strtolower($expire) !== "forever"){
161 $entry->setExpires(self::parseDate($expire));
162 }
163 }
164 if(count($parts) > 0){
165 $entry->setReason(trim(array_shift($parts)));
166 }
167
168 return $entry;
169 }
170}
Definition: BanEntry.php:35
setCreated(\DateTime $date)
Definition: BanEntry.php:61
static fromString(string $str)
Definition: BanEntry.php:145
setExpires(?\DateTime $date)
Definition: BanEntry.php:81