src/Dcarrith/LxMPD/LxMPD.php
private function parse( $response, $command = '', $args = array() ) {

		// This is the array for storing all the parsed output
                $parsed = array();

		// If the response is a boolean, and the command is one that expects a boolean response, then return the response
		if( is_bool($response) ) {

			if( in_array( $command, $this->_responseShouldBeBoolean )) {

				return $response;

			} else {

				// If the command isn't expecting a boolean result, then we need to set the response back to an empty array
				$response = array();
			}
		}

		// If the response from MPD was an empty array, then just return the empty parsed array
		if( !count( $response ) ) {
			return $parsed;
		}

		switch( $command ) {

			// This will parse out a list of something like artists or albums into a simple array of values
			case 'list' :
			case 'listplaylist' :
			case 'listplaylists' :

				foreach( $response as $line ) {

					// Get the key value pairs from the line of output
					preg_match('/(.*?):\s(.*)/', $line, $matches);

					if( count($matches) != 3 ) {

						continue;
					}

					// Put the cleaned up matched pieces into the variables we'll be using
					list( $subject, $key, $value ) = $matches;

					// listplaylists requires special treatment
					if( $command == "listplaylists") {

						// We only care about the elements with the key 'playlist'
						if( $key == "playlist" ) {

							// We only need an array of playlist names
							$parsed[] = $value;
						}

					} else {

						// For playlists that aren't the current playlist, we only need an array of values
						$parsed[] = $value;
					}
				}

				return $parsed;

				break;

			// listplaylistinfo
			// playlistinfo
			// statistics
			// stats
			// idle
			default :

				$items = array();

				foreach( $response as $line ) {

					// Get the key value pairs from the line of output
					preg_match('/(.*?):\s(.*)/', $line, $matches);

					// Put the cleaned up matched pieces into the variables we'll be using
					list( $subject, $key, $value ) = $matches;

					// The response output from certain commands like statistics and stats will never
					// meet this condition, so therefore the items array will always be built as an
					// associative array with key => value pairs.  The response output from commands
					// like list, or list
					if( array_key_exists( $key, $items ) ) {

						// Append the track array onto the array of parsedOutput to be returned
						$parsed[] = $items;

						// Initialize a new track to compile
						$items = array( $key => $value );

					} else {

						// Set the key value pair in the track array
						$items[ $key ] = $value;
					}
				}

				if( in_array( $command, $this->_expectArrayOutput ) ) {

					// Append the last items array onto the array of parsedOutput to return
					$parsed[] = $items;

				} else {

					$parsed = $items;
				}

				// If the output contains one or more tracks, then we can filter and report on missing tags if needed
				if( in_array( $command, $this->_outputContainsTracks )) {

					if( $this->_tagFiltering ) {

						$parsed = $this->filterOutUnwantedTags( $parsed );
					}

					if( $this->_throwMissingTagExceptions ) {

						$this->reportOnMissingTags( $command, $parsed );
					}
				}

				return $parsed;

				break;
		}

		return false;
        }
private function parse( $response, $command = '', $args = array() ) {

		// This is the array for storing all the parsed output
                $parsed = array();

		// If the response is a boolean, and the command is one that expects a boolean response, then return the response
		if( is_bool($response) ) {

			if( in_array( $command, $this->_responseShouldBeBoolean )) {

				return $response;

			} else {

				// If the command isn't expecting a boolean result, then we need to set the response back to an empty array
				$response = array();
			}
		}

		// If the response from MPD was an empty array, then just return the empty parsed array
		if( !count( $response ) ) {
			return $parsed;
		}

		switch( $command ) {

			// This will parse out a list of something like artists or albums into a simple array of values
			case 'list' :
			case 'listplaylist' :
			case 'listplaylists' :

				foreach( $response as $line ) {

					// Get the key value pairs from the line of output
					preg_match('/(.*?):\s(.*)/', $line, $matches);

					if( count($matches) != 3 ) {

						continue;
					}

					// Put the cleaned up matched pieces into the variables we'll be using
					list( $subject, $key, $value ) = $matches;

					// listplaylists requires special treatment
					if( $command == "listplaylists") {

						// We only care about the elements with the key 'playlist'
						if( $key == "playlist" ) {

							// We only need an array of playlist names
							$parsed[] = $value;
						}

					} else {

						// For playlists that aren't the current playlist, we only need an array of values
						$parsed[] = $value;
					}
				}

				return $parsed;

				break;

			// listplaylistinfo
			// playlistinfo
			// statistics
			// stats
			// idle
			default :

				$items = array();

				foreach( $response as $line ) {

					// Get the key value pairs from the line of output
					preg_match('/(.*?):\s(.*)/', $line, $matches);

					// Put the cleaned up matched pieces into the variables we'll be using
					list( $subject, $key, $value ) = $matches;

					// The response output from certain commands like statistics and stats will never
					// meet this condition, so therefore the items array will always be built as an
					// associative array with key => value pairs.  The response output from commands
					// like list, or list
					if( array_key_exists( $key, $items ) ) {

						// Append the track array onto the array of parsedOutput to be returned
						$parsed[] = $items;

						// Initialize a new track to compile
						$items = array( $key => $value );

					} else {

						// Set the key value pair in the track array
						$items[ $key ] = $value;
					}
				}

				if( in_array( $command, $this->_expectArrayOutput ) ) {

					// Append the last items array onto the array of parsedOutput to return
					$parsed[] = $items;

				} else {

					$parsed = $items;
				}

				// If the output contains one or more tracks, then we can filter and report on missing tags if needed
				if( in_array( $command, $this->_outputContainsTracks )) {

					if( $this->_tagFiltering ) {

						$parsed = $this->filterOutUnwantedTags( $parsed );
					}

					if( $this->_throwMissingTagExceptions ) {

						$this->reportOnMissingTags( $command, $parsed );
					}
				}

				return $parsed;

				break;
		}

		return false;
        }
