/** * Apparat URL constructor * * If the constructor does not throw an exception, the URL is valid and * * 1. either an absolute URL (local or remote) or * 2. a relative URL to a known local repository (respectively the to the context repository if given) * * @param string $url Apparat URL * @param boolean $remote Accept remote URL (less strict date component checking) * @param RepositoryInterface $contextRepository Context repository * @throws ApparatInvalidArgumentException If the URL is absolute but doesn't have the apparat scheme * @throws ApparatInvalidArgumentException If this is a local Apparat URL with an unknown repository */ public function __construct($url, $remote = false, RepositoryInterface $contextRepository = null) { parent::__construct($url, $remote); // If it's an absolute URL if ($this->isAbsolute()) { // If the Apparat URL scheme is invalid if (!array_key_exists($this->urlParts['scheme'], self::$schemes)) { throw new ApparatInvalidArgumentException(sprintf('Invalid absolute apparat URL "%s"', $url), ApparatInvalidArgumentException::INVALID_ABSOLUTE_APPARAT_URL); } return; } // If this URL doesn't have a repository URL and a context repository is given: Inherit its URL if (!strlen($this->getPath()) && $contextRepository instanceof RepositoryInterface) { $this->urlParts['path'] = $contextRepository->getUrl(); } // If the the repository involved is unknown and cannot be auto-connected if (!Kernel::create(Service::class)->isRegistered($this->getPath())) { throw new ApparatInvalidArgumentException(sprintf('Unknown local repository URL "%s"', $this->getPath()), ApparatInvalidArgumentException::UNKNOWN_LOCAL_REPOSITORY_URL); } }
/** * Create and return a new object * * @param RepositoryInterface $repository Repository to create the object in * @param Type $type Object type * @param string $payload Object payload * @param array $propertyData Object property data * @param \DateTimeInterface $creationDate Object creation date * @return ObjectInterface Object */ public function createObject(RepositoryInterface $repository, Type $type, $payload = '', array $propertyData = [], \DateTimeInterface $creationDate = null) { // Set the creation date to now if empty if ($creationDate === null) { $creationDate = new \DateTimeImmutable('now'); } // Construct a creation closure $creationClosure = function (Id $uid) use($repository, $type, $payload, $propertyData, $creationDate) { /** @var Revision $revision */ $revision = Kernel::create(Revision::class, [1, true]); /** @var RepositoryLocator $repositoryLocator */ $repositoryLocator = Kernel::create(RepositoryLocator::class, [$repository]); $repositoryLocator = $repositoryLocator->setId($uid); $repositoryLocator = $repositoryLocator->setRevision($revision); $repositoryLocator = $repositoryLocator->setObjectType($type); $repositoryLocator = $repositoryLocator->setCreationDate($creationDate); return ObjectFactory::createFromParams($repositoryLocator, $payload, $propertyData); }; // Wrap the object creation in an ID allocation transaction return $repository->getAdapterStrategy()->createObjectResource($creationClosure); }
/** * Load an object from this repository * * @param string $locator Object locator * @param int $visibility Object visibility * @return ApparatObjectInterface Object */ public function loadObject($locator, $visibility = SelectorInterface::ALL) { /** @var LocatorInterface $objectLocator */ $objectLocator = Kernel::create(RepositoryLocator::class, [$this->repository, $locator]); return ApparatObjectFactory::create($this->repository->loadObject($objectLocator, $visibility)); }