| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Query; |
| 4 |
|
| 5 |
use Elementor\Modules\WpRest\Classes\Post_Query; |
| 6 |
use Elementor\Modules\WpRest\Classes\Term_Query; |
| 7 |
use Elementor\Modules\WpRest\Classes\User_Query; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Query_Builder_Factory { |
| 14 |
const ENDPOINT_KEY = 'endpoint'; |
| 15 |
|
| 16 |
private const BUILDERS = [ |
| 17 |
Post_Query::ENDPOINT => Post_Query_Builder::class, |
| 18 |
Term_Query::ENDPOINT => Term_Query_Builder::class, |
| 19 |
User_Query::ENDPOINT => User_Query_Builder::class, |
| 20 |
]; |
| 21 |
|
| 22 |
public static function create( ?array $config = [] ): Query_Builder_Base { |
| 23 |
$endpoint = $config[ self::ENDPOINT_KEY ] ?? Post_Query::ENDPOINT; |
| 24 |
|
| 25 |
$class = self::BUILDERS[ $endpoint ] ?? null; |
| 26 |
|
| 27 |
if ( ! $class ) { |
| 28 |
throw new \Exception( 'Unsupported query type' ); |
| 29 |
} |
| 30 |
|
| 31 |
return new $class( $config ?? [] ); |
| 32 |
} |
| 33 |
} |
| 34 |
|