Certificationy\Certification\Loader::init PHP Method

init() public static method

Returns a new set of randomized questions
public static init ( integer $number, array $categories, $path ) : Certificationy\Certification\Set
$number integer
$categories array
return Certificationy\Certification\Set
    public static function init($number, array $categories, $path)
    {
        $data = self::prepareFromYaml($categories, $path);
        if (!$data) {
            return new Set(array());
        }
        $dataMax = count($data) - 1;
        $questions = array();
        for ($i = 0; $i < $number; $i++) {
            do {
                $random = rand(0, $dataMax);
            } while (isset($questions[$random]) && count($questions) < $dataMax);
            $item = $data[$random];
            $answers = array();
            foreach ($item['answers'] as $dataAnswer) {
                $answers[] = new Answer($dataAnswer['value'], $dataAnswer['correct']);
            }
            if (!isset($item['shuffle']) || true === $item['shuffle']) {
                shuffle($answers);
            }
            $questions[$random] = new Question($item['question'], $item['category'], $answers);
        }
        return new Set($questions);
    }

Usage Example

 /**
  * Tests loader
  */
 public function testInitialization()
 {
     $set = Loader::init(5, array(), $this->configFile);
     $this->assertInstanceOf('Certificationy\\Certification\\Set', $set, 'Should return an instance of set');
     $this->assertCount(5, $set->getQuestions(), 'Should return 5 questions');
     $this->assertNull($set->getAnswers());
 }
All Usage Examples Of Certificationy\Certification\Loader::init