summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/zfdbstore/src/DbStorable.php
blob: 4bd1783167c46c77f138f8b74a094cc8b397a985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php

namespace gipfl\ZfDbStore;

use RuntimeException;

/**
 * DbStorable
 *
 * This trait provides all you need to create an object implementing the
 * DbStorableInterface
 */
trait DbStorable
{
    use Storable;

    // protected $tableName;

    public function getTableName()
    {
        if (isset($this->tableName)) {
            return $this->tableName;
        } else {
            throw new RuntimeException('A DbStorable needs a tableName');
        }
    }

    public function hasAutoIncKey()
    {
        return $this->getAutoIncKeyName() !== null;
    }

    public function getAutoIncKeyName()
    {
        if (isset($this->autoIncKeyName)) {
            return $this->autoIncKeyName;
        } else {
            return null;
        }
    }

    protected function requireAutoIncKeyName()
    {
        $key = $this->getAutoIncKeyName();
        if ($key === null) {
            throw new RuntimeException('This DbStorable has no autoinc key');
        }

        return $key;
    }

    public function getAutoIncId()
    {
        $key = $this->requireAutoIncKeyName();
        if (isset($this->properties[$key])) {
            return (int) $this->properties[$key];
        }

        return null;
    }

    protected function forgetAutoIncId()
    {
        $key = $this->requireAutoIncKeyName();
        if (isset($this->properties[$key])) {
            $this->properties[$key] = null;
        }

        return $this;
    }

    public function __invoke($properties = [])
    {
        $storable = new static();
        $storable->setProperties($properties);

        return $storable;
    }
}