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
ArraySearch.php
54 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 | * Searches a given a given value in the array. |
| 11 | * |
| 12 | * @package Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\Transformers |
| 13 | */ |
| 14 | class ArraySearch implements TransformerInterface { |
| 15 | /** |
| 16 | * Search a given value in the array. |
| 17 | * |
| 18 | * @param mixed $value a value to transform. |
| 19 | * @param stdClass|null $arguments required argument 'value'. |
| 20 | * @param string|null $default_value default value. |
| 21 | * |
| 22 | * @throws InvalidArgumentException Throws when the required 'value' is missing. |
| 23 | * |
| 24 | * @return mixed|null |
| 25 | */ |
| 26 | public function transform( $value, ?stdClass $arguments = null, $default_value = null ) { |
| 27 | if ( ! is_array( $value ) ) { |
| 28 | return $default_value; |
| 29 | } |
| 30 | |
| 31 | $key = array_search( $arguments->value, $value, true ); |
| 32 | if ( false !== $key ) { |
| 33 | return $value[ $key ]; |
| 34 | } |
| 35 | |
| 36 | return null; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Validate Transformer arguments. |
| 41 | * |
| 42 | * @param stdClass|null $arguments arguments to validate. |
| 43 | * |
| 44 | * @return mixed |
| 45 | */ |
| 46 | public function validate( ?stdClass $arguments = null ) { |
| 47 | if ( ! isset( $arguments->value ) ) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | return true; |
| 52 | } |
| 53 | } |
| 54 |