/** * To build a repository, either provide : * * - Mappable object's class name as a string * - Mappable object instance * - Instance of mapper * * @param Mapper|Mappable|string $mapper * @param EntityMap $entityMap (optionnal) * * @throws \InvalidArgumentException */ public function __construct($mapper, EntityMap $entityMap = null) { if ($mapper instanceof Mappable || is_string($mapper)) { $this->mapper = Manager::getMapper($mapper, $entityMap); } else { if ($mapper instanceof Mapper) { $this->mapper = $mapper; } else { new InvalidArgumentException('Repository class constuctor need a valid Mapper or Mappable object.'); } } }
/** * Get the mapper instance for the entity * * @param Mappable $entity * @return \Analogue\ORM\System\Mapper */ protected function getMapper(Mappable $entity) { return Manager::getMapper($entity); }
protected function getEntityKey(Mappable $entity) { $keyName = Manager::getMapper($entity)->getEntityMap()->getKeyName(); return $entity->getEntityAttribute($keyName); }
protected function cachedArray(Mappable $entity) { // Flatten Value Objects as attributes $attributes = $this->flattenEmbeddables($entity->getEntityAttributes()); $cache = []; foreach ($attributes as $key => $value) { if ($value instanceof ProxyInterface) { continue; } if ($value instanceof Mappable) { $class = get_class($value); $mapper = Manager::getMapper($class); $keyName = $mapper->getEntityMap()->getKeyName(); $cache[$key] = new CachedRelationship($class . '.' . $value->{$keyName}, $this->getPivotValues($key, $value)); continue; } if ($value instanceof EntityCollection) { $cache[$key] = []; foreach ($value as $relatedEntity) { $hash = $this->getEntityHash($relatedEntity); $cache[$key][$hash] = new CachedRelationship($hash, $this->getPivotValues($key, $relatedEntity)); } continue; } $cache[$key] = $value; } return $cache; }
/** * Execute a store command on a dirty entity * * @param mixed $entity * @return void */ protected function updateEntityIfDirty($entity) { $mapper = Manager::getMapper($entity); $checker = new StateChecker($entity, $mapper); $dirtyAttributes = $checker->getDirtyAttributes(); if (count($dirtyAttributes) > 0) { $mapper->store($entity); } }
/** * Get the mapper's instance for this entity class * * @return \Analogue\ORM\System\Mapper */ protected function getMapper() { return $this->manager->getMapper($this->class); }