ArrayColumn.php
1 year ago
ArrayFlatten.php
1 year ago
ArrayKeys.php
1 year ago
ArraySearch.php
1 year ago
ArrayValues.php
1 year ago
Count.php
1 year ago
DotNotation.php
1 year ago
PrepareUrl.php
1 year ago
TransformerInterface.php
1 year ago
TransformerService.php
1 year ago
ArrayColumn.php
57 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\Transformers; |
| 4 | |
| 5 | use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\Transformers\TransformerInterface; |
| 6 | use InvalidArgumentException; |
| 7 | use stdClass; |
| 8 | |
| 9 | /** |
| 10 | * Search array value by one of its key. |
| 11 | * |
| 12 | * @package Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\Transformers |
| 13 | */ |
| 14 | class ArrayColumn implements TransformerInterface { |
| 15 | /** |
| 16 | * Search array value by one of its key. |
| 17 | * |
| 18 | * @param mixed $value a value to transform. |
| 19 | * @param stdClass|null $arguments required arguments 'key'. |
| 20 | * @param string|null $default_value default value. |
| 21 | * |
| 22 | * @throws InvalidArgumentException Throws when the required argument 'key' is missing. |
| 23 | * |
| 24 | * @return mixed |
| 25 | */ |
| 26 | public function transform( $value, ?stdClass $arguments = null, $default_value = array() ) { |
| 27 | if ( ! is_array( $value ) ) { |
| 28 | return $default_value; |
| 29 | } |
| 30 | |
| 31 | return array_column( $value, $arguments->key ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Validate Transformer arguments. |
| 36 | * |
| 37 | * @param stdClass|null $arguments arguments to validate. |
| 38 | * |
| 39 | * @return mixed |
| 40 | */ |
| 41 | public function validate( ?stdClass $arguments = null ) { |
| 42 | if ( ! isset( $arguments->key ) ) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | if ( |
| 47 | null !== $arguments->key && |
| 48 | ! is_string( $arguments->key ) && |
| 49 | ! is_int( $arguments->key ) |
| 50 | ) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 |