annotate core/modules/jsonapi/src/JsonApiSpec.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
rev   line source
Chris@5 1 <?php
Chris@5 2
Chris@5 3 namespace Drupal\jsonapi;
Chris@5 4
Chris@5 5 /**
Chris@5 6 * Defines constants used for compliance with the JSON:API specification.
Chris@5 7 *
Chris@5 8 * @internal JSON:API maintains no PHP API since its API is the HTTP API. This
Chris@5 9 * class may change at any time and this will break any dependencies on it.
Chris@5 10 *
Chris@5 11 * @see https://www.drupal.org/project/jsonapi/issues/3032787
Chris@5 12 * @see jsonapi.api.php
Chris@5 13 *
Chris@5 14 * @see http://jsonapi.org/format
Chris@5 15 */
Chris@5 16 class JsonApiSpec {
Chris@5 17
Chris@5 18 /**
Chris@5 19 * The minimum supported specification version.
Chris@5 20 *
Chris@5 21 * @see http://jsonapi.org/format/#document-jsonapi-object
Chris@5 22 */
Chris@5 23 const SUPPORTED_SPECIFICATION_VERSION = '1.0';
Chris@5 24
Chris@5 25 /**
Chris@5 26 * The URI of the supported specification document.
Chris@5 27 */
Chris@5 28 const SUPPORTED_SPECIFICATION_PERMALINK = 'http://jsonapi.org/format/1.0/';
Chris@5 29
Chris@5 30 /**
Chris@5 31 * Member name: globally allowed characters.
Chris@5 32 *
Chris@5 33 * U+0080 and above (non-ASCII Unicode characters) are allowed, but are not
Chris@5 34 * URL-safe. It is RECOMMENDED to not use them.
Chris@5 35 *
Chris@5 36 * A character class, for use in regular expressions.
Chris@5 37 *
Chris@5 38 * @see http://jsonapi.org/format/#document-member-names-allowed-characters
Chris@5 39 * @see http://php.net/manual/en/regexp.reference.character-classes.php
Chris@5 40 */
Chris@5 41 const MEMBER_NAME_GLOBALLY_ALLOWED_CHARACTER_CLASS = '[a-zA-Z0-9\x{80}-\x{10FFFF}]';
Chris@5 42
Chris@5 43 /**
Chris@5 44 * Member name: allowed characters except as the first or last character.
Chris@5 45 *
Chris@5 46 * Space (U+0020) is allowed, but is not URL-safe. It is RECOMMENDED to not
Chris@5 47 * use it.
Chris@5 48 *
Chris@5 49 * A character class, for use in regular expressions.
Chris@5 50 *
Chris@5 51 * @see http://jsonapi.org/format/#document-member-names-allowed-characters
Chris@5 52 * @see http://php.net/manual/en/regexp.reference.character-classes.php
Chris@5 53 */
Chris@5 54 const MEMBER_NAME_INNER_ALLOWED_CHARACTERS = "[a-zA-Z0-9\x{80}-\x{10FFFF}\-_ ]";
Chris@5 55
Chris@5 56 /**
Chris@5 57 * Checks whether the given member name is valid.
Chris@5 58 *
Chris@5 59 * Requirements:
Chris@5 60 * - it MUST contain at least one character.
Chris@5 61 * - it MUST contain only the allowed characters
Chris@5 62 * - it MUST start and end with a "globally allowed character"
Chris@5 63 *
Chris@5 64 * @param string $member_name
Chris@5 65 * A member name to validate.
Chris@5 66 *
Chris@5 67 * @return bool
Chris@5 68 * Whether the given member name is in compliance with the JSON:API
Chris@5 69 * specification.
Chris@5 70 *
Chris@5 71 * @see http://jsonapi.org/format/#document-member-names
Chris@5 72 */
Chris@5 73 public static function isValidMemberName($member_name) {
Chris@5 74 // @todo When D8 requires PHP >=5.6, move to a MEMBER_NAME_REGEXP constant.
Chris@5 75 static $regexp;
Chris@5 76 // @codingStandardsIgnoreStart
Chris@5 77 if (!isset($regexp)) {
Chris@5 78 $regexp = '/^' .
Chris@5 79 // First character must be "globally allowed". Length must be >=1.
Chris@5 80 self::MEMBER_NAME_GLOBALLY_ALLOWED_CHARACTER_CLASS . '{1}' .
Chris@5 81 '(' .
Chris@5 82 // As many non-globally allowed characters as desired.
Chris@5 83 self::MEMBER_NAME_INNER_ALLOWED_CHARACTERS . '*' .
Chris@5 84 // If length > 1, then it must end in a "globally allowed" character.
Chris@5 85 self::MEMBER_NAME_GLOBALLY_ALLOWED_CHARACTER_CLASS . '{1}' .
Chris@5 86 // >1 characters is optional.
Chris@5 87 ')?' .
Chris@5 88 '$/u';
Chris@5 89 }
Chris@5 90 // @codingStandardsIgnoreEnd
Chris@5 91
Chris@5 92 return preg_match($regexp, $member_name) === 1;
Chris@5 93 }
Chris@5 94
Chris@5 95 /**
Chris@5 96 * The reserved (official) query parameters.
Chris@5 97 *
Chris@5 98 * @todo When D8 requires PHP >= 5.6, convert to an array.
Chris@5 99 */
Chris@5 100 const RESERVED_QUERY_PARAMETERS = 'filter|sort|page|fields|include';
Chris@5 101
Chris@5 102 /**
Chris@5 103 * The query parameter for providing a version (revision) value.
Chris@5 104 *
Chris@5 105 * @var string
Chris@5 106 */
Chris@5 107 const VERSION_QUERY_PARAMETER = 'resourceVersion';
Chris@5 108
Chris@5 109 /**
Chris@5 110 * Gets the reserved (official) JSON:API query parameters.
Chris@5 111 *
Chris@5 112 * @return string[]
Chris@5 113 * Gets the query parameters reserved by the specification.
Chris@5 114 */
Chris@5 115 public static function getReservedQueryParameters() {
Chris@5 116 return explode('|', static::RESERVED_QUERY_PARAMETERS);
Chris@5 117 }
Chris@5 118
Chris@5 119 /**
Chris@5 120 * Checks whether the given custom query parameter name is valid.
Chris@5 121 *
Chris@5 122 * A custom query parameter name must be a valid member name, with one
Chris@5 123 * additional requirement: it MUST contain at least one non a-z character.
Chris@5 124 *
Chris@5 125 * Requirements:
Chris@5 126 * - it MUST contain at least one character.
Chris@5 127 * - it MUST contain only the allowed characters
Chris@5 128 * - it MUST start and end with a "globally allowed character"
Chris@5 129 * - it MUST contain at least none a-z (U+0061 to U+007A) character
Chris@5 130 *
Chris@5 131 * It is RECOMMENDED that a hyphen (U+002D), underscore (U+005F) or capital
Chris@5 132 * letter is used (i.e. camelCasing).
Chris@5 133 *
Chris@5 134 * @param string $custom_query_parameter_name
Chris@5 135 * A custom query parameter name to validate.
Chris@5 136 *
Chris@5 137 * @return bool
Chris@5 138 * Whether the given query parameter is in compliane with the JSON:API
Chris@5 139 * specification.
Chris@5 140 *
Chris@5 141 * @see http://jsonapi.org/format/#query-parameters
Chris@5 142 */
Chris@5 143 public static function isValidCustomQueryParameter($custom_query_parameter_name) {
Chris@5 144 return static::isValidMemberName($custom_query_parameter_name) && preg_match('/[^a-z]/u', $custom_query_parameter_name) === 1;
Chris@5 145 }
Chris@5 146
Chris@5 147 }