/** * @param array|\Traversable|string $rm * @param TypeMapperRegistry $typeMapperRegistry * @param array $choices * @param null|string $class * @param QueryBuilder|null $queryBuilder * @param null $labelPath * @param array $preferredChoices * @param null $groupPath * @param null $valuePath * @param PropertyAccessorInterface $propertyAccessor */ public function __construct($rm, $choices, $class, $queryBuilder, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null) { $this->rm = $rm; $this->class = $class; if ($queryBuilder === null) { $this->queryBuilder = $this->rm->getRepository($class)->getQueryBuilder(); } else { if ($queryBuilder !== false) { $this->queryBuilder = $queryBuilder; } } parent::__construct($choices, $labelPath, $preferredChoices, $groupPath, $valuePath, $propertyAccessor); }
/** * Use mapping options to find a resource. * * @param $class * @param Request $request * @param $options * * @return bool */ protected function findOneBy($class, Request $request, $options) { if (!$options['mapping']) { $keys = $request->attributes->keys(); $options['mapping'] = $keys ? array_combine($keys, $keys) : array(); } foreach ($options['exclude'] as $exclude) { unset($options['mapping'][$exclude]); } if (!$options['mapping']) { return false; } $criteria = array(); foreach ($options['mapping'] as $attribute => $field) { $criteria[$attribute] = '"' . $request->attributes->get($field) . '"'; } if ($options['strip_null']) { $criteria = array_filter($criteria, function ($value) { return !is_null($value); }); } if (!$criteria) { return false; } if (isset($options['repository_method'])) { $method = $options['repository_method']; } else { $method = 'findOneBy'; } return $this->rm->getRepository($class)->{$method}($criteria); }
/** * @return Query */ public function getQuery() { if (!$this->rm) { throw new \InvalidArgumentException('You must provide a Manager to execute query.'); } return $this->rm->createQuery($this->getSparqlQuery())->setType($this->type)->setOffset($this->offset)->setMaxResults($this->maxResults)->setOrderBy($this->getOrderBy()); }
/** * Construct an ORM Query Builder Loader. * * @param QueryBuilder|\Closure $queryBuilder * @param Manager $manager * @param string $class * * @throws UnexpectedTypeException */ public function __construct($queryBuilder, $manager = null, $class = null) { // If a query builder was passed, it must be a closure or QueryBuilder // instance if (!($queryBuilder instanceof QueryBuilder || $queryBuilder instanceof \Closure)) { throw new UnexpectedTypeException($queryBuilder, 'Conjecto\\Nemrod\\QueryBuilder or \\Closure'); } if ($queryBuilder instanceof \Closure) { if (!$manager instanceof Manager) { throw new UnexpectedTypeException($manager, 'Conjecto\\Nemrod\\Manager'); } $queryBuilder = $queryBuilder($manager->getRepository($class)); if (!$queryBuilder instanceof QueryBuilder) { throw new UnexpectedTypeException($queryBuilder, 'Conjecto\\Nemrod\\QueryBuilder'); } } $this->queryBuilder = $queryBuilder; $this->class = $class; }
/** * @param $query * @param $variable * @param $uri */ private function bindVariableAsUri($query, $variable, $criteria) { if (isset($criteria['uri'])) { if (is_string($criteria['uri'])) { $criteria['uri'] = array($criteria['uri']); } foreach ($criteria['uri'] as $uri) { $query->andWhere("BIND (<" . $this->_rm->getNamespaceRegistry()->expand($uri) . "> AS {$variable})."); } } }
/** * @param $property * @param $value */ private function snapshot() { if (!$this->isReady) { return; } if (!empty($this->_rm) && !$this->isDirty) { $this->_rm->getUnitOfWork()->snapshot($this); $this->isDirty = true; $this->_rm->getUnitOfWork()->setDirty($this); } }
/** * Execute an update query. * * @return Graph|Result */ public function update($hydratation = null, $options = array()) { if ($this->state === self::STATE_DIRTY) { $this->completeSparqlQuery = $this->getCompleteSparqlQuery(); $this->state = self::STATE_CLEAN; } $this->result = $this->rm->getClient()->update($this->completeSparqlQuery); $this->result = $this->resultToGraph($this->result); if (($hydrator = $this->newHydrator($hydratation)) !== null) { $this->result = $hydrator->hydrateResources($options); } return $this->result; }
/** * Queries for all resources pointing to the current one, and declares resources. * * @param $resource */ private function removeUplinks(BaseResource $resource) { /** @var Graph $result */ $result = $this->_rm->createQueryBuilder()->construct('?s a ?t; ?p <' . $resource->getUri() . '>')->where('?s a ?t; ?p <' . $resource->getUri() . '>')->getQuery()->execute(); $resources = $result->resources(); /** @var \Conjecto\Nemrod\Resource $re */ foreach ($resources as $re) { $this->registerResource($re); foreach ($result->properties($re->getUri()) as $prop) { if ($prop !== 'rdf:type') { $re->delete($prop); } } } }
protected function getSize($key) { $qb = $this->resourceManager->getRepository($key)->getQueryBuilder(); $qb->reset()->select('(COUNT(DISTINCT ?instance) AS ?count)')->where('?instance a ' . $key); return $qb->getQuery()->execute(); }
/** * Search all documents containing the updated resource * @param $uri * @param $resourceType * @param $propertiesUpdated * @param Manager $rm * @return array */ protected function search($uri, $resourceType, $propertiesUpdated, Manager $rm) { $qb = $rm->getQueryBuilder(); $typesToReIndex = $this->getAllResourceTypesIndexingThisResourceType($resourceType, $propertiesUpdated); $qbByIndex = $this->getQueryBuilderByIndex($typesToReIndex, $resourceType, $propertiesUpdated, $qb, $uri); return $qbByIndex; }