Pool::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace FForattini\Btrieve\Persistence;
4
5
use Exception;
6
7
abstract class Pool implements DatasetInterface
8
{
9
    protected $dataset;
10
11
    /**
12
     * Contructor.
13
     */
14
    public function __construct()
15
    {
16
        $this->init();
17
    }
18
19
    /**
20
     * Initializes a pool of elements.
21
     *
22
     * @return Pool
23
     */
24
    public function init()
25
    {
26
        $this->dataset = [];
27
28
        return $this;
29
    }
30
31
    /**
32
     * Erase all information from dataset.
33
     *
34
     * @return Pool
35
     */
36
    public function reset()
37
    {
38
        foreach ($this->dataset as $index => $element) {
39
            unset($this->dataset[$index]);
40
        }
41
        unset($this->dataset);
42
        $this->initDataset();
0 ignored issues
show
Bug introduced by
The method initDataset() does not seem to exist on object<FForattini\Btrieve\Persistence\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
44
        return $this;
45
    }
46
47
    /**
48
     * Checks if this element exists in this dataset.
49
     *
50
     * @param int $index
51
     *
52
     * @return bool
53
     */
54
    public function has($index)
55
    {
56
        if (isset($this->dataset[$index])) {
57
            return true;
58
        }
59
60
        return false;
61
    }
62
63
    /**
64
     * @aliasof has
65
     *
66
     * @param int $index
67
     *
68
     * @return bool
69
     */
70
    public function exists($index)
71
    {
72
        return $this->has($index);
73
    }
74
75
    /**
76
     * Returns the element in this index from the dataset.
77
     *
78
     * @param int $index
79
     *
80
     * @return mixed
81
     */
82
    public function elem($index)
83
    {
84
        if ($this->has($index)) {
85
            return $this->dataset[$index];
86
        }
87
        throw new Exception('Element doesnt exist in this dataset.');
88
    }
89
90
    /**
91
     * Function will show how to deal with the element.
92
     *
93
     * @param array $element
94
     *
95
     * @return mixed
96
     */
97
    abstract public function cast($element);
98
99
    /**
100
     * Adds an element to the dataset.
101
     *
102
     * @param array $element
103
     */
104
    public function add($element)
105
    {
106
        $this->dataset[] = $this->cast($element);
107
    }
108
109
    /**
110
     * Return number of elements.
111
     *
112
     * @return int
113
     */
114
    public function count()
115
    {
116
        return count($this->dataset);
117
    }
118
}
119