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: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284:
<?php
namespace LeanMapper;
use dibi;
use DibiConnection;
use DibiRow;
use LeanMapper\Exception\InvalidArgumentException;
use LeanMapper\Exception\InvalidStateException;
use LeanMapper\Reflection\AnnotationsParser;
use ReflectionClass;
abstract class Repository
{
protected $defaultEntityNamespace = 'Model\Entity';
protected $connection;
protected $table;
protected $entityClass;
private $docComment;
public function __construct(DibiConnection $connection)
{
$this->connection = $connection;
}
public function persist(Entity $entity)
{
$this->checkEntityType($entity);
if ($entity->isModified()) {
$values = $entity->getModifiedRowData();
if ($entity->isDetached()) {
$values = $this->beforeCreate($values);
$this->connection->insert($this->getTable(), $values)
->execute();
$id = isset($values['id']) ? $values['id'] : $this->connection->getInsertId();
$entity->markAsCreated($id, $this->getTable(), $this->connection);
return $id;
} else {
$values = $this->beforeUpdate($values);
$result = $this->connection->update($this->getTable(), $values)
->where('[id] = %i', $entity->id)
->execute();
$entity->markAsUpdated();
return $result;
}
}
}
public function delete($arg)
{
$id = $arg;
if ($arg instanceof Entity) {
$this->checkEntityType($arg);
if ($arg->isDetached()) {
throw new InvalidStateException('Cannot delete detached entity.');
}
$id = $arg->id;
$arg->detach();
}
$this->connection->delete($this->getTable())
->where('[id] = %i', $id)
->execute();
}
protected function beforeCreate(array $values)
{
return $this->beforePersist($values);
}
protected function beforeUpdate(array $values)
{
return $this->beforePersist($values);
}
protected function beforePersist(array $values)
{
return $values;
}
protected function createEntity(DibiRow $row, $entityClass = null, $table = null)
{
if ($entityClass === null) {
$entityClass = $this->getEntityClass();
}
if ($table === null) {
$table = $this->getTable();
}
$result = Result::getInstance($row, $table, $this->connection);
return new $entityClass($result->getRow($row->id));
}
protected function createEntities(array $rows, $entityClass = null, $table = null)
{
if ($entityClass === null) {
$entityClass = $this->getEntityClass();
}
if ($table === null) {
$table = $this->getTable();
}
$entities = array();
$collection = Result::getInstance($rows, $table, $this->connection);
foreach ($rows as $row) {
$entities[$row->id] = new $entityClass($collection->getRow($row->id));
}
return $this->createCollection($entities);
}
protected function getTable()
{
if ($this->table === null) {
$name = AnnotationsParser::parseSimpleAnnotationValue('table', $this->getDocComment());
if ($name !== null) {
$this->table = $name;
} else {
$matches = array();
if (preg_match('#([a-z0-9]+)repository$#i', get_called_class(), $matches)) {
$this->table = strtolower($matches[1]);
} else {
throw new InvalidStateException('Cannot determine table name.');
}
}
}
return $this->table;
}
protected function getEntityClass()
{
if ($this->entityClass === null) {
$entityClass = AnnotationsParser::parseSimpleAnnotationValue('entity', $this->getDocComment());
if ($entityClass !== null) {
$this->entityClass = $entityClass;
} else {
$matches = array();
if (preg_match('#([a-z0-9]+)repository$#i', get_called_class(), $matches)) {
$this->entityClass = $matches[1];
} else {
throw new InvalidStateException('Cannot determine entity class name.');
}
}
}
return $this->getFullyQualifiedClass($this->entityClass);
}
protected function checkEntityType(Entity $entity)
{
$entityClass = $this->getEntityClass();
if (!($entity instanceof $entityClass)) {
throw new InvalidArgumentException('Repository ' . get_called_class() . ' cannot handle ' . get_class($entity) . ' entity.');
}
}
protected function createCollection(array $entities)
{
return $entities;
}
private function getDocComment()
{
if ($this->docComment === null) {
$reflection = new ReflectionClass(get_called_class());
$this->docComment = $reflection->getDocComment();
}
return $this->docComment;
}
private function getFullyQualifiedClass($entityClass)
{
if (substr($entityClass, 0, 1) === '\\') {
return substr($entityClass, 1);
}
if ($this->defaultEntityNamespace === null) {
return $entityClass;
}
return $this->defaultEntityNamespace . '\\' . $entityClass;
}
}