Skip to content

Instantly share code, notes, and snippets.

@Tocacar
Created December 16, 2013 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tocacar/7985959 to your computer and use it in GitHub Desktop.
Save Tocacar/7985959 to your computer and use it in GitHub Desktop.
//answer entity class
/**
* Set answer
*
* If answer is an array, converts to
* string with 'CSV:' prepended. This
* value is used to detect whether or not
* to convert answer back to array for form
* choice widget (checkbox requires array)
*
* @param string $answer
* @return Answer
*/
public function setAnswer($answer)
{
if(is_array($answer)){
$answer = implode (',', $answer);
$answer = 'CSV:'.$answer;
}
$this->answer = $answer;
return $this;
}
/**
* Set answer as array
*
* Converts string answer into array
*
* @param $answer
* @return $this
*/
public function setAnswerAsArray($answer)
{
$answer = str_replace('CSV:', '', $answer);
$this->answer = explode(',', $answer);
return $this;
}
/**
* Get answer
*
* @return string
*/
public function getAnswer()
{
return $this->answer;
}
// event listener that sets data in form before it is displayed
if(substr($answer->getAnswer(), 0, 4) == "CSV:"){
$answer->setAnswerAsArray($answer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment