117 lines
2.2 KiB
PHP
117 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Questionnaire\Model;
|
|
|
|
class Question
|
|
{
|
|
protected $id;
|
|
|
|
protected $title;
|
|
|
|
protected $choices = array();
|
|
|
|
protected $required = array();
|
|
|
|
public function hydrate(array $data)
|
|
{
|
|
foreach ($data as $k => $v) {
|
|
$setter = 'set'.ucfirst($k);
|
|
|
|
if (method_exists($this, $setter)) {
|
|
$this->$setter($v);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setId($id)
|
|
{
|
|
$this->id = $id;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getId()
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function setTitle($title)
|
|
{
|
|
$this->title = $title;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getTitle()
|
|
{
|
|
return $this->title;
|
|
}
|
|
|
|
public function setChoices(array $choices)
|
|
{
|
|
$this->choices = $choices;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getChoices()
|
|
{
|
|
return $this->choices;
|
|
}
|
|
|
|
public function setRequired(array $required)
|
|
{
|
|
$this->required = $required;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getRequired()
|
|
{
|
|
return $this->required;
|
|
}
|
|
|
|
public function getRequirements()
|
|
{
|
|
return $this->getRequired();
|
|
}
|
|
|
|
public function hasRequirements()
|
|
{
|
|
return count($this->getRequirements()) > 0;
|
|
}
|
|
|
|
public function isCompatibleWithResponses(array $response)
|
|
{
|
|
if (!$this->hasRequirements()) {
|
|
return true;
|
|
}
|
|
|
|
foreach ($response as $questionId => $response) {
|
|
$question = QuestionPeer::retrieveQuestionById($questionId);
|
|
|
|
if (null === $question) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($this->getRequirements() as $requirement) {
|
|
list($requirementQuestionId, $requirementResponses) = $requirement;
|
|
|
|
if ($requirementQuestionId === $questionId) {
|
|
if (!is_array($requirementResponses)) {
|
|
$requirementResponses = array($requirementResponses);
|
|
}
|
|
|
|
if (!in_array($response, $requirementResponses)) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|