Completed
Branch 2.0.0 (814c19)
by Jimmy
03:05
created

Array_Util   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 19
lcom 0
cbo 1
1
<?php
2
/**
3
 * Méthodes utilitaires pour les tableaux.
4
 *
5
 * @author Eoxia <[email protected]>
6
 * @since 0.1.0
7
 * @version 1.0.0
8
 * @copyright 2015-2018 Eoxia
9
 * @package EO_Framework\Core\Util
10
 */
11
12
namespace eoxia;
13
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
if ( ! class_exists( '\eoxia\Array_Util' ) ) {
19
20
	/**
21
	 * Gestion des tableaux
22
	 */
23
	class Array_Util extends \eoxia\Singleton_Util {
24
		/**
25
		 * Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util
26
		 *
27
		 * @since 0.1.0
28
		 * @version 1.0.0
29
		 */
30
		protected function construct() {}
31
32
		/**
33
		 * Compte le nombre de valeur dans un tableau avec récursivité en vérifiant que $match_element soit dans la valeur
34
		 *
35
		 * @since 1.0.0
36
		 * @version 1.0.0
37
		 *
38
		 * @param  array   $array         Les données pour la moulinette.
39
		 * @param  boolean $start         Initialise count avec le tableau du premier niveau.
40
		 * @param  array   $match_element Doit être un tableau contenant des integers.
41
		 * @return int                    Le nombre d'entrée
42
		 */
43
		public function count_recursive( $array, $start = true, $match_element = array() ) {
44
			$count = 0;
45
46
			if ( $start ) {
47
				$count = count( $array );
48
			}
49
50
			if ( ! empty( $array ) ) {
51
				foreach ( $array as $id => $_array ) {
52
					if ( is_array( $_array ) ) {
53
						if ( is_string( $id ) && ! empty( $match_element ) && in_array( $id, $match_element, true ) ) {
54
							$count += count( $_array );
55
						}
56
57
						$count += $this->count_recursive( $_array, false, $match_element );
58
					}
59
				}
60
			}
61
62
			return $count;
63
		}
64
65
		/**
66
		 * Forces à convertir les valeurs d'un tableau en integer.
67
		 *
68
		 * @since 0.1.0
69
		 * @version 1.0.0
70
		 *
71
		 * @param  array $array Le tableau à convertir en int.
72
		 * @return array        Le tableau converti en int.
73
		 */
74
		public function to_int( $array ) {
75
			if ( ! empty( $array ) ) {
76
				foreach ( $array as &$element ) {
77
					$element = (int) $element;
78
				}
79
			}
80
			return $array;
81
		}
82
83
		/**
84
		 * Déplaces l'index du tableau vers l'index $to_key.
85
		 *
86
		 * @since 0.5.0
87
		 * @version 1.0.0
88
		 *
89
		 * @param  Array   $array Les valeurs contenu dans le tableau. Le tableau ne doit pas être 2D.
90
		 * @param  mixed   $value Tous types de valeurs.
91
		 * @param  integer $to_key La clé qui vas être déplacer. Defaut 0
92
		 * @return Array   Le tableau.
93
		 */
94
		public function switch_key( $array, $value, $to_key = 0 ) {
95
			if ( empty( $array[ $to_key ] ) ) {
96
				return $array;
97
			}
98
99
			$index_founded = array_search( $value, $array, true );
100
101
			if ( false === $index_founded ) {
102
				return $array;
103
			}
104
105
			$tmp_val = $array[ $to_key ];
106
			$array[ $to_key ] = $array[ $index_founded ];
107
			$array[ $index_founded ] = $tmp_val;
108
109
			return $array;
110
		}
111
112
		/**
113
		 * Récursive wp_parse_args de WordPress.
114
		 *
115
		 * @since 1.0.0
116
		 * @version 1.0.0
117
		 *
118
		 * @param  mixed $a       Les données a mergées.
119
		 * @param  mixed $default Les données par défaut.
120
		 * @return array          Les données mergées.
121
		 */
122
		public function recursive_wp_parse_args( &$a, $default ) {
123
			$a       = (array) $a;
124
			$default = (array) $default;
125
126
			$result = $default;
127
			foreach ( $a as $k => $v ) {
128
				if ( is_array( $v ) && isset( $result[ $k ] ) ) {
129
					$result[ $k ] = $this->recursive_wp_parse_args( $v, $result[ $k ] );
130
				} else {
131
					$result[ $k ] = $v;
132
				}
133
			}
134
135
			return $result;
136
		}
137
	}
138
} // End if().
139