annotate core/modules/jsonapi/src/Revisions/VersionByRel.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@18 1 <?php
Chris@18 2
Chris@18 3 namespace Drupal\jsonapi\Revisions;
Chris@18 4
Chris@18 5 use Drupal\Core\Entity\EntityInterface;
Chris@18 6 use Drupal\Core\Entity\RevisionableInterface;
Chris@18 7
Chris@18 8 /**
Chris@18 9 * Revision ID implementation for the default or latest revisions.
Chris@18 10 *
Chris@18 11 * @internal JSON:API maintains no PHP API since its API is the HTTP API. This
Chris@18 12 * class may change at any time and this will break any dependencies on it.
Chris@18 13 *
Chris@18 14 * @see https://www.drupal.org/project/jsonapi/issues/3032787
Chris@18 15 * @see jsonapi.api.php
Chris@18 16 */
Chris@18 17 class VersionByRel extends NegotiatorBase {
Chris@18 18
Chris@18 19 /**
Chris@18 20 * Version argument which loads the revision known to be the "working copy".
Chris@18 21 *
Chris@18 22 * In Drupal terms, a "working copy" is the latest revision. It may or may not
Chris@18 23 * be a "default" revision. This revision is the working copy because it is
Chris@18 24 * the revision to which new work will be applied. In other words, it denotes
Chris@18 25 * the most recent revision which might be considered a work-in-progress.
Chris@18 26 *
Chris@18 27 * @var string
Chris@18 28 */
Chris@18 29 const WORKING_COPY = 'working-copy';
Chris@18 30
Chris@18 31 /**
Chris@18 32 * Version argument which loads the revision known to be the "latest version".
Chris@18 33 *
Chris@18 34 * In Drupal terms, the "latest version" is the latest "default" revision. It
Chris@18 35 * may or may not have later revisions after it, as long as none of them are
Chris@18 36 * "default" revisions. This revision is the latest version because it is the
Chris@18 37 * last revision where work was considered finished. Typically, this means
Chris@18 38 * that it is the most recent "published" revision.
Chris@18 39 *
Chris@18 40 * @var string
Chris@18 41 */
Chris@18 42 const LATEST_VERSION = 'latest-version';
Chris@18 43
Chris@18 44 /**
Chris@18 45 * {@inheritdoc}
Chris@18 46 */
Chris@18 47 protected function getRevisionId(EntityInterface $entity, $version_argument) {
Chris@18 48 assert($entity instanceof RevisionableInterface);
Chris@18 49 switch ($version_argument) {
Chris@18 50 case static::WORKING_COPY:
Chris@18 51 /* @var \Drupal\Core\Entity\RevisionableStorageInterface $entity_storage */
Chris@18 52 $entity_storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
Chris@18 53 return static::ensureVersionExists($entity_storage->getLatestRevisionId($entity->id()));
Chris@18 54
Chris@18 55 case static::LATEST_VERSION:
Chris@18 56 // The already loaded revision will be the latest version by default.
Chris@18 57 // @see \Drupal\Core\Entity\Sql\SqlContentEntityStorage::buildQuery().
Chris@18 58 return $entity->getLoadedRevisionId();
Chris@18 59
Chris@18 60 default:
Chris@18 61 $message = sprintf('The version specifier must be either `%s` or `%s`, `%s` given.', static::LATEST_VERSION, static::WORKING_COPY, $version_argument);
Chris@18 62 throw new InvalidVersionIdentifierException($message);
Chris@18 63 }
Chris@18 64 }
Chris@18 65
Chris@18 66 }