<?php
/*
This script is developed by Arturs Sosins aka ar2rsawseen, http://code-snippets.co.cc
Fee free to distribute and modify code, but keep reference to its creator
This class uses Guitar chord API http://pargitaru.co.cc/api/.
It can find chord variations based on given data about chord
You can specify chord name, chord modification, or/and string/fret combination
*/
class chords
{
//bool if chord is special
private $special = false;
//chord modification
private $modf = "";
//chord name
private $chord_name = "";
//array of possible strings
private $string_arr = array("e2"=>"", "b"=>"", "g"=>"", "d"=>"", "a"=>"", "e"=>"");
//bool if any of search citeria are specified
private $request = false;
//storing errors
private $errors = array();
/*arrays for validating input or generating forms*/
//array with all possible chord modifications
private $modifications = array("minor", "major", "aug", "dim", "sus", "add9", "m6", "m7", "m9",
"maj7", "maj9", "mmaj7", "-5", "11", "13", "5", "6", "6add9",
"7", "7-5", "7maj5", "7sus4", "9");
//array with all possible chord names
private $chords = array("C", "C#", "Db", "D", "D#", "Eb", "E", "F", "F#", "Gb", "G", "G#", "Ab", "A" , "A#", "Bb", "B");
//array with all possible special chord names
private $special_chords = array("A/C#", "A/E", "A/F", "A/G", "A/G#", "Am/C", "Am/E", "Am/F",
"Am/F#", "Am/G", "Am/G#", "C/E", "C/F", "C/G", "D/A", "D/B",
"D/Bb", "D/C", "D/F#", "E/B", "E/C#", "E/D", "E/D#", "E/F",
"E/F#", "E/G", "E/G#", "Em/B", "Em/C#", "Em/D", "Em/D#",
"Em/F", "Em/F#", "Em/G", "Em/G#", "F/A", "F/C", "F/D", "F/D#",
"F/E", "F/G", "Fm/C", "G/B", "G/D", "G/E", "G/F", "G/F#");
//array with all possible fret values
private $fret_val = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "x", "");
//returns array with all chord modifications for generating input form
public function get_modifications(){
return $this->modifications;
}
/*setting chord modification search criteria
if modification exists sets modification returns true,
else sets error and returns false
ignores empty value*/
public function set_modification($value){
if(trim($value) != "")
{
if(in_array($value, $this->modifications))
{
$this->modf = urlencode($value);
$this->request = true;
return true;
}
else
{
$this->errors[] = "Invalid chord modification";
return false;
}
}
}
//returns array with all chord names for generating input form
public function get_chords(){
return $this->chords;
}
/*setting chord_name search criteria
checking if special is true
if yes validating for special chords and setting special true
else validating for standart chords and setting special to false
if chord_name validates, setting chord_name and returns true,
else sets error and returns false
ignores empty values*/
public function set_chord($value, $special = false){
if(trim($value) != "")
{
$value = strtoupper($value);
if($special)
{
if(in_array($value, $this->special_chords))
{
$this->chord_name = urlencode($value);
$this->special = true;
$this->request = true;
return true;
}
else
{
$this->errors[] = "Invalid special chord name";
return false;
}
}
else
{
if(in_array($value, $this->chords))
{
$this->chord_name = urlencode($value);
$this->special = false;
$this->request = true;
return true;
}
else
{
$this->errors[] = "Invalid chord name";
return false;
}
}
}
}
//returns array with all special chord names for generating input form
public function get_specials(){
return $this->special_chords;
}
/*setting string and frets to search by given array from thinnest string e to thickest string E
checking if array is passed, then if all array values ar valid
then checking if any of the values are not empty, so we should have something to request
and copying values to string_arr array
*/
public function set_strings($arr, $special = false){
if(is_array($arr))
{
$valid = true;
foreach($arr as $val)
{
if(!in_array(trim(strtolower($val)), $this->fret_val))
{
$valid = false;
break;
}
}
if(!$valid)
{
$this->errors[] = "Incorrect fret value. Frets can have values from 0 - 15 and value 'x' if fret is not used";
return false;
}
else if($special)
{
$value = current($arr);
foreach($this->string_arr as $string => $fret)
{
if(trim($value) != "")
{
$this->request = true;
}
$value = strtolower($value);
$this->string_arr[$string] = urlencode($value);
$value = next($arr);
$this->special = true;
}
}
else
{
$value = current($arr);
foreach($this->string_arr as $string => $fret)
{
if(trim($value) != "")
{
$this->request = true;
}
$value = strtolower($value);
$this->string_arr[$string] = urlencode($value);
$value = next($arr);
$this->special = false;
}
}
}
else
{
$this->errors[] = "set_strings parameter must be array";
return false;
}
}
/*
resets all search criterias to default data
if you set modification and chord name, and then without calling reset method
change chord name, modification will stay the same
*/
public function reset(){
$this->special = false;
$this->modf = "";
$this->chord_name = "";
$this->string_arr = array("e2"=>"", "b"=>"", "g"=>"", "d"=>"", "a"=>"", "e"=>"");
$this->request = false;
$this->errors = array();
}
//returns errors and empties error array
public function get_errors(){
$error = $this->errors;
$this->errors = array();
return $error;
}
//making request to api
//you can specify type json or xml for output value
//json is a default vaue
public function request($type = "json"){
if($this->request)
{
$url = "http://pargitaru.co.cc/api/";
if($this->special)
{
$url .= "?request=special";
}
else
{
$url .= "?request=chords";
if($this->modf != "")
{
$url .= "&modf=".$this->modf;
}
}
if($this->chord_name != "")
{
$url .= "&chord=".$this->chord_name;
}
foreach($this->string_arr as $string => $fret)
{
if(trim($fret) != "")
{
$url .= "&".$string."=".$fret;
}
}
if(in_array($type, array("json", "xml")))
{
$url .= "&type=".$type;
}
else
{
$type = "json";
}
//curl request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$chord_data = curl_exec($curl);
curl_close($curl);
//checking if returned error
if($type == "json")
{
$arr = json_decode($chord_data, true);
if(!is_array($arr['chords']))
{
$this->errors[] = $arr['chords'];
}
}
else
{
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $chord_data, $vals, $index);
xml_parser_free($xml_parser);
if($vals[0]['type'] == 'complete')
{
$this->errors[] = $vals[0]['value'];
}
}
return $chord_data;
}
else
{
$this->errors[] = "No search criterias specified";
return false;
}
}
}
?>