ArrayGenerator::generateRandomInt()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace PragmaRX\Random\Generators;
4
5
trait ArrayGenerator
6
{
7
    protected $array = false;
8
9
    protected $items = [];
10
11
    protected $count = 1;
12
13
    /**
14
     * Set the array items count.
15
     *
16
     * @param $count
17
     * @return $this
18
     */
19 1
    public function count($count)
20
    {
21 1
        $this->count = $count;
22
23 1
        return $this;
24
    }
25
26
    /**
27
     * Generate random array elements.
28
     *
29
     * @return array
30
     */
31 1
    protected function generateArray()
32
    {
33 1
        $result = [];
34
35 1
        $last = count($this->items)-1;
36
37 1
        for ($counter = 1; $counter <= $this->count; $counter++) {
38 1
            $result[] = $this->items[$this->generateRandomInt(0, $last)];
39
        }
40
41 1
        if ($this->count == 1) {
42 1
            return $result[0];
43
        }
44
45 1
        return $result;
46
    }
47
48
    /**
49
     * Generate a random integer.
50
     *
51
     * @param int $start
52
     * @param int $end
53
     * @return int
54
     */
55
    abstract protected function generateRandomInt($start, $end);
56
}
57