| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\Lib; |
| 4 | 4 | |
| 5 | +use Exception; | |
| 5 | 6 | use JsonSerializable; |
| 6 | 7 | use ReturnTypeWillChange; |
| 7 | 8 | |
| 8 | 9 | /** |
| @@ -21,9 +22,9 @@ | ||
| 21 | 22 | * a public static $id_column property to your model classes. |
| 22 | 23 | * |
| 23 | 24 | * @var string |
| 24 | 25 | */ |
| 25 | - const DEFAULT_ID_COLUMN = 'id'; | |
| 26 | + public const DEFAULT_ID_COLUMN = 'id'; | |
| 26 | 27 | |
| 27 | 28 | /** |
| 28 | 29 | * Default foreign key suffix used by relationship methods. |
| 29 | 30 | * |
| @@ -28,9 +29,9 @@ | ||
| 28 | 29 | * Default foreign key suffix used by relationship methods. |
| 29 | 30 | * |
| 30 | 31 | * @var string |
| 31 | 32 | */ |
| 32 | - const DEFAULT_FOREIGN_KEY_SUFFIX = '_id'; | |
| 33 | + public const DEFAULT_FOREIGN_KEY_SUFFIX = '_id'; | |
| 33 | 34 | |
| 34 | 35 | /** |
| 35 | 36 | * Set a prefix for model names. This can be a namespace or any other |
| 36 | 37 | * abitrary prefix such as the PEAR naming convention. |
| @@ -329,12 +330,12 @@ | ||
| 329 | 330 | * @param string|null $foreign_key_name_in_current_models_table The foreign key in the current models table. |
| 330 | 331 | * |
| 331 | 332 | * @return ORM Instance of the ORM. |
| 332 | 333 | * |
| 333 | - * @throws \Exception When ID of current model has a null value. | |
| 334 | + * @throws Exception When ID of current model has a null value. | |
| 334 | 335 | */ |
| 335 | 336 | protected function has_one_or_many( $associated_class_name, $foreign_key_name = null, $foreign_key_name_in_current_models_table = null ) { |
| 336 | - $base_table_name = static::get_table_name_for_class( \get_class( $this ) ); | |
| 337 | + $base_table_name = static::get_table_name_for_class( static::class ); | |
| 337 | 338 | $foreign_key_name = static::build_foreign_key_name( $foreign_key_name, $base_table_name ); |
| 338 | 339 | |
| 339 | 340 | /* |
| 340 | 341 | * Value of foreign_table.{$foreign_key_name} we're looking for. Where foreign_table is the actual |
| @@ -361,9 +362,9 @@ | ||
| 361 | 362 | * @param string|null $foreign_key_name_in_current_models_table The foreign key in the current models table. |
| 362 | 363 | * |
| 363 | 364 | * @return ORM Instance of the ORM. |
| 364 | 365 | * |
| 365 | - * @throws \Exception When ID of current model has a null value. | |
| 366 | + * @throws Exception When ID of current model has a null value. | |
| 366 | 367 | */ |
| 367 | 368 | protected function has_one( $associated_class_name, $foreign_key_name = null, $foreign_key_name_in_current_models_table = null ) { |
| 368 | 369 | return $this->has_one_or_many( $associated_class_name, $foreign_key_name, $foreign_key_name_in_current_models_table ); |
| 369 | 370 | } |
| @@ -377,9 +378,9 @@ | ||
| 377 | 378 | * @param string|null $foreign_key_name_in_current_models_table The foreign key in the current models table. |
| 378 | 379 | * |
| 379 | 380 | * @return ORM Instance of the ORM. |
| 380 | 381 | * |
| 381 | - * @throws \Exception When ID has a null value. | |
| 382 | + * @throws Exception When ID has a null value. | |
| 382 | 383 | */ |
| 383 | 384 | protected function has_many( $associated_class_name, $foreign_key_name = null, $foreign_key_name_in_current_models_table = null ) { |
| 384 | 385 | $this->set_table_name( $associated_class_name ); |
| 385 | 386 | |
| @@ -401,9 +402,9 @@ | ||
| 401 | 402 | |
| 402 | 403 | $associated_table_name = static::get_table_name_for_class( static::$auto_prefix_models . $associated_class_name ); |
| 403 | 404 | $foreign_key_name = static::build_foreign_key_name( $foreign_key_name, $associated_table_name ); |
| 404 | 405 | $associated_object_id = $this->{$foreign_key_name}; |
| 405 | - $desired_record = null; | |
| 406 | + | |
| 406 | 407 | if ( $foreign_key_name_in_associated_models_table === null ) { |
| 407 | 408 | /* |
| 408 | 409 | * Comparison: "{$associated_table_name}.primary_key = {$associated_object_id}". |
| 409 | 410 | * |
| @@ -430,9 +431,9 @@ | ||
| 430 | 431 | * |
| 431 | 432 | * @return ORM Instance of the ORM. |
| 432 | 433 | */ |
| 433 | 434 | protected function has_many_through( $associated_class_name, $join_class_name = null, $key_to_base_table = null, $key_to_associated_table = null, $key_in_base_table = null, $key_in_associated_table = null ) { |
| 434 | - $base_class_name = \get_class( $this ); | |
| 435 | + $base_class_name = static::class; | |
| 435 | 436 | |
| 436 | 437 | /* |
| 437 | 438 | * The class name of the join model, if not supplied, is formed by |
| 438 | 439 | * concatenating the names of the base class and the associated class, |
| @@ -482,9 +483,9 @@ | ||
| 482 | 483 | [ |
| 483 | 484 | "{$associated_table_name}.{$associated_table_id_column}", |
| 484 | 485 | '=', |
| 485 | 486 | "{$join_table_name}.{$key_to_associated_table}", |
| 486 | - ] | |
| 487 | + ], | |
| 487 | 488 | ) |
| 488 | 489 | ->where( "{$join_table_name}.{$key_to_base_table}", $this->{$base_table_id_column} ); |
| 489 | 490 | } |
| 490 | 491 | |
| @@ -570,9 +571,13 @@ | ||
| 570 | 571 | * |
| 571 | 572 | * @return array |
| 572 | 573 | */ |
| 573 | 574 | public function __debugInfo() { |
| 574 | - return $this->orm->as_array(); | |
| 575 | + if ( $this->orm ) { | |
| 576 | + return $this->orm->as_array(); | |
| 577 | + } | |
| 578 | + | |
| 579 | + return []; | |
| 575 | 580 | } |
| 576 | 581 | |
| 577 | 582 | /** |
| 578 | 583 | * Magic isset method, allows isset($model->property) to work correctly. |
| @@ -684,9 +689,9 @@ | ||
| 684 | 689 | * Get the database ID of this model instance. |
| 685 | 690 | * |
| 686 | 691 | * @return int The database ID of the models instance. |
| 687 | 692 | * |
| 688 | - * @throws \Exception When the ID is a null value. | |
| 693 | + * @throws Exception When the ID is a null value. | |
| 689 | 694 | */ |
| 690 | 695 | public function id() { |
| 691 | 696 | return $this->orm->id(); |
| 692 | 697 | } |
| @@ -717,9 +722,9 @@ | ||
| 717 | 722 | if ( ! \function_exists( 'get_called_class' ) ) { |
| 718 | 723 | return []; |
| 719 | 724 | } |
| 720 | 725 | |
| 721 | - $model = static::factory( \get_called_class() ); | |
| 726 | + $model = static::factory( static::class ); | |
| 722 | 727 | |
| 723 | 728 | return \call_user_func_array( [ $model, $method ], $arguments ); |
| 724 | 729 | } |
| 725 | 730 | } |