libasynql  3.2.0
Asynchronous MySQL access library for PocketMine plugins.
GenericVariable.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * libasynql
5  *
6  * Copyright (C) 2018 SOFe
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 declare(strict_types=1);
22 
23 namespace poggit\libasynql\generic;
24 
28 use function assert;
29 use function in_array;
30 use function is_string;
31 use function json_decode;
32 use function stripos;
33 use function strlen;
34 use function strpos;
35 use function strtoupper;
36 use function substr;
37 
42  public const TYPE_STRING = "string";
43  public const TYPE_INT = "int";
44  public const TYPE_FLOAT = "float";
45  public const TYPE_BOOL = "bool";
46  public const TYPE_TIMESTAMP = "timestamp";
47 
48  public const TIME_0 = "0";
49  public const TIME_NOW = "NOW";
50 
51  protected $name;
52  protected $list = false;
53  protected $canEmpty = false;
54  protected $nullable = false;
55  protected $type;
57  protected $default = null;
58 
59  public function __construct(string $name, string $type, ?string $default){
60  if(strpos($name, ":") !== false){
61  throw new InvalidArgumentException("Colon is disallowed in a variable name");
62  }
63  $this->name = $name;
64  if(stripos($type, "list:") === 0){
65  $this->list = true;
67  $type = substr($type, strlen("list:"));
68  }elseif(stripos($type, "list?") === 0){
69  $this->list = true;
70  $this->canEmpty = true;
72  $type = substr($type, strlen("list?"));
73  }elseif($type{0} === "?"){
74  $this->nullable = true;
75  $type = substr($type, 1);
76  }
77  $this->type = $type;
78  if($default !== null){
79  if($this->list){
80  throw new InvalidArgumentException("Lists cannot have default value");
81  }
82  switch($type){
83  case self::TYPE_STRING:
84  if($default{0} === "\"" && $default{strlen($default) - 1} === "\""){
87  }
88  $this->default = $default;
89  break;
90 
91  case self::TYPE_INT:
92  $this->default = (int) $default;
93  break;
94 
95  case self::TYPE_FLOAT:
96  $this->default = (float) $default;
97  break;
98 
99  case self::TYPE_BOOL:
100  $this->default = in_array($default, ["true", "on", "1"], true);
101  break;
102 
103  case self::TYPE_TIMESTAMP:
105  self::TIME_NOW,
106  self::TIME_0,
107  ], true)){
108  throw new InvalidArgumentException("Invalid timestamp default");
109  }
110  $this->default = $default;
111 
112  default:
113  throw new InvalidArgumentException("Unknown type \"$type\"");
114  }
115  }
116  }
117 
118  public function unlist() : GenericVariable{
119  if(!$this->list){
120  throw new InvalidStateException("Cannot unlist a non-list variable");
121  }
122  $clone = clone $this;
123  $clone->list = false;
124  return $clone;
125  }
126 
127  public function getName() : string{
128  return $this->name;
129  }
130 
131  public function isList() : bool{
132  return $this->list;
133  }
134 
144  public function canBeEmpty() : bool{
145  if(!$this->list){
146  throw new InvalidStateException("canBeEmpty() is only available for list variables");
147  }
148 
149  return $this->canEmpty;
150  }
151 
152  public function isNullable() : bool{
153  return $this->nullable;
154  }
155 
156  public function getType() : string{
157  return $this->type;
158  }
159 
163  public function getDefault(){
164  return $this->default;
165  }
166 
167  public function isOptional() : bool{
168  return $this->default !== null;
169  }
170 
171  public function equals(GenericVariable $that, &$diff = null) : bool{
172  if($this->name !== $that->name){
173  $diff = "name";
174  return false;
175  }
176  if($this->list !== $that->list){
177  $diff = "isList";
178  return false;
179  }
180  if($this->canEmpty !== $that->canEmpty){
181  $diff = "canBeEmpty";
182  return false;
183  }
184  if($this->type !== $that->type){
185  $diff = "type";
186  return false;
187  }
188  if($this->default !== $that->default){
189  $diff = "defaultValue";
190  return false;
191  }
192  return true;
193  }
194 
195  public function jsonSerialize(){
196  return [
197  "name" => $this->name,
198  "isList" => $this->list,
199  "canEmpty" => $this->canEmpty,
200  "type" => $this->type,
201  "default" => $this->default,
202  ];
203  }
204 }
__construct(string $name, string $type, ?string $default)
equals(GenericVariable $that, &$diff=null)