-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection.php
More file actions
117 lines (101 loc) · 2.84 KB
/
Collection.php
File metadata and controls
117 lines (101 loc) · 2.84 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* AttwFramework
*
* @author Gabriel Jacinto <gamjj74@hotmail.com>
* @license MIT License
* @link http://attwframework.github.io
*/
namespace Attw\Db;
use Attw\Db\Connection\ConnectorInterface;
use \RuntimeException;
use \ArrayObject;
/**
* A collection with database connections to the application
*
* @example
* To add a connection:
* Attw\Db\Collection::getInstance()->add('Conn1', new PDOConnector($connector_configs));
*/
class Collection
{
/**
* Instance for singleton
*
* @var \Attw\Db\Collection
*/
private static $instance;
private $count;
/**
* Collection with user database connections
*
* @var \ArrayObject
*/
private $connections;
private function __construct()
{
if (!($this->connections instanceof ArrayObject)) {
$this->connections = new ArrayObject();
}
}
private function __clone() {}
public static function getInstance()
{
if (!(self::$instance instanceof Collection)) {
self::$instance = new Collection();
}
return self::$instance;
}
/**
* Attach a connector to colletion
*
* @param string $key identification to connection
* @param \Attw\Db\Connection\ConnectorInterface $connector
* @throws \RuntimeException case param $key already exists
*/
public function add($key, ConnectorInterface $connector)
{
$this->connections->offsetSet($key, $connector);
}
/**
* Remove a connection from colletion
*
* @param string $key identification to connection
* @throws \RuntimeException case param $key do not exists
*/
public function remove($key)
{
$this->throwAnExceptionIfConnectionDoesNotExists($key);
$this->connections->offsetUnset($key);
}
/**
* Get a connection from collection by identification key
*
* @param string $key identification to connection
* @return \Attw\Db\Connection\ConnectorInterface
* @throws \RuntimeException case param $key do not exists
*/
public function get($key)
{
if (!$this->connections->offsetExists($key)) {
throw new RuntimeException('This connection doesn\'t exists: ' . print_r($key, true));
}
return $this->connections->offsetGet($key);
}
/**
* Verify a identification key exists in the collection
*
* @param string $key identification to connection
* @return boolean 'true' case exists and 'false' case does not
*/
public function exists($key)
{
return $this->connections->offsetExists($key);
}
private function throwAnExceptionIfConnectionDoesNotExists($key)
{
if (!$this->exists($key)) {
throw new RuntimeException('This connection doesn\'t exists: ' . print_r($key, true));
}
}
}