| 1 |
<?php |
| 2 |
|
| 3 |
namespace HelloPlus\Modules\Forms\Registrars; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Basic items registrar. |
| 11 |
* |
| 12 |
* TODO: Move to Core. |
| 13 |
*/ |
| 14 |
class Registrar { |
| 15 |
|
| 16 |
/** |
| 17 |
* @var array |
| 18 |
*/ |
| 19 |
private $items; |
| 20 |
|
| 21 |
/** |
| 22 |
* Registrar constructor. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
$this->items = []; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register a new item. |
| 32 |
* |
| 33 |
* @param $instance - Item instance. |
| 34 |
* @param string $id - Optional - For BC - Deprecated. |
| 35 |
* |
| 36 |
* @return boolean - Whether the item was registered. |
| 37 |
*/ |
| 38 |
public function register( $instance, $id = null ) { |
| 39 |
// TODO: For BC. Remove in the future. |
| 40 |
if ( ! $id ) { |
| 41 |
// Get the ID or default to the class name. |
| 42 |
$id = ( method_exists( $instance, 'get_id' ) ) ? $instance->get_id() : get_class( $instance ); |
| 43 |
} |
| 44 |
|
| 45 |
if ( $this->get( $id ) ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
$this->items[ $id ] = $instance; |
| 50 |
|
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get an item by ID. |
| 56 |
* |
| 57 |
* @param string $id |
| 58 |
* |
| 59 |
* @return array|null |
| 60 |
*/ |
| 61 |
public function get( $id = null ) { |
| 62 |
if ( ! $id ) { |
| 63 |
return $this->items; |
| 64 |
} |
| 65 |
|
| 66 |
return isset( $this->items[ $id ] ) ? $this->items[ $id ] : null; |
| 67 |
} |
| 68 |
} |
| 69 |
|