* @return int|string|null
* Returns the key, which could be an int or a string, or NULL on failure.
*/
- public static function key($value, &$list) {
+ public static function key($value, $list) {
if (is_array($list)) {
$key = array_search($value, $list);
* @param string $seperator
* (optional) String to be appended after open/close tags.
*
- *
* @return string
* XML fragment representing $list.
*/
}
/**
- * @param $subset
- * @param $superset
+ * Is array A a subset of array B.
+ *
+ * @param array $subset
+ * @param array $superset
+ *
* @return bool
* TRUE if $subset is a subset of $superset
*/
}
/**
- * convert associative array names to values.
- * and vice-versa.
+ * Convert associative array names to values and vice-versa.
*
* This function is used by both the web form layer and the api. Note that
* the api needs the name => value conversion, also the view layer typically
* requires value => name conversion
+ *
+ * @param array $defaults
+ * @param string $property
+ * @param $lookup
+ * @param $reverse
+ *
+ * @return bool
*/
public static function lookupValue(&$defaults, $property, $lookup, $reverse) {
$id = $property . '_id';
$look = $reverse ? array_flip($lookup) : $lookup;
- //trim lookup array, ignore . ( fix for CRM-1514 ), eg for prefix/suffix make sure Dr. and Dr both are valid
+ // trim lookup array, ignore . ( fix for CRM-1514 ), eg for prefix/suffix make sure Dr. and Dr both are valid
$newLook = array();
foreach ($look as $k => $v) {
$newLook[trim($k, ".")] = $v;
return $result;
}
+ /**
+ * Iterates over a list of objects and executes some method on each.
+ *
+ * Comparison:
+ * - This is like array_map(), except it executes the objects' method
+ * instead of a free-form callable.
+ * - This is like Array::collect(), except it uses a method
+ * instead of a property.
+ *
+ * @param string $method
+ * The method to execute.
+ * @param array|Traversable $objects
+ * A list of objects.
+ * @param array $args
+ * An optional list of arguments to pass to the method.
+ *
+ * @return array
+ * Keys are the original keys of $objects; values are the method results.
+ */
+ public static function collectMethod($method, $objects, $args = array()) {
+ $result = array();
+ if (is_array($objects)) {
+ foreach ($objects as $key => $object) {
+ $result[$key] = call_user_func_array(array($object, $method), $args);
+ }
+ }
+ return $result;
+ }
+
/**
* Trims delimiters from a string and then splits it using explode().
*
/**
* Diff multidimensional arrays
- * ( array_diff does not support multidimensional array)
+ * (array_diff does not support multidimensional array)
*
* @param array $array1
* @param array $array2
$r[$last] = $value;
}
+ /**
+ * Convert a simple dictionary into separate key+value records.
+ *
+ * @param array $array
+ * Ex: array('foo' => 'bar').
+ * @param string $keyField
+ * Ex: 'key'.
+ * @param string $valueField
+ * Ex: 'value'.
+ * @return array
+ * Ex: array(
+ * 0 => array('key' => 'foo', 'value' => 'bar')
+ * ).
+ */
+ public static function toKeyValueRows($array, $keyField = 'key', $valueField = 'value') {
+ $result = array();
+ foreach ($array as $key => $value) {
+ $result[] = array(
+ $keyField => $key,
+ $valueField => $value,
+ );
+ }
+ return $result;
+ }
+
+ /**
+ * Convert array where key(s) holds the actual value and value(s) as 1 into array of actual values
+ * Ex: array('foobar' => 1, 4 => 1) formatted into array('foobar', 4)
+ *
+ * @param array $array
+ * @return void
+ */
+ public static function formatArrayKeys(&$array) {
+ $keys = array_keys($array, 1);
+ if (count($keys) > 1 ||
+ (count($keys) == 1 &&
+ (current($keys) > 1 ||
+ is_string(current($keys)) ||
+ (current($keys) == 1 && $array[1] == 1) // handle (0 => 4), (1 => 1)
+ )
+ )
+ ) {
+ $array = $keys;
+ }
+ }
+
}