| @@ -16,9 +16,8 @@ | ||
| 16 | 16 | * Base model class for all models. |
| 17 | 17 | */ |
| 18 | 18 | abstract class Model extends Component { |
| 19 | 19 | |
| 20 | - | |
| 21 | 20 | /** |
| 22 | 21 | * Table name | option name | post type name. |
| 23 | 22 | * |
| 24 | 23 | * @var string |
| @@ -60,8 +59,17 @@ | ||
| 60 | 59 | */ |
| 61 | 60 | protected $mapping = array(); |
| 62 | 61 | |
| 63 | 62 | /** |
| 63 | + * Get the table name. | |
| 64 | + * | |
| 65 | + * @return string|null | |
| 66 | + */ | |
| 67 | + public function get_table(): ?string { | |
| 68 | + return $this->table; | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 64 | 72 | * Run the validation. |
| 65 | 73 | * |
| 66 | 74 | * @return bool |
| 67 | 75 | */ |
| @@ -66,9 +74,9 @@ | ||
| 66 | 74 | * @return bool |
| 67 | 75 | */ |
| 68 | 76 | public function validate(): bool { |
| 69 | 77 | $this->before_validate(); |
| 70 | - if ( empty( $this->annotations ) ) { | |
| 78 | + if ( array() === $this->annotations ) { | |
| 71 | 79 | $this->log( 'Empty annotations.', wd_internal_log() ); |
| 72 | 80 | } |
| 73 | 81 | $validate = $this->get_validate_rules(); |
| 74 | 82 | $validate->validate(); |
| @@ -95,9 +103,9 @@ | ||
| 95 | 103 | * |
| 96 | 104 | * @return array |
| 97 | 105 | */ |
| 98 | 106 | public function export(): array { |
| 99 | - if ( empty( $this->annotations ) ) { | |
| 107 | + if ( array() === $this->annotations ) { | |
| 100 | 108 | return $this->export_oldway(); |
| 101 | 109 | } |
| 102 | 110 | $return = array(); |
| 103 | 111 | foreach ( array_keys( $this->annotations ) as $property ) { |
| @@ -197,9 +205,9 @@ | ||
| 197 | 205 | */ |
| 198 | 206 | public function import_old_way( $data ): void { |
| 199 | 207 | foreach ( $data as $key => $val ) { |
| 200 | 208 | // Check if we have a safe list. |
| 201 | - if ( ! empty( $this->safe ) && ! in_array( $key, $this->safe, true ) ) { | |
| 209 | + if ( array() !== $this->safe && ! in_array( $key, $this->safe, true ) ) { | |
| 202 | 210 | continue; |
| 203 | 211 | } |
| 204 | 212 | |
| 205 | 213 | $allowed = array_keys( $this->export() ); |
| @@ -220,14 +228,23 @@ | ||
| 220 | 228 | * |
| 221 | 229 | * @return void |
| 222 | 230 | */ |
| 223 | 231 | public function import( $data ): void { |
| 224 | - if ( empty( $this->annotations ) ) { | |
| 232 | + if ( array() === $this->annotations ) { | |
| 225 | 233 | $this->import_old_way( $data ); |
| 226 | 234 | } else { |
| 227 | 235 | foreach ( array_keys( $this->annotations ) as $property ) { |
| 228 | 236 | if ( isset( $data[ $property ] ) && $this->has_property( $property ) ) { |
| 229 | - $this->$property = $data[ $property ]; | |
| 237 | + $value = $data[ $property ]; | |
| 238 | + $type = $this->annotations[ $property ]['type']; | |
| 239 | + if ( false !== $type ) { | |
| 240 | + if ( 'boolean' === $type || 'bool' === $type ) { | |
| 241 | + $value = filter_var( $value, FILTER_VALIDATE_BOOLEAN ); | |
| 242 | + } else { | |
| 243 | + settype( $value, $type ); | |
| 244 | + } | |
| 245 | + } | |
| 246 | + $this->$property = $value; | |
| 230 | 247 | } |
| 231 | 248 | } |
| 232 | 249 | |
| 233 | 250 | $this->sanitize(); |
| @@ -246,9 +263,9 @@ | ||
| 246 | 263 | if ( ! count( $data ) ) { |
| 247 | 264 | $data = $this->export(); |
| 248 | 265 | $scenario = 'export'; |
| 249 | 266 | } |
| 250 | - if ( empty( $this->mapping ) ) { | |
| 267 | + if ( array() === $this->mapping ) { | |
| 251 | 268 | return $data; |
| 252 | 269 | } |
| 253 | 270 | foreach ( $this->mapping as $key => $val ) { |
| 254 | 271 | if ( 'export' === $scenario && isset( $data[ $key ] ) ) { |
| @@ -268,9 +285,9 @@ | ||
| 268 | 285 | * |
| 269 | 286 | * @return void |
| 270 | 287 | */ |
| 271 | 288 | protected function sanitize() { |
| 272 | - if ( empty( $this->annotations ) ) { | |
| 289 | + if ( array() === $this->annotations ) { | |
| 273 | 290 | return; |
| 274 | 291 | } |
| 275 | 292 | |
| 276 | 293 | foreach ( $this->annotations as $property => $meta ) { |
| @@ -363,9 +380,9 @@ | ||
| 363 | 380 | public function export_type(): array { |
| 364 | 381 | $reflection = new ReflectionClass( $this ); |
| 365 | 382 | $props = $reflection->getProperties( ReflectionProperty::IS_PUBLIC ); |
| 366 | 383 | |
| 367 | - if ( empty( $this->annotations ) ) { | |
| 384 | + if ( array() === $this->annotations ) { | |
| 368 | 385 | return $this->export_type_oldway(); |
| 369 | 386 | } |
| 370 | 387 | |
| 371 | 388 | $types = array(); |