-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPicoDatabaseEntity.php
More file actions
93 lines (84 loc) · 2.4 KB
/
PicoDatabaseEntity.php
File metadata and controls
93 lines (84 loc) · 2.4 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
<?php
namespace MagicObject\Database;
use MagicObject\MagicObject;
/**
* Class PicoDatabaseEntity
*
* Represents a database entity that manages multiple database connections.
*
* @author Kamshory
* @package MagicObject\Database
* @link https://github.com/Planetbiru/MagicObject
*/
class PicoDatabaseEntity
{
/**
* An associative array of databases indexed by entity class name.
*
* @var PicoDatabase[]
*/
private $databases = array();
/**
* Default database connection
*
* @var PicoDatabase
*/
private $defaultDatabase;
/**
* Adds an entity to the database.
*
* @param MagicObject $entity The entity to add.
* @param PicoDatabase|null $database The database to associate with the entity. If null,
* the current database of the entity will be used.
* @return self Returns the current instance for method chaining.
*/
public function add($entity, $database = null)
{
if ($database === null) {
$database2 = $entity->currentDatabase();
if ($database2 !== null && $database2->isConnected()) {
$database = $database2;
}
}
if ($database !== null) {
$className = get_class($entity);
$this->databases[$className] = $database;
}
return $this;
}
/**
* Gets the database associated with an entity.
*
* @param MagicObject $entity The entity whose database is to be retrieved.
* @return PicoDatabase|null Returns the associated database or null if not found.
*/
public function getDatabase($entity)
{
$className = get_class($entity);
if (isset($this->databases[$className])) {
return $this->databases[$className];
}
return $this->defaultDatabase;
}
/**
* Get default database connection
*
* @return PicoDatabase Default database connection
*/
public function getDefaultDatabase()
{
return $this->defaultDatabase;
}
/**
* Set default database connection
*
* @param PicoDatabase $defaultDatabase Default database connection
*
* @return self Returns the current instance for method chaining.
*/
public function setDefaultDatabase($defaultDatabase)
{
$this->defaultDatabase = $defaultDatabase;
return $this;
}
}