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

109 lines
3.3 KiB
PHP

<?php
namespace Questionnaire\Model;
use Questionnaire\Configuration;
use Symfony\Component\Yaml\Yaml;
class ResultPeer extends QuestionnairePeer
{
protected static $results = array();
public static function retrieveResults()
{
if (!empty(self::$results)) {
return self::$results;
}
$yaml = self::getYaml();
$questions = isset($yaml['results']) ? $yaml['results'] : 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)
);
}
$result = new Result();
$collection[] = $result->hydrate(array_merge(array('id' => $k), $v));
}
return self::$results = $collection;
}
public static function retrieveResultById($id)
{
if (!is_int($id)) {
throw \InvalidArgumentException('You must provide a valid integer id.');
}
foreach (self::retrieveResults() 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['weightings'])) {
$errors[] = '"weightings" index must be defined (array).';
} else {
if (!is_array($entry['weightings'])) {
$errors[] = '"weightings" index must be defined as array.';
} else {
if (empty($entry['weightings'])) {
$errors[] = '"weightings" index value can not be empty.';
} else {
foreach ($entry['weightings'] as $k => $v) {
if (!is_int($k)) {
$errors[] = 'Weighting with key "'.$k.'" must have an integer key.';
}
if (!is_array($v)) {
$errors[] = 'Weighting value with key "'.$k.'" must be an array.';
} else {
foreach ($v as $q => $p) {
if (!is_bool($p) && !is_int($p)) {
$errors[] = 'Weighting value with key "'.$k.'" is invalid.';
}
}
}
}
}
}
}
// if (isset($entry['info'])) {
// if (!file_exists(sprintf('info/%s.md', $entry['info']))) {
// $errors[] = 'Info file does not exist.';
// }
// }
return !empty($errors) ? $errors : null;
}
}