Component.php
2 years ago
ComponentTrait.php
3 years ago
Field.php
3 years ago
FormFactory.php
3 years ago
Section.php
3 years ago
Subsection.php
3 years ago
Tab.php
3 years ago
Component.php
139 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Abstract class for product form components. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Admin\ProductForm; |
| 7 | |
| 8 | /** |
| 9 | * Component class. |
| 10 | */ |
| 11 | abstract class Component { |
| 12 | /** |
| 13 | * Product Component traits. |
| 14 | */ |
| 15 | use ComponentTrait; |
| 16 | |
| 17 | /** |
| 18 | * Component additional arguments. |
| 19 | * |
| 20 | * @var array |
| 21 | */ |
| 22 | protected $additional_args; |
| 23 | |
| 24 | /** |
| 25 | * Constructor |
| 26 | * |
| 27 | * @param string $id Component id. |
| 28 | * @param string $plugin_id Plugin id. |
| 29 | * @param array $additional_args Array containing additional arguments. |
| 30 | */ |
| 31 | public function __construct( $id, $plugin_id, $additional_args ) { |
| 32 | $this->id = $id; |
| 33 | $this->plugin_id = $plugin_id; |
| 34 | $this->additional_args = $additional_args; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Component arguments. |
| 39 | * |
| 40 | * @return array |
| 41 | */ |
| 42 | public function get_additional_args() { |
| 43 | return $this->additional_args; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Component arguments. |
| 48 | * |
| 49 | * @param string $key key of argument. |
| 50 | * @return mixed |
| 51 | */ |
| 52 | public function get_additional_argument( $key ) { |
| 53 | return self::get_argument_from_path( $this->additional_args, $key ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the component as JSON. |
| 58 | * |
| 59 | * @return array |
| 60 | */ |
| 61 | public function get_json() { |
| 62 | return array_merge( |
| 63 | array( |
| 64 | 'id' => $this->get_id(), |
| 65 | 'plugin_id' => $this->get_plugin_id(), |
| 66 | ), |
| 67 | $this->get_additional_args() |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Sorting function for product form component. |
| 73 | * |
| 74 | * @param Component $a Component a. |
| 75 | * @param Component $b Component b. |
| 76 | * @param array $sort_by key and order to sort by. |
| 77 | * @return int |
| 78 | */ |
| 79 | public static function sort( $a, $b, $sort_by = array() ) { |
| 80 | $key = $sort_by['key']; |
| 81 | $a_val = $a->get_additional_argument( $key ); |
| 82 | $b_val = $b->get_additional_argument( $key ); |
| 83 | if ( 'asc' === $sort_by['order'] ) { |
| 84 | return $a_val <=> $b_val; |
| 85 | } else { |
| 86 | return $b_val <=> $a_val; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Gets argument by dot notation path. |
| 92 | * |
| 93 | * @param array $arguments Arguments array. |
| 94 | * @param string $path Path for argument key. |
| 95 | * @param string $delimiter Path delimiter, default: '.'. |
| 96 | * @return mixed|null |
| 97 | */ |
| 98 | public static function get_argument_from_path( $arguments, $path, $delimiter = '.' ) { |
| 99 | $path_keys = explode( $delimiter, $path ); |
| 100 | $num_keys = false !== $path_keys ? count( $path_keys ) : 0; |
| 101 | |
| 102 | $val = $arguments; |
| 103 | for ( $i = 0; $i < $num_keys; $i++ ) { |
| 104 | $key = $path_keys[ $i ]; |
| 105 | if ( array_key_exists( $key, $val ) ) { |
| 106 | $val = $val[ $key ]; |
| 107 | } else { |
| 108 | $val = null; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | return $val; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Array of required arguments. |
| 117 | * |
| 118 | * @var array |
| 119 | */ |
| 120 | protected $required_arguments = array(); |
| 121 | |
| 122 | /** |
| 123 | * Get missing arguments of args array. |
| 124 | * |
| 125 | * @param array $args field arguments. |
| 126 | * @return array |
| 127 | */ |
| 128 | public function get_missing_arguments( $args ) { |
| 129 | return array_values( |
| 130 | array_filter( |
| 131 | $this->required_arguments, |
| 132 | function( $arg_key ) use ( $args ) { |
| 133 | return null === self::get_argument_from_path( $args, $arg_key ); |
| 134 | } |
| 135 | ) |
| 136 | ); |
| 137 | } |
| 138 | } |
| 139 |