Passed
Pull Request — master (#82)
by David
02:40
created

StandardObjectStorage::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE
6
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
*/
21
22
namespace TheCodingMachine\TDBM;
23
24
/**
25
 * The StandardObjectStorage class is used to reference all beans that have been fetched from the database.
26
 * If a bean is requested twice from TDBM, the StandardObjectStorage is used to "cache" the bean.
27
 * The StandardObjectStorage acts like a big array. It is used if the "weakref" extension is not available.
28
 * Otherwise, the WeakrefObjectStorage is used instead.
29
 *
30
 * @author David Negrier
31
 */
32
class StandardObjectStorage implements ObjectStorageInterface
33
{
34
    /**
35
     * An array of fetched object, accessible via table name and primary key.
36
     * If the primary key is split on several columns, access is done by an array of columns, serialized.
37
     *
38
     * @var array<string, array<string, DbRow>>
39
     */
40
    private $objects = array();
41
42
    /**
43
     * Sets an object in the storage.
44
     *
45
     * @param string     $tableName
46
     * @param string|int $id
47
     * @param DbRow      $dbRow
48
     */
49
    public function set(string $tableName, $id, DbRow $dbRow): void
50
    {
51
        $this->objects[$tableName][$id] = $dbRow;
52
    }
53
54
    /**
55
     * Returns an object from the storage (or null if no object is set).
56
     *
57
     * @param string $tableName
58
     * @param string|int $id
59
     *
60
     * @return DbRow|null
61
     */
62
    public function get(string $tableName, $id) : ?DbRow
63
    {
64
        if (isset($this->objects[$tableName][$id])) {
65
            return $this->objects[$tableName][$id];
66
        } else {
67
            return null;
68
        }
69
    }
70
71
    /**
72
     * Removes an object from the storage.
73
     *
74
     * @param string $tableName
75
     * @param string|int $id
76
     */
77
    public function remove(string $tableName, $id): void
78
    {
79
        unset($this->objects[$tableName][$id]);
80
    }
81
}
82