| 1 |
<?php |
| 2 |
/** |
| 3 |
* A class that is used when filtering a set of fields by a particular property. |
| 4 |
* |
| 5 |
* This is designed to be used in conjunction with array_filter in a PHP 5.2-friendly way. |
| 6 |
* i.e.: |
| 7 |
* |
| 8 |
* array_filter( $fields, array( new Charitable_Field_Filter( 'property' ), 'is_true' ) ); |
| 9 |
* |
| 10 |
* @package Charitable/Classes/Charitable_Field_Filter |
| 11 |
* @author Eric Daams |
| 12 |
* @copyright Copyright (c) 2018, Studio 164a |
| 13 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 14 |
* @since 1.6.0 |
| 15 |
* @version 1.6.0 |
| 16 |
*/ |
| 17 |
|
| 18 |
// Exit if accessed directly. |
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
if ( ! class_exists( 'Charitable_Field_Filter' ) ) : |
| 24 |
|
| 25 |
/** |
| 26 |
* Charitable_Field_Filter |
| 27 |
* |
| 28 |
* @since 1.6.0 |
| 29 |
*/ |
| 30 |
class Charitable_Field_Filter { |
| 31 |
|
| 32 |
/** |
| 33 |
* The property to be used for the filter. |
| 34 |
* |
| 35 |
* @since 1.6.0 |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $property; |
| 40 |
|
| 41 |
/** |
| 42 |
* Property value to compare against. |
| 43 |
* |
| 44 |
* @since 1.6.0 |
| 45 |
* |
| 46 |
* @var mixed |
| 47 |
*/ |
| 48 |
protected $value; |
| 49 |
|
| 50 |
/** |
| 51 |
* Sets up the filter with the property that will be used for the filter. |
| 52 |
* |
| 53 |
* @since 1.6.0 |
| 54 |
* |
| 55 |
* @param string $property The property to be used for the filter. |
| 56 |
* @param mixed $value A value to compare against. |
| 57 |
*/ |
| 58 |
public function __construct( $property, $value = '' ) { |
| 59 |
$this->property = $property; |
| 60 |
$this->value = $value; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns whether the value of the property for the given field is true. |
| 65 |
* |
| 66 |
* @since 1.6.0 |
| 67 |
* |
| 68 |
* @param Charitable_Field $field Instance of `Charitable_Field`. |
| 69 |
* @return boolean |
| 70 |
*/ |
| 71 |
public function is_true( Charitable_Field $field ) { |
| 72 |
return (bool) $field->{$this->property}; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns whether the value of the property for the given field is not false. |
| 77 |
* |
| 78 |
* @since 1.6.0 |
| 79 |
* |
| 80 |
* @param Charitable_Field $field Instance of `Charitable_Field`. |
| 81 |
* @return boolean |
| 82 |
*/ |
| 83 |
public function is_not_false( Charitable_Field $field ) { |
| 84 |
return false !== $field->{$this->property}; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns whether the value of the property for the given field is equal to the passed value. |
| 89 |
* |
| 90 |
* @since 1.6.0 |
| 91 |
* |
| 92 |
* @param Charitable_Field $field Instance of `Charitable_Field`. |
| 93 |
* @return boolean |
| 94 |
*/ |
| 95 |
public function is_equal_to( Charitable_Field $field ) { |
| 96 |
return $this->value === $field->{$this->property}; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
endif; |
| 101 |
|