forked from vlucas/Spot
/
Manager.php
executable file
·159 lines (142 loc) · 4.94 KB
/
Manager.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* Entity Manager for storing information about entities
*
* @package Spot\Entity
* @author Brandon Lamb <brandon@brandonlamb.com>
*/
namespace Spot\Entity;
use Spot\Di\DiInterface,
Spot\Di\InjectableTrait;
class Manager
{
use InjectableTrait;
/**
* @var array
*/
protected $primaryKeyColumns = [];
/**
* Constructor
* @param \Spot\Di\DiInterface $di
*/
public function __construct(DiInterface $di)
{
$this->setDi($di);
}
/**
* Get name of table for given entity class
* @param string $entityName Name of the entity class
* @return string
*/
public function getTable($entityName)
{
$entityName instanceof ResultsetInterface && $entityName = $entityName->getEntityName();
return $entityName::getMetaData()->getTable();
}
/**
* Get name of sequence for given entity class
* @param string $entityName Name of the entity class
* @return string
*/
public function getSequence($entityName)
{
$entityName instanceof ResultsetInterface && $entityName = $entityName->getEntityName();
return $entityName::getMetaData()->getSequence();
}
/**
* Get value of primary key for given row result
*
* @param string $entityName Name of the entity class
* @return string
*/
public function getPrimaryKeys($entityName)
{
#$entityName instanceof EntityInterface && $entityName = $entityName->toString();
$entityName instanceof ResultsetInterface && $entityName = $entityName->getEntityName();
// Store primary key(s)
if (!isset($this->primaryKeyColumns[$entityName])) {
$this->primaryKeyColumns[$entityName] = [];
foreach ($entityName::getMetaData()->getColumns() as $column) {
$column->isPrimary() && $this->primaryKeyColumns[$entityName][] = $column->getName();
}
}
return $this->primaryKeyColumns[$entityName];
}
/**
* Get value of primary key for given entity
* @param \Spot\Entity\EntityInterface $entity Instance of an entity to find the primary key of
* @return mixed
*/
public function getPrimaryKeyValues(EntityInterface $entity)
{
$values = [];
foreach ($this->getPrimaryKeys($entity->toString()) as $pk) {
$values[$pk] = $entity->get($pk);
}
return $values;
}
/**
* Get formatted columns with all neccesary array keys and values.
* Merges defaults with defined field values to ensure all options exist for each field.
*
* @param string $entityName Name of the entity class
* @param string $column Name of the field to return attributes for
* @return array Defined columns plus all defaults for full array of all possible options
*/
public function getColumns($entityName, $column = null)
{
$entityName instanceof ResultsetInterface && $entityName = $entityName->getEntityName();
$metaData = $entityName::getMetaData();
return null === $column ? $metaData->getColumns() : $metaData->getColumn($column);
}
/**
* Get column default values as defined in class column definitons
*
* @param string $entityName Name of the entity class
* @return array Array of field key => value pairs
*/
public function getDefaultColumnValues($entityName)
{
$entityName instanceof ResultsetInterface && $entityName = $entityName->getEntityName();
return $entityName::getMetaData()->getDefault();
}
/**
* Check if column exists in defined columns
*
* @param string $entityName Name of the entity class
* @param string $column Column name to check for existence
* @return bool
*/
public function hasColumn($entityName, $column)
{
$entityName instanceof ResultsetInterface && $entityName = $entityName->getEntityName();
return false !== $entityName::getMetaData()->getColumn($column);
}
/**
* Return column type for given entity's column
*
* @param string $entityName Name of the entity class
* @param string $column Column name
* @return int Column type string or boolean false
* @throws \InvalidArgumentException
*/
public function getColumnType($entityName, $column)
{
$entityName instanceof ResultsetInterface && $entityName = $entityName->getEntityName();
if ($column = $entityName::getMetaData()->getColumn($column)) {
return $column->getType();
}
throw new \InvalidArgumentException("'$column' is not a properly configured column");
}
/**
* Get defined relations
*
* @param string $entityName Name of the entity class
* @return array
*/
public function getRelations($entityName)
{
$entityName instanceof ResultsetInterface && $entityName = $entityName->getEntityName();
return $entityName::getMetaData()->getRelations();
}
}