Completed
Pull Request — develop (#1161)
by Naveen
02:34
created
src/wordlift/mappings/class-mappings-validator.php 2 patches
Indentation   +139 added lines, -139 removed lines patch added patch discarded remove patch
@@ -16,144 +16,144 @@
 block discarded – undo
16 16
 use Wordlift\Mappings\Validators\Rule_Groups_Validator;
17 17
 
18 18
 final class Mappings_Validator {
19
-	const TRASH_CATEGORY = 'trash';
20
-	const ACTIVE_CATEGORY = 'active';
21
-
22
-	/**
23
-	 * The {@link Mappings_DBO} instance to test.
24
-	 *
25
-	 * @since  3.25.0
26
-	 * @access private
27
-	 * @var Mappings_DBO $dbo The {@link Mappings_DBO} instance to test.
28
-	 */
29
-	private $dbo;
30
-
31
-	/**
32
-	 * @var Rule_Groups_Validator
33
-	 */
34
-	private $rule_groups_validator;
35
-
36
-	/**
37
-	 * Constructor for Wordlift_Mapping_Validator.
38
-	 *
39
-	 * @param Mappings_DBO $dbo The {@link Mappings_DBO} instance.
40
-	 * @param Rule_Groups_Validator $rule_groups_validator
41
-	 */
42
-	public function __construct( $dbo, $rule_groups_validator ) {
43
-
44
-		$this->dbo                   = $dbo;
45
-		$this->rule_groups_validator = $rule_groups_validator;
46
-
47
-	}
48
-
49
-	/**
50
-	 * This method is used to filter properties based on presence
51
-	 * of certain key values.
52
-	 *
53
-	 * @param $items array Array of properties.
54
-	 *
55
-	 * @return array
56
-	 */
57
-	private static function filter_properties_for_required_keys( $items ) {
58
-		return array_filter(
59
-			$items,
60
-			function ( $item ) {
61
-				/**
62
-				 * Since the properties might also be passed
63
-				 * by external plugins, we might need to check if
64
-				 * they have correct data format.
65
-				 */
66
-				if ( ! array_key_exists( 'property_name', $item ) ||
67
-				     ! array_key_exists( 'field_type', $item ) ||
68
-				     ! array_key_exists( 'field_name', $item ) ||
69
-				     ! array_key_exists( 'transform_function', $item )
70
-				) {
71
-					// If these keys doesnt exist, then dont process.
72
-					return false;
73
-				} else {
74
-					// If the keys exist, then filter it.
75
-					return true;
76
-				}
77
-			}
78
-		);
79
-	}
80
-
81
-	/**
82
-	 * Validates two values based on the passed logic
83
-	 * a single rule passes the user defined logic.
84
-	 *
85
-	 * @param string $key The key which every object has mapped to our value.
86
-	 * @param array $items The array of items.
87
-	 * @param string $status The value which the items should have.
88
-	 *
89
-	 * @return array
90
-	 */
91
-	private static function get_property_item_by_status( $key, $items, $status ) {
92
-		return array_filter(
93
-			$items,
94
-			function ( $item ) use ( $key, $status ) {
95
-				return $item[ $key ] === (string) $status;
96
-			}
97
-		);
98
-	}
99
-
100
-	/**
101
-	 * Validates a post id with the list of active mapping items and check if
102
-	 * a mapping can be applied.
103
-	 *
104
-	 * @param int $identifier The post id or term id based on type.
105
-	 *
106
-	 * @param string $type Post or term.
107
-	 *
108
-	 * @return array
109
-	 */
110
-	public function validate( $identifier, $type ) {
111
-		// Reset the valid property items before making the validation.
112
-		$properties = array();
113
-
114
-		// Filter registered properties
115
-		$filter_registered_properties = array();
116
-
117
-		// Get active mappings.
118
-		$mappings = $this->dbo->get_active_mappings();
119
-		/**
120
-		 * Apply this filter to get mappings from external plugins.
121
-		 *
122
-		 * @param $mappings array Array of mappings from database.
123
-		 * @param $identifier int The post id or term id based on type.
124
-		 */
125
-		$mappings = apply_filters( 'wl_mappings_post', $mappings, $identifier );
126
-
127
-		// Get all active rule groups for the mapping items.
128
-		foreach ( $mappings as $mapping ) {
129
-			if ( array_key_exists( 'mapping_id', $mapping ) ) {
130
-				$rule_groups          = $this->dbo->get_rule_groups_by_mapping( (int) $mapping['mapping_id'] );
131
-				$should_apply_mapping = $this->rule_groups_validator->is_valid( $identifier, $rule_groups, $type );
132
-				if ( $should_apply_mapping ) {
133
-					$mapping_item_properties = $this->dbo->get_properties( $mapping['mapping_id'] );
134
-					$properties              = array_merge( $properties, $mapping_item_properties );
135
-				}
136
-			} else {
137
-				/**
138
-				 * This is a programmatically defined mapping,
139
-				 * so we will have the rule groups and the properties in the array keys
140
-				 */
141
-				if ( array_key_exists( 'properties', $mapping ) &&
142
-				     is_array( $mapping['properties'] ) ) {
143
-					$filter_registered_properties = array_merge( $filter_registered_properties, $mapping['properties'] );
144
-				}
145
-			}
146
-		}
147
-		// Filter all registered properties based on required key values.
148
-		$filter_registered_properties = self::filter_properties_for_required_keys( $filter_registered_properties );
149
-		$active_properties            = self::get_property_item_by_status(
150
-			'property_status',
151
-			$properties,
152
-			self::ACTIVE_CATEGORY
153
-		);
154
-
155
-		// Merge ui defined properties with filter registered properties.
156
-		return array_merge( $active_properties, $filter_registered_properties );
157
-	}
19
+    const TRASH_CATEGORY = 'trash';
20
+    const ACTIVE_CATEGORY = 'active';
21
+
22
+    /**
23
+     * The {@link Mappings_DBO} instance to test.
24
+     *
25
+     * @since  3.25.0
26
+     * @access private
27
+     * @var Mappings_DBO $dbo The {@link Mappings_DBO} instance to test.
28
+     */
29
+    private $dbo;
30
+
31
+    /**
32
+     * @var Rule_Groups_Validator
33
+     */
34
+    private $rule_groups_validator;
35
+
36
+    /**
37
+     * Constructor for Wordlift_Mapping_Validator.
38
+     *
39
+     * @param Mappings_DBO $dbo The {@link Mappings_DBO} instance.
40
+     * @param Rule_Groups_Validator $rule_groups_validator
41
+     */
42
+    public function __construct( $dbo, $rule_groups_validator ) {
43
+
44
+        $this->dbo                   = $dbo;
45
+        $this->rule_groups_validator = $rule_groups_validator;
46
+
47
+    }
48
+
49
+    /**
50
+     * This method is used to filter properties based on presence
51
+     * of certain key values.
52
+     *
53
+     * @param $items array Array of properties.
54
+     *
55
+     * @return array
56
+     */
57
+    private static function filter_properties_for_required_keys( $items ) {
58
+        return array_filter(
59
+            $items,
60
+            function ( $item ) {
61
+                /**
62
+                 * Since the properties might also be passed
63
+                 * by external plugins, we might need to check if
64
+                 * they have correct data format.
65
+                 */
66
+                if ( ! array_key_exists( 'property_name', $item ) ||
67
+                     ! array_key_exists( 'field_type', $item ) ||
68
+                     ! array_key_exists( 'field_name', $item ) ||
69
+                     ! array_key_exists( 'transform_function', $item )
70
+                ) {
71
+                    // If these keys doesnt exist, then dont process.
72
+                    return false;
73
+                } else {
74
+                    // If the keys exist, then filter it.
75
+                    return true;
76
+                }
77
+            }
78
+        );
79
+    }
80
+
81
+    /**
82
+     * Validates two values based on the passed logic
83
+     * a single rule passes the user defined logic.
84
+     *
85
+     * @param string $key The key which every object has mapped to our value.
86
+     * @param array $items The array of items.
87
+     * @param string $status The value which the items should have.
88
+     *
89
+     * @return array
90
+     */
91
+    private static function get_property_item_by_status( $key, $items, $status ) {
92
+        return array_filter(
93
+            $items,
94
+            function ( $item ) use ( $key, $status ) {
95
+                return $item[ $key ] === (string) $status;
96
+            }
97
+        );
98
+    }
99
+
100
+    /**
101
+     * Validates a post id with the list of active mapping items and check if
102
+     * a mapping can be applied.
103
+     *
104
+     * @param int $identifier The post id or term id based on type.
105
+     *
106
+     * @param string $type Post or term.
107
+     *
108
+     * @return array
109
+     */
110
+    public function validate( $identifier, $type ) {
111
+        // Reset the valid property items before making the validation.
112
+        $properties = array();
113
+
114
+        // Filter registered properties
115
+        $filter_registered_properties = array();
116
+
117
+        // Get active mappings.
118
+        $mappings = $this->dbo->get_active_mappings();
119
+        /**
120
+         * Apply this filter to get mappings from external plugins.
121
+         *
122
+         * @param $mappings array Array of mappings from database.
123
+         * @param $identifier int The post id or term id based on type.
124
+         */
125
+        $mappings = apply_filters( 'wl_mappings_post', $mappings, $identifier );
126
+
127
+        // Get all active rule groups for the mapping items.
128
+        foreach ( $mappings as $mapping ) {
129
+            if ( array_key_exists( 'mapping_id', $mapping ) ) {
130
+                $rule_groups          = $this->dbo->get_rule_groups_by_mapping( (int) $mapping['mapping_id'] );
131
+                $should_apply_mapping = $this->rule_groups_validator->is_valid( $identifier, $rule_groups, $type );
132
+                if ( $should_apply_mapping ) {
133
+                    $mapping_item_properties = $this->dbo->get_properties( $mapping['mapping_id'] );
134
+                    $properties              = array_merge( $properties, $mapping_item_properties );
135
+                }
136
+            } else {
137
+                /**
138
+                 * This is a programmatically defined mapping,
139
+                 * so we will have the rule groups and the properties in the array keys
140
+                 */
141
+                if ( array_key_exists( 'properties', $mapping ) &&
142
+                     is_array( $mapping['properties'] ) ) {
143
+                    $filter_registered_properties = array_merge( $filter_registered_properties, $mapping['properties'] );
144
+                }
145
+            }
146
+        }
147
+        // Filter all registered properties based on required key values.
148
+        $filter_registered_properties = self::filter_properties_for_required_keys( $filter_registered_properties );
149
+        $active_properties            = self::get_property_item_by_status(
150
+            'property_status',
151
+            $properties,
152
+            self::ACTIVE_CATEGORY
153
+        );
154
+
155
+        // Merge ui defined properties with filter registered properties.
156
+        return array_merge( $active_properties, $filter_registered_properties );
157
+    }
158 158
 
159 159
 }
Please login to merge, or discard this patch.
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -39,7 +39,7 @@  discard block
 block discarded – undo
39 39
 	 * @param Mappings_DBO $dbo The {@link Mappings_DBO} instance.
40 40
 	 * @param Rule_Groups_Validator $rule_groups_validator
41 41
 	 */
42
-	public function __construct( $dbo, $rule_groups_validator ) {
42
+	public function __construct($dbo, $rule_groups_validator) {
43 43
 
44 44
 		$this->dbo                   = $dbo;
45 45
 		$this->rule_groups_validator = $rule_groups_validator;
@@ -54,19 +54,19 @@  discard block
 block discarded – undo
54 54
 	 *
55 55
 	 * @return array
56 56
 	 */
57
-	private static function filter_properties_for_required_keys( $items ) {
57
+	private static function filter_properties_for_required_keys($items) {
58 58
 		return array_filter(
59 59
 			$items,
60
-			function ( $item ) {
60
+			function($item) {
61 61
 				/**
62 62
 				 * Since the properties might also be passed
63 63
 				 * by external plugins, we might need to check if
64 64
 				 * they have correct data format.
65 65
 				 */
66
-				if ( ! array_key_exists( 'property_name', $item ) ||
67
-				     ! array_key_exists( 'field_type', $item ) ||
68
-				     ! array_key_exists( 'field_name', $item ) ||
69
-				     ! array_key_exists( 'transform_function', $item )
66
+				if ( ! array_key_exists('property_name', $item) ||
67
+				     ! array_key_exists('field_type', $item) ||
68
+				     ! array_key_exists('field_name', $item) ||
69
+				     ! array_key_exists('transform_function', $item)
70 70
 				) {
71 71
 					// If these keys doesnt exist, then dont process.
72 72
 					return false;
@@ -88,11 +88,11 @@  discard block
 block discarded – undo
88 88
 	 *
89 89
 	 * @return array
90 90
 	 */
91
-	private static function get_property_item_by_status( $key, $items, $status ) {
91
+	private static function get_property_item_by_status($key, $items, $status) {
92 92
 		return array_filter(
93 93
 			$items,
94
-			function ( $item ) use ( $key, $status ) {
95
-				return $item[ $key ] === (string) $status;
94
+			function($item) use ($key, $status) {
95
+				return $item[$key] === (string) $status;
96 96
 			}
97 97
 		);
98 98
 	}
@@ -107,7 +107,7 @@  discard block
 block discarded – undo
107 107
 	 *
108 108
 	 * @return array
109 109
 	 */
110
-	public function validate( $identifier, $type ) {
110
+	public function validate($identifier, $type) {
111 111
 		// Reset the valid property items before making the validation.
112 112
 		$properties = array();
113 113
 
@@ -122,30 +122,30 @@  discard block
 block discarded – undo
122 122
 		 * @param $mappings array Array of mappings from database.
123 123
 		 * @param $identifier int The post id or term id based on type.
124 124
 		 */
125
-		$mappings = apply_filters( 'wl_mappings_post', $mappings, $identifier );
125
+		$mappings = apply_filters('wl_mappings_post', $mappings, $identifier);
126 126
 
127 127
 		// Get all active rule groups for the mapping items.
128
-		foreach ( $mappings as $mapping ) {
129
-			if ( array_key_exists( 'mapping_id', $mapping ) ) {
130
-				$rule_groups          = $this->dbo->get_rule_groups_by_mapping( (int) $mapping['mapping_id'] );
131
-				$should_apply_mapping = $this->rule_groups_validator->is_valid( $identifier, $rule_groups, $type );
132
-				if ( $should_apply_mapping ) {
133
-					$mapping_item_properties = $this->dbo->get_properties( $mapping['mapping_id'] );
134
-					$properties              = array_merge( $properties, $mapping_item_properties );
128
+		foreach ($mappings as $mapping) {
129
+			if (array_key_exists('mapping_id', $mapping)) {
130
+				$rule_groups          = $this->dbo->get_rule_groups_by_mapping((int) $mapping['mapping_id']);
131
+				$should_apply_mapping = $this->rule_groups_validator->is_valid($identifier, $rule_groups, $type);
132
+				if ($should_apply_mapping) {
133
+					$mapping_item_properties = $this->dbo->get_properties($mapping['mapping_id']);
134
+					$properties              = array_merge($properties, $mapping_item_properties);
135 135
 				}
136 136
 			} else {
137 137
 				/**
138 138
 				 * This is a programmatically defined mapping,
139 139
 				 * so we will have the rule groups and the properties in the array keys
140 140
 				 */
141
-				if ( array_key_exists( 'properties', $mapping ) &&
142
-				     is_array( $mapping['properties'] ) ) {
143
-					$filter_registered_properties = array_merge( $filter_registered_properties, $mapping['properties'] );
141
+				if (array_key_exists('properties', $mapping) &&
142
+				     is_array($mapping['properties'])) {
143
+					$filter_registered_properties = array_merge($filter_registered_properties, $mapping['properties']);
144 144
 				}
145 145
 			}
146 146
 		}
147 147
 		// Filter all registered properties based on required key values.
148
-		$filter_registered_properties = self::filter_properties_for_required_keys( $filter_registered_properties );
148
+		$filter_registered_properties = self::filter_properties_for_required_keys($filter_registered_properties);
149 149
 		$active_properties            = self::get_property_item_by_status(
150 150
 			'property_status',
151 151
 			$properties,
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
 		);
154 154
 
155 155
 		// Merge ui defined properties with filter registered properties.
156
-		return array_merge( $active_properties, $filter_registered_properties );
156
+		return array_merge($active_properties, $filter_registered_properties);
157 157
 	}
158 158
 
159 159
 }
Please login to merge, or discard this patch.