private function parse ( $ response , $ command = '' , $ args = array ( ) ) { // This is the array for storing all the parsed output $ parsed = array ( ) ; // If the response is a boolean, and the command is one that expects a boolean response, then return the response if ( is_bool ( $ response ) ) { if ( in_array ( $ command , $ this -> _responseShouldBeBoolean ) ) { return $ response ; } else { // If the command isn't expecting a boolean result, then we need to set the response back to an empty array $ response = array ( ) ; } } // If the response from MPD was an empty array, then just return the empty parsed array if ( ! count ( $ response ) ) { return $ parsed ; } switch ( $ command ) { // This will parse out a list of something like artists or albums into a simple array of values case 'list' : case 'listplaylist' : case 'listplaylists' : foreach ( $ response as $ line ) { // Get the key value pairs from the line of output preg_match ( '/(.*?):\s(.*)/' , $ line , $ matches ) ; if ( count ( $ matches ) != 3 ) { continue ; } // Put the cleaned up matched pieces into the variables we'll be using list ( $ subject , $ key , $ value ) = $ matches ; // listplaylists requires special treatment if ( $ command == "listplaylists" ) { // We only care about the elements with the key 'playlist' if ( $ key == "playlist" ) { // We only need an array of playlist names $ parsed [ ] = $ value ; } } else { // For playlists that aren't the current playlist, we only need an array of values $ parsed [ ] = $ value ; } } return $ parsed ; break ; // listplaylistinfo // playlistinfo // statistics // stats // idle default : $ items = array ( ) ; foreach ( $ response as $ line ) { // Get the key value pairs from the line of output preg_match ( '/(.*?):\s(.*)/' , $ line , $ matches ) ; // Put the cleaned up matched pieces into the variables we'll be using list ( $ subject , $ key , $ value ) = $ matches ; // The response output from certain commands like statistics and stats will never // meet this condition, so therefore the items array will always be built as an // associative array with key => value pairs.  The response output from commands // like list, or list if ( array_key_exists ( $ key , $ items ) ) { // Append the track array onto the array of parsedOutput to be returned $ parsed [ ] = $ items ; // Initialize a new track to compile $ items = array ( $ key => $ value ) ; } else { // Set the key value pair in the track array $ items [ $ key ] = $ value ; } } if ( in_array ( $ command , $ this -> _expectArrayOutput ) ) { // Append the last items array onto the array of parsedOutput to return $ parsed [ ] = $ items ; } else { $ parsed = $ items ; } // If the output contains one or more tracks, then we can filter and report on missing tags if needed if ( in_array ( $ command , $ this -> _outputContainsTracks ) ) { if ( $ this -> _tagFiltering ) { $ parsed = $ this -> filterOutUnwantedTags ( $ parsed ) ; } if ( $ this -> _throwMissingTagExceptions ) { $ this -> reportOnMissingTags ( $ command , $ parsed ) ; } } return $ parsed ; break ; } return false ; }
Parses an array of output lines from MPD into a common array format
@param array $response the output read from the connection to MPD
@return mixed (string || array)
Parses an array of output lines from MPD into a common array format