| 1 |
<?php |
| 2 |
/** |
| 3 |
* Experiment Registry class. |
| 4 |
* |
| 5 |
* @package WordPress\AI |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace WordPress\AI; |
| 11 |
|
| 12 |
use WordPress\AI\Contracts\Experiment; |
| 13 |
|
| 14 |
/** |
| 15 |
* Central registry for managing experiment storage and retrieval. |
| 16 |
* |
| 17 |
* Provides a simple storage mechanism for registered experiments. |
| 18 |
* Experiment initialization is handled by the Experiment_Loader class. |
| 19 |
* |
| 20 |
* @since 0.1.0 |
| 21 |
*/ |
| 22 |
final class Experiment_Registry { |
| 23 |
/** |
| 24 |
* Registered experiments. |
| 25 |
* |
| 26 |
* @since 0.1.0 |
| 27 |
* @var \WordPress\AI\Contracts\Experiment[] |
| 28 |
*/ |
| 29 |
private array $experiments = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* Registers an experiment. |
| 33 |
* |
| 34 |
* @since 0.1.0 |
| 35 |
* |
| 36 |
* @param \WordPress\AI\Contracts\Experiment $experiment Experiment instance to register. |
| 37 |
* @return bool True if registered successfully, false if already exists or invalid. |
| 38 |
*/ |
| 39 |
public function register_experiment( Experiment $experiment ): bool { |
| 40 |
$id = $experiment->get_id(); |
| 41 |
|
| 42 |
// Validate experiment ID is not empty. |
| 43 |
if ( empty( $id ) ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
if ( $this->has_experiment( $id ) ) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
$this->experiments[ $id ] = $experiment; |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Gets an experiment by ID. |
| 57 |
* |
| 58 |
* @since 0.1.0 |
| 59 |
* |
| 60 |
* @param string $id Experiment identifier. |
| 61 |
* @return \WordPress\AI\Contracts\Experiment|null Experiment instance or null if not found. |
| 62 |
*/ |
| 63 |
public function get_experiment( string $id ): ?Experiment { |
| 64 |
return $this->experiments[ $id ] ?? null; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Gets all registered experiments. |
| 69 |
* |
| 70 |
* @since 0.1.0 |
| 71 |
* |
| 72 |
* @return \WordPress\AI\Contracts\Experiment[] Array of experiment instances keyed by experiment ID. |
| 73 |
*/ |
| 74 |
public function get_all_experiments(): array { |
| 75 |
return $this->experiments; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Checks if an experiment is registered. |
| 80 |
* |
| 81 |
* @since 0.1.0 |
| 82 |
* |
| 83 |
* @param string $id Experiment identifier. |
| 84 |
* @return bool True if registered, false otherwise. |
| 85 |
*/ |
| 86 |
public function has_experiment( string $id ): bool { |
| 87 |
return isset( $this->experiments[ $id ] ); |
| 88 |
} |
| 89 |
} |
| 90 |
|