\n\t"; foreach ( $xml_data as $key => $value ) { if ( ! $value ) { continue; } $key = esc_attr( $key ); $value = esc_html( $value ); $xml .= "\n\t\t{$value}"; } $xml .= "\n\t\n"; return $xml; } protected static function get_module_class() { return 'FQP\\ZOHO\\' . static::$module; } protected static function get_xml_data( array $data ) { $xml_data = []; foreach ( static::$fields as $specs ) { $key = @static::$fields_map[$specs['name']]; if ( $key ) { $value = ( $key ) ? @$data[ $key ] : null; } else { $value = @$specs['default']; } $xml_data[$specs['name']] = call_user_func_array( [ static::get_module_class(), $specs['callback'] ], [ $value, $specs, $data ] ); } $xml_data = array_filter( $xml_data ); return $xml_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 = [ 0, intval( $length ) ]; } return substr( $value, $length[0] - 1, $length[1] ); } 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( static::$email_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 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 ); } }