Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1426213
ArrayDef.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
7 KB
Referenced Files
None
Subscribers
None
ArrayDef.php
View Options
<?php
namespace
MediaWiki\ParamValidator\TypeDef
;
use
InvalidArgumentException
;
use
JsonSchema\Constraints\Constraint
;
use
JsonSchema\Validator
;
use
LogicException
;
use
Wikimedia\Message\DataMessageValue
;
use
Wikimedia\ParamValidator\TypeDef
;
/**
* Type definition for array structures, typically
* used for validating JSON request bodies.
*
* Failure codes:
* - 'notarray': The value is not an array.
*
* @since 1.42
*/
class
ArrayDef
extends
TypeDef
{
/**
* (object) Schema settings.
*
*/
public
const
PARAM_SCHEMA
=
'param-schema'
;
public
function
supportsArrays
()
{
return
true
;
}
public
function
validate
(
$name
,
$value
,
array
$settings
,
array
$options
)
{
if
(
!
is_array
(
$value
)
)
{
// Message used: paramvalidator-notarray
$this
->
failure
(
'notarray'
,
$name
,
$value
,
$settings
,
$options
);
}
if
(
isset
(
$settings
[
self
::
PARAM_SCHEMA
]
)
)
{
$schema
=
$settings
[
self
::
PARAM_SCHEMA
];
if
(
!
isset
(
$schema
[
'type'
]
)
)
{
throw
new
InvalidArgumentException
(
"Schema type not set "
);
}
$types
=
(
array
)
$schema
[
'type'
];
foreach
(
$types
as
$type
)
{
// @todo: start using JsonSchemaTrait::normalizeJsonSchema
// so we can also support the "list" and "map" types
if
(
(
$type
!==
'object'
)
&&
(
$type
!==
'array'
)
)
{
throw
new
LogicException
(
'Invalid data type'
);
}
}
$validator
=
new
Validator
();
$validator
->
validate
(
$value
,
$schema
,
Constraint
::
CHECK_MODE_TYPE_CAST
|
Constraint
::
CHECK_MODE_APPLY_DEFAULTS
);
if
(
!
$validator
->
isValid
()
)
{
$errorCode
=
'schema-validation-failed'
;
foreach
(
$validator
->
getErrors
()
as
$error
)
{
$message
=
DataMessageValue
::
new
(
"paramvalidator-$errorCode"
,
[
$error
[
'message'
]
],
$errorCode
,
[
'schema-validation-error'
=>
$error
]
);
$this
->
failure
(
$message
,
$name
,
$value
,
$settings
,
$options
);
}
}
}
return
$value
;
}
public
function
stringifyValue
(
$name
,
$value
,
array
$settings
,
array
$options
)
{
if
(
!
is_array
(
$value
)
)
{
return
parent
::
stringifyValue
(
$name
,
$value
,
$settings
,
$options
);
}
return
json_encode
(
$value
);
}
/**
* Returns a JSON Schema of type array, with the input schema for each array item.
*
* If $itemSchema is a string, it must be a valid JSON type, and all list entries will be
* validated to be of that type.
*
* If $itemSchema is an array, it must represent a valid schema, and all list entries will
* be validated to be of that schema. Nested lists are supported, as are lists of maps and
* more complicated schemas.
*
* Examples:
* A list of integers, like [ 1, 2, 3 ]
* ArrayDef::makeListSchema( "integer" )
* A list of strings, where each value must be either "a" or "b", like [ "a", "a", "b", "b" ]
* * ArrayDef::makeListSchema( [ 'enum' => [ 'a', 'b' ] ] )
* A list of lists of strings, like [ [ "foo", 'bar" ], [ "baz", "qux" ] ]
* ArrayDef::makeListSchema( ArrayDef::makeListSchema( "string" ) )
*
* @since 1.43
*
* @param array|string $itemSchema
*
* @return array
*/
public
static
function
makeListSchema
(
$itemSchema
):
array
{
return
[
'type'
=>
'array'
,
'items'
=>
static
::
normalizeSchema
(
$itemSchema
)
];
}
/**
* Returns a JSON Schema of type object, with the input schema for each array item.
*
* If $entrySchema is a string, it must be a valid JSON type, and all map entries will be
* validated to be of that type.
*
* If $entrySchema is an array, it must represent a valid schema, and all map entries will
* be validated to be of that schema. Nested maps are supported, as are maps of lists and
* more complicated schemas
*
* Examples:
* A map of integers, like [ 'key1' => 1, 'key2' => 2, 'key3' => 3 ]
* ArrayDef::makeMapSchema( "integer" )
* A map of where each value must be 0 or 1, like [ 'key1' => 1, 'key2' => 1, 'key3' => 0 ]
* ArrayDef::makeMapSchema( [ 'enum' => [ 0, 1 ] ] )
* A map of maps, like [ 'k1' => [ 'k2' => 'a' ], 'k3' => [ 'k4' => 'b', 'k5' => 'c' ] ]
* ArrayDef::makeMapSchema( ArrayDef::makeMapSchema( "string" ) )
*
* @since 1.43
*
* @param array|string $entrySchema
*
* @return array
*/
public
static
function
makeMapSchema
(
$entrySchema
):
array
{
return
[
'type'
=>
'object'
,
'additionalProperties'
=>
static
::
normalizeSchema
(
$entrySchema
)
];
}
/**
* Returns a JSON Schema of type object, with properties defined by the function params.
*
* Any input schemas must either be a string corresponding to valid JSON types, or valid
* schemas. Nested schemas are supported.
*
* Examples:
* An object with required parameters "a" and "b", where "a" must be an integer and "b" can
* have one of the values "x", "y", or "z", no optional parameters, and additional parameters
* are disallowed:
* ArrayDef::makeObjectSchema( [ 'a' => 'integer', 'b' => [ 'enum' => [ 'x', 'y', 'z' ] ] ] )
* The same object, but parameter "b" is optional:
* ArrayDef::makeObjectSchema( [ 'a' => 'integer' ], [ 'b' => [ 'enum' => [ 'x', 'y', 'z' ] ] ] )
* An object with no required properties, an optional property "a" of type string, with
* arbitrary additional properties allowed (effectively, an arbitrary object, but if "a"
* is present, it must be a string):
* ArrayDef::makeObjectSchema( [ ], [ 'a' => 'string' ], true )
* @since 1.43
*
* @param array $required properties that are required to be present, as name/schema pairs
* @param array $optional properties that may or may not be present, as name/schema pairs
* @param array|bool|string $additional schema additional properties must match, or false
* to disallow additional properties, or true to allow arbitrary additional properties
*
* @return array
*/
public
static
function
makeObjectSchema
(
array
$required
=
[],
array
$optional
=
[],
$additional
=
false
):
array
{
$schema
=
[
'type'
=>
'object'
];
if
(
$required
)
{
foreach
(
$required
as
$propertyName
=>
$propertySchema
)
{
$schema
[
'required'
][]
=
$propertyName
;
$schema
[
'properties'
][
$propertyName
]
=
static
::
normalizeSchema
(
$propertySchema
);
}
}
if
(
$optional
)
{
foreach
(
$optional
as
$propertyName
=>
$propertySchema
)
{
if
(
isset
(
$schema
[
'properties'
][
$propertyName
]
)
)
{
throw
new
InvalidArgumentException
(
"Property {$propertyName} defined as both required and optional"
);
}
$schema
[
'properties'
][
$propertyName
]
=
static
::
normalizeSchema
(
$propertySchema
);
}
}
// The easiest way to allow all extra properties is to not specify additionalProperties
if
(
$additional
!==
true
)
{
// A value of false disallows additional properties
if
(
$additional
===
false
)
{
$schema
[
'additionalProperties'
]
=
false
;
}
else
{
$schema
[
'additionalProperties'
]
=
static
::
normalizeSchema
(
$additional
);
}
}
return
$schema
;
}
/**
* Returns a representation of the input schema
*
* If $schema is a string, it must be a valid JSON type.
* If $schema is an array, it must represent a valid schema.
*
* @param array|string $schema
*
* @return array
*/
private
static
function
normalizeSchema
(
$schema
):
array
{
if
(
is_array
(
$schema
)
)
{
return
$schema
;
}
else
{
return
[
'type'
=>
$schema
];
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 12:49 (1 d, 3 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
4e/ec/c247e004097a9393f582f34d2424
Default Alt Text
ArrayDef.php (7 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment