Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
9 / 10 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Abstract_Merge_Strategy | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| create_empty_item | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| get_default_value_for_field | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Abstract Merge Strategy |
| 4 | * |
| 5 | * Base class providing shared helper methods for merge strategies. |
| 6 | * |
| 7 | * @package Automattic\Jetpack\PremiumAnalytics\Reports\Export\MergeStrategy |
| 8 | */ |
| 9 | |
| 10 | declare( strict_types=1 ); |
| 11 | |
| 12 | namespace Automattic\Jetpack\PremiumAnalytics\Reports\Export\MergeStrategy; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | use Automattic\Jetpack\PremiumAnalytics\Reports\Export\Csv_Report_Controller_Interface; |
| 17 | |
| 18 | /** |
| 19 | * Abstract base class for merge strategies. |
| 20 | * |
| 21 | * Provides shared helper methods for creating empty items and getting default values. |
| 22 | * |
| 23 | * @since 0.1.0 |
| 24 | */ |
| 25 | abstract class Abstract_Merge_Strategy implements Merge_Strategy_Interface { |
| 26 | |
| 27 | /** |
| 28 | * Create an empty item with default values based on a template. |
| 29 | * |
| 30 | * @param array $template_item The template item to use for structure. |
| 31 | * @param Csv_Report_Controller_Interface|null $controller Optional controller for default values. |
| 32 | * @return array An array with the same keys but default values. |
| 33 | */ |
| 34 | protected function create_empty_item( array $template_item, ?Csv_Report_Controller_Interface $controller = null ): array { |
| 35 | $empty = array(); |
| 36 | foreach ( array_keys( $template_item ) as $key ) { |
| 37 | $empty[ $key ] = $this->get_default_value_for_field( $key, $controller ); |
| 38 | } |
| 39 | return $empty; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get the default value for a field based on its type/name. |
| 44 | * |
| 45 | * @param string $field_name The field name. |
| 46 | * @param Csv_Report_Controller_Interface|null $controller Optional controller for default values. |
| 47 | * @return mixed The default value for the field. |
| 48 | */ |
| 49 | protected function get_default_value_for_field( string $field_name, ?Csv_Report_Controller_Interface $controller = null ) { |
| 50 | if ( $controller ) { |
| 51 | $defaults = $controller->get_default_values(); |
| 52 | if ( isset( $defaults[ $field_name ] ) ) { |
| 53 | return $defaults[ $field_name ]; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return ''; |
| 58 | } |
| 59 | } |