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