-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_dbAccess.php
More file actions
103 lines (79 loc) · 2.33 KB
/
_dbAccess.php
File metadata and controls
103 lines (79 loc) · 2.33 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/orm/blob/master/LICENSE
*/
namespace Quid\Orm;
// _dbAccess
// trait that grants database access to the class using
trait _dbAccess
{
// dynamique
protected $db = null; // objet db, peut être objet ou string
// serialize
// serialize un objet
// envoie une exception si l'objet db est stocké en objet (ne peut pas être serialize)
final public function __serialize():array
{
if($this->db instanceof Pdo)
static::throw('cannotSerializeDbObject');
return parent::__serialize();
}
// setDb
// lie une base de donnée à l'objet
// si la base de donnée est dans inst, met le inst name, sinon met l'objet
final protected function setDb(Pdo $db):void
{
if($db instanceof Db)
{
if($db->inInst())
$this->db = $db->instName();
else
$this->db = $db;
}
else
$this->db = $db;
}
// hasDb
// retourne vrai si une base de donnée est lié à l'objet (via string ou objet)
final public function hasDb():bool
{
return is_string($this->db) || $this->db instanceof Pdo;
}
// checkDb
// envoie une exception si l'objet n'est pas lié à une base de donnée
// retourne l'objet courant
final public function checkDb():self
{
if(!$this->hasDb())
static::throw('dbPropertyIsInvalid','objectUnusable');
return $this;
}
// db
// retourne l'objet base de données
// retourne une erreur si le retour n'est pas instance de db
final public function db():Pdo
{
$return = $this->db;
if(is_string($return))
$return = Db::instSafe($return);
return static::typecheck($return,Pdo::class,'dbPropertyIsInvalid','objectUnusable');
}
// isLinked
// retourne vrai si l'objet est lié
final public function isLinked():bool
{
return $this->hasDb();
}
// checkLink
// retourne this si l'objet est lié, envoie une exception sinon
final public function checkLink():self
{
if($this->isLinked() === false)
static::throw();
return $this;
}
}
?>