1: <?php
2:
3: namespace RedBeanPHP\Util;
4:
5: use RedBeanPHP\OODB as OODB;
6: use RedBeanPHP\OODBBean as OODBBean;
7: use RedBeanPHP\RedException as RedException;
8:
9: /**
10: * Array Tool Helper
11: *
12: * This code was originally part of the facade, however it has
13: * been decided to remove unique features to service classes like
14: * this to make them available to developers not using the facade class.
15: *
16: * This is a helper or service class containing frequently used
17: * array functions for dealing with SQL queries.
18: *
19: * @file RedBeanPHP/Util/ArrayTool.php
20: * @author Gabor de Mooij and the RedBeanPHP Community
21: * @license BSD/GPLv2
22: *
23: * @copyright
24: * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
25: * This source file is subject to the BSD/GPLv2 License that is bundled
26: * with this source code in the file license.txt.
27: */
28: class ArrayTool
29: {
30: /**
31: * Generates question mark slots for an array of values.
32: *
33: * @param array $array array to generate question mark slots for
34: *
35: * @return string
36: */
37: public static function genSlots( $array, $template = NULL )
38: {
39: $str = count( $array ) ? implode( ',', array_fill( 0, count( $array ), '?' ) ) : '';
40: return ( is_null( $template ) || $str === '' ) ? $str : sprintf( $template, $str );
41: }
42:
43: /**
44: * Flattens a multi dimensional bindings array for use with genSlots().
45: *
46: * @param array $array array to flatten
47: * @param array $result result array parameter (for recursion)
48: *
49: * @return array
50: */
51: public static function flat( $array, $result = array() )
52: {
53: foreach( $array as $value ) {
54: if ( is_array( $value ) ) $result = self::flat( $value, $result );
55: else $result[] = $value;
56: }
57: return $result;
58: }
59: }
60: