forked from cucumber/common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php.php.erb
88 lines (78 loc) · 3.29 KB
/
php.php.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
/**
* This code was auto-generated by {this script}[https://github.com/cucumber/common/blob/main/messages/jsonschema/scripts/codegen.rb]
*/
namespace Cucumber\Messages;
use JsonSerializable;
use Cucumber\Messages\DecodingException\SchemaViolationException;
<%- @schemas.sort.each do |key, schema| -%>
// CLASS_START <%= class_name(key) %>.php
/**
* Represents the <%= class_name(key) %> message in Cucumber's message protocol
* @see https://github.com/cucumber/common/tree/main/messages#readme
*
<%=- format_description(schema['description'], indent_string: '') -%> */
final class <%= class_name(key) %> implements JsonSerializable
{
use JsonEncodingTrait;
public function __construct(<%- schema['properties'].each do |property_name, property| -%>
<%- property_type = type_for(key, property_name, property) -%>
<%- if property['description'] or property_type == 'array' -%>
/**
<%- if property['description'] -%>
<%= format_description(property['description']) %>
<%- end -%>
<%- if property_type == 'array' -%>
* @param <%= is_nullable(property_name, schema) ? '?' : '' %>list<<%= array_contents_type(key, property_name, property) %>> $<%= property_name %>
<%- end -%>
*/
<%- end -%>
public readonly <%= is_nullable(property_name, schema) ? '?' : '' %><%= property_type %> $<%= property_name %> = <%= default_value(class_name(key), property_name, property, schema) -%>,
<%- end -%>
) {
}
/**
* @throws SchemaViolationException
*
* @internal
*/
public static function fromArray(array $arr): self
{
<%- schema['properties'].each do |property_name, property| -%>
self::ensure<%= capitalize(property_name)%>($arr);
<%- end -%>
return new self(
<%- schema['properties'].each do |property_name, property| -%>
<%= constructor_for(key, property, property_name, schema, 'arr') %>,
<%- end -%>
);
}
<%- schema['properties'].each do |property_name, property| -%>
/**
<%- if is_scalar(property) -%>
* @psalm-assert array{<%= property_name %><%- if (is_nullable(property_name, schema)) -%>?<%- end -%>: string|int|bool} $arr
<%- else -%>
* @psalm-assert array{<%= property_name %><%- if (is_nullable(property_name, schema)) -%>?<%- end -%>: array} $arr
<%- end -%>
*/
private static function ensure<%= capitalize(property_name)%>(array $arr): void
{
<%- if (!is_nullable(property_name, schema)) -%>
if (!array_key_exists('<%= property_name %>', $arr)) {
throw new SchemaViolationException('Property \'<%= property_name %>\' is required but was not found');
}
<%- end -%>
<%- if(is_scalar(property)) -%>
if (array_key_exists('<%= property_name %>', $arr) && is_array($arr['<%= property_name %>'])) {
throw new SchemaViolationException('Property \'<%= property_name %>\' was array');
}
<%- else -%>
if (array_key_exists('<%= property_name %>', $arr) && !is_array($arr['<%= property_name %>'])) {
throw new SchemaViolationException('Property \'<%= property_name %>\' was not array');
}
<%- end -%>
}
<%- end -%>
}
<% end %>