Completed
Push — master ( cc2c1e...ad65a7 )
by Renato
07:15
created

ModelResultset   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 66
rs 10
wmc 6
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A initialize() 0 8 2
A getPrototype() 0 4 1
A current() 0 10 2
1
<?php
2
3
namespace NwLaravel\Resultset;
4
5
use PDOStatement;
6
use RuntimeException;
7
use \Illuminate\Database\Eloquent\Model as EloquentModel;
8
9
/**
10
 * Class ModelResultset
11
 */
12
class ModelResultset extends AbstractResultset
13
{
14
    /**
15
     * @var \Illuminate\Database\Eloquent\Model
16
     */
17
    protected $model = null;
18
19
    /**
20
     * @var \Illuminate\Database\Eloquent\Model
21
     */
22
    private $prototype = null;
23
24
    /**
25
     * Initialize
26
     *
27
     * @param PDOStatement  $statement PDO Statement
28
     * @param EloquentModel $model     Model
29
     */
30
    public function __construct(PDOStatement $statement, EloquentModel $model)
31
    {
32
        $this->statement = $statement;
33
        $this->model = $model;
34
35
        $this->initialize();
36
    }
37
38
    /**
39
     * Initialize
40
     *
41
     * @return void
42
     */
43
    public function initialize()
44
    {
45
        if (! $this->initialized) {
46
            $this->prototype = $this->model->newFromBuilder();
47
        }
48
49
        $this->initialized = true;
50
    }
51
52
    /**
53
     * Get Prototype
54
     *
55
     * @return Illuminate\Database\Eloquent\Model
56
     */
57
    public function getPrototype()
58
    {
59
        return $this->prototype;
60
    }
61
62
    /**
63
     * Get the data
64
     *
65
     * @return object|bool
66
     */
67
    public function current()
68
    {
69
        if (!$data = parent::current()) {
70
            return false;
71
        }
72
73
        $newInstance = clone $this->getPrototype();
74
        $newInstance->setRawAttributes((array) $data, true);
75
        return $newInstance;
76
    }
77
}
78