| 1 |
<?php |
| 2 |
/** |
| 3 |
* Holds a collection of schedule objects. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Cron; |
| 11 |
|
| 12 |
/** |
| 13 |
* Holds a collection of schedule objects. |
| 14 |
* |
| 15 |
* @package SolidWP\Performance |
| 16 |
*/ |
| 17 |
final class Schedule_Collection { |
| 18 |
|
| 19 |
/** |
| 20 |
* @var array<string, Schedule> |
| 21 |
*/ |
| 22 |
private array $schedules; |
| 23 |
|
| 24 |
/** |
| 25 |
* @param Schedule ...$schedules The schedule objects. |
| 26 |
*/ |
| 27 |
public function __construct( Schedule ...$schedules ) { |
| 28 |
foreach ( $schedules as $schedule ) { |
| 29 |
$this->schedules[ $schedule->name() ] = $schedule; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get all schedules in the collection. |
| 35 |
* |
| 36 |
* @return array<string, Schedule> |
| 37 |
*/ |
| 38 |
public function all(): array { |
| 39 |
return $this->schedules; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get the schedules formatted for the cron_schedules filter. |
| 44 |
* |
| 45 |
* @return array<array<string, array{interval: int, display: string}>> |
| 46 |
*/ |
| 47 |
public function to_array(): array { |
| 48 |
return array_reduce( |
| 49 |
$this->all(), |
| 50 |
function ( array $carry, Schedule $schedule ) { |
| 51 |
return array_merge( $carry, $schedule->get() ); |
| 52 |
}, |
| 53 |
[] |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Retrieve a specific schedule by name. |
| 59 |
* |
| 60 |
* @param string $name The schedule name. |
| 61 |
* |
| 62 |
* @return Schedule|null |
| 63 |
*/ |
| 64 |
public function get( string $name ): ?Schedule { |
| 65 |
return $this->schedules[ $name ] ?? null; |
| 66 |
} |
| 67 |
} |
| 68 |
|