linux-questionnaire/src/Questionnaire/Model/QuestionPeer.php
2015-03-02 20:07:17 +01:00

137 lines
4.7 KiB
PHP

<?php
namespace Questionnaire\Model;
use Questionnaire\Application;
use Symfony\Component\Yaml\Yaml;
class QuestionPeer extends QuestionnairePeer
{
protected static $questions = array();
public static function retrieveQuestions()
{
if (!empty(self::$questions)) {
return self::$questions;
}
$yaml = self::getYaml();
$questions = isset($yaml['questions']) ? $yaml['questions'] : array();
$collection = [];
foreach ($questions as $k => $v) {
if ($errors = self::validateYamlEnty($k, $v)) {
throw new \RuntimeException(
'Invalid entry for index "'.$k.'" is not valid:'.PHP_EOL.implode(PHP_EOL, $errors)
);
}
$question = new Question();
$collection[] = $question->hydrate(array_merge(array('id' => $k), $v));
}
if (empty($collection)) {
throw new \RuntimeException('At least one question must be defined.');
}
return self::$questions = $collection;
}
public static function retrieveQuestionById($id)
{
if (!is_int($id)) {
throw \InvalidArgumentException('You must provide a valid integer id.');
}
foreach (self::retrieveQuestions() as $d) {
if ($d->getId() === $id) {
return $d;
}
}
return null;
}
protected static function validateYamlEnty($key, $entry)
{
$errors = [];
if (!is_int($key)) {
$errors[] = 'The key must be a integer.';
}
if (!isset($entry['title'])) {
$errors[] = '"title" index must be defined (string).';
} else {
if (!is_string($entry['title'])) {
$errors[] = '"title" index must be defined as string.';
} else {
if (!trim($entry['title'])) {
$errors[] = '"title" index value can not be empty.';
}
}
}
if (empty($entry['choices'])) {
$errors[] = '"choices" index must be defined (array).';
} else {
if (!is_array($entry['choices'])) {
$errors[] = '"choices" index must be defined as array.';
} else {
if (empty($entry['choices'])) {
$errors[] = '"choices" index value can not be empty.';
} else {
foreach ($entry['choices'] as $k => $v) {
if (!is_int($k)) {
$errors[] = 'Choice with key "'.$k.'" must have an integer key.';
}
if (!is_string($v)) {
$errors[] = 'Choice value with key "'.$k.'" must be a string.';
} else {
if (empty($v)) {
$errors[] = 'Choice value with key "'.$k.'" cannot be empty.';
}
}
}
}
}
}
if (isset($entry['required'])) {
if (!is_array($entry['required'])) {
$errors[] = '"required" index must be defined as array.';
}
if (empty($entry['required'])) {
$errors[] = '"required" index value can not be empty if defined.';
} else {
foreach ($entry['required'] as $k => $v) {
if (!is_array($v)) {
$errors[] = 'At "required" index, value with key "'.$k.'" must be an array.';
} else {
if (count($v) !== 2) {
$errors[] = 'At "required" index, value with key "'.$k.'" must have 2 values: question index and choice index.';
} else {
if (!is_int($v[0])) {
$errors[] = 'At "required" index, value with key "'.$k.'" must have a integer for question\'s index.';
} else {
if ($v[0] >= $key) {
$errors[] = 'At "required" index, value with key "'.$k.'" must have a value smaller than "'.$key.'".';
}
}
if (!is_int($v[1]) && !is_array($v[1])) {
$errors[] = 'At "required" index, value with key "'.$k.'" must have a integer or array for choice\'s index.';
}
}
}
}
}
}
return !empty($errors) ? $errors : null;
}
}