@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Array manipulation library.
|
||||
*
|
||||
* @author Time.ly Network, Inc.
|
||||
* @since 2.0
|
||||
* @package Ai1EC
|
||||
* @subpackage Ai1EC.Primitive
|
||||
*/
|
||||
class Ai1ec_Primitive_Array {
|
||||
|
||||
/**
|
||||
* Optionally explode given value.
|
||||
*
|
||||
* Perform explosion only if given value is not an array.
|
||||
*
|
||||
* @param char $separator Entities separator value.
|
||||
* @param string|array $input Allegedly string, but might be an array.
|
||||
*
|
||||
* @return array List of values.
|
||||
*/
|
||||
static public function opt_explode( $separator, $input ) {
|
||||
if ( ! is_array( $input ) ) {
|
||||
$input = explode( $separator, $input );
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge two arrays recursively maintaining key type as long as possible
|
||||
*
|
||||
* Method similar to array_merge_recursive, although it does not cast non
|
||||
* array value to array, unless one of arguments is an array.
|
||||
* Merge product is produced only on two arrays, not unlimited many.
|
||||
*
|
||||
* @param array $arr1 First (base) array to merge
|
||||
* @param array $arr2 Second (ammendment) array to merge
|
||||
*
|
||||
* @return array Merge product
|
||||
*/
|
||||
static public function deep_merge( array $arr1, array $arr2 ) {
|
||||
$result = array();
|
||||
foreach ( $arr1 as $key => $value ) {
|
||||
self::_merge_value( $result, $key, $value );
|
||||
if ( isset( $arr2[$key] ) ) {
|
||||
if ( is_array( $result[$key] ) || is_array( $arr2[$key] ) ) {
|
||||
$result[$key] = (array)$result[$key];
|
||||
$arr2[$key] = (array)$arr2[$key];
|
||||
$result[$key] = self::deep_merge(
|
||||
$result[$key],
|
||||
$arr2[$key]
|
||||
);
|
||||
} else {
|
||||
self::_merge_value( $result, $key, $arr2[$key] );
|
||||
}
|
||||
}
|
||||
unset( $arr2[$key] );
|
||||
}
|
||||
foreach ( $arr2 as $key => $value ) {
|
||||
self::_merge_value( $result, $key, $value );
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject value into merge array
|
||||
*
|
||||
* If key is numeric (appears to be integer) - value is pushed
|
||||
* into array, otherwise added under given key.
|
||||
*
|
||||
* @param array $result Reference to merge array
|
||||
* @param string|int $key Key to use for merge
|
||||
* @param mixed $value Value to add under key
|
||||
*
|
||||
* @return bool Success If it is not true - something wrong happened
|
||||
*/
|
||||
static protected function _merge_value( array& $result, $key, $value ) {
|
||||
if ( is_int( $key ) || ctype_digit( $key ) ) {
|
||||
$result[] = $value;
|
||||
return true;
|
||||
}
|
||||
$result[$key] = $value;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Integers manipulation class.
|
||||
*
|
||||
* @author Time.ly Network, Inc.
|
||||
* @since 2.0
|
||||
* @package Ai1EC
|
||||
* @subpackage Ai1EC.Primitive
|
||||
*/
|
||||
final class Ai1ec_Primitive_Int {
|
||||
|
||||
/**
|
||||
* Cast input as non-negative integer.
|
||||
*
|
||||
* @param string $input Arbitrary scalar input.
|
||||
*
|
||||
* @return int Non-negative integer parsed from input.
|
||||
*/
|
||||
public function positive( $input ) {
|
||||
$input = (int)$input;
|
||||
if ( $input < 1 ) {
|
||||
return 0;
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* map_to_integer method
|
||||
*
|
||||
* Return positive integer values from given array.
|
||||
*
|
||||
* @param array $input Allegedly list of positive integers
|
||||
*
|
||||
* @return array List of positive integers
|
||||
*/
|
||||
static public function map_to_integer( array $input ) {
|
||||
$output = array();
|
||||
foreach ( $input as $value ) {
|
||||
$value = (int)$value;
|
||||
if ( $value > 0 ) {
|
||||
$output[] = $value;
|
||||
}
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* convert_to_int_list method
|
||||
*
|
||||
* Convert given input to array of positive integers.
|
||||
*
|
||||
* @param char $separator Value used to separate integers, if any
|
||||
* @param array $input Allegedly list of positive integers
|
||||
*
|
||||
* @return array List of positive integers
|
||||
*/
|
||||
static public function convert_to_int_list( $separator, $input ) {
|
||||
return self::map_to_integer(
|
||||
Ai1ec_Primitive_Array::opt_explode( $separator, $input )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* db_bool method
|
||||
*
|
||||
* Convert value to MySQL boolean
|
||||
*
|
||||
* @param int|bool $value
|
||||
*
|
||||
* @return int Value to use as MySQL boolean value
|
||||
*/
|
||||
static public function db_bool( $value ) {
|
||||
return (int)(bool)intval( $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* index method
|
||||
*
|
||||
* Get valid integer index for given input.
|
||||
*
|
||||
* @param int $value User input expected to be integer
|
||||
* @param int $limit Lowest acceptable value
|
||||
* @param int $default Returned when value is bellow limit [optional=NULL]
|
||||
*
|
||||
* @return int Valid value
|
||||
*/
|
||||
static public function index( $value, $limit = 0, $default = NULL ) {
|
||||
$value = (int)$value;
|
||||
if ( $value < $limit ) {
|
||||
return $default;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* casts all the values of the array to int
|
||||
*
|
||||
* @param array $data
|
||||
* @return array:
|
||||
*/
|
||||
static public function cast_array_values_to_int( array $data ) {
|
||||
foreach( $data as &$value ) {
|
||||
$value = (int) $value;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user