$value ) { $record->setFieldValue( $key, $value ); } } protected static function get_record_data( array $data ) { $record_data = []; foreach ( static::$fields as $specs ) { $key = @static::$fields_map[$specs['name']]; if ( $key ) { $value = ( $key ) ? @$data[ $key ] : null; } else { $value = @$specs['default']; } $record_data[$specs['name']] = call_user_func_array( [ static::get_module_class(), $specs['callback'] ], [ $value, $specs, $data ] ); } $record_data = static::data_filter( $record_data ); return $record_data; } protected static function get_module_class() { return 'FQP\\ZOHO\\' . static::$module; } protected static function data_filter(array $data) { $exceptions = static::$data_filter_exceptions; $data = array_filter( $data, function ( $v, $k ) use ( $exceptions ) { if (in_array($k, $exceptions)) { return true; } return (bool) $v; }, ARRAY_FILTER_USE_BOTH ); return $data; } protected static function check_length( $value, $length ) { if ( ! $length ) { return true; } if ( ! is_array( $length ) ) { $length = [ 0, intval( $length ) ]; } $value_length = strlen( strval( $value ) ); return ( $value_length >= $length[0] && $value_length <= $length[1] ); } protected static function align_length( $value, $length ) { if ( ! is_array( $length ) ) { $length = [ 1, intval( $length ) ]; } $start = $length[0] - 1; if ( 0 > $start ) { $start = 0; } $length = $length[1]; return substr( $value, $start, $length); } public static function text( $value, array $specs ) { $value = strval( $value ); if ( '' === $value ) { $value = strval( @$specs['default'] ); } if ( @$specs['required'] && '' === $value ) { throw new Exception( "Field '{$specs['name']}' is required" ); } if ( ! static::check_length( $value, @$specs['length'] ) ) { $value = static::align_length( $value, $specs['length'] ); } return $value; } public static function decimal( $value, array $specs ) { $value = floatval( $value ); if ( 0 == $value ) { $value = floatval( @$specs['default'] ); } if ( 0 == $value ) { return ''; } if ( @$specs['required'] && 0 == $value ) { throw new Exception( "Field '{$specs['name']}' is required" ); } $value = number_format( $value, 2, '.', '' ); if ( ! static::check_length( $value, @$specs['length'] ) ) { throw new Exception( "Field '{$specs['name']}' is too long" ); } return $value; } public static function email( $value, array $specs ) { $value = strval( $value ); if ( ! $value ) { return ''; } if ( ! preg_match( FQP\FQP::EMAIL_REGEX, $value ) ) { throw new Exception( "Field '{$specs['name']}' is in wrong format" ); } if ( ! static::check_length( $value, @$specs['length'] ) ) { throw new Exception( "Field '{$specs['name']}' is too long" ); } return $value; } public static function url( $value, array $specs ) { $value = strval( $value ); if ( ! $value ) { return ''; } if ( ! preg_match( static::$url_pattern, $value ) ) { throw new Exception( "Field '{$specs['name']}' is in wrong format" ); } if ( ! static::check_length( $value, @$specs['length'] ) ) { throw new Exception( "Field '{$specs['name']}' is too long" ); } return $value; } public static function int( $value, array $specs ) { $value = abs( intval( $value ) ); if ( 0 == $value ) { $value = intval( @$specs['default'] ); } if ( @$specs['required'] && 0 == $value ) { throw new Exception( "Field '{$specs['name']}' is required" ); } $value = number_format( $value, 0, '.', '' ); if ( ! static::check_length( $value, @$specs['length'] ) ) { throw new Exception( "Field '{$specs['name']}' is too long" ); } return $value; } public static function number( $value, array $specs ) { $value = preg_replace( '/[^\d]+/', '', $value ); if ( empty( $value ) ) { $value = @$specs['default']; } if ( @$specs['required'] && empty( $value ) ) { throw new Exception( "Field '{$specs['name']}' is required" ); } if ( ! static::check_length( $value, @$specs['length'] ) ) { throw new Exception( "Field '{$specs['name']}' is too long" ); } return $value; } public static function checkbox( $value, array $specs ) { if ( ! $value && @$specs['default'] ) { return $specs['default']; } return ('y' == $value ) ? true : false; } public static function date( $value, array $specs ) { $date = null; if ( @$specs['required'] && '' == $value ) { $date = date( 'Y-m-d' ); } else { if ( $value ) { $date = date( 'Y-m-d', strtotime( $value ) ); } } return ( null !== $date ) ? $date : @$specs['default']; } public static function umlaut( $value ) { return str_replace( [ 'ä', 'ü' ], [ 'ae', 'ue' ], $value ); } }