1
1
# Attributes
2
2
3
- ## Generic Attributes
4
-
5
- You can define a generic attribute on your resource using the ` Attribute ` class.
6
- No specific serialization, deserialization, or validation will be performed:
3
+ You can define a attribute field on your resource using the ` Attribute ` class.
7
4
8
5
``` php
9
- use Tobyz\JsonApiServer\Field\Attribute;
6
+ use Tobyz\JsonApiServer\Schema\ Field\Attribute;
10
7
11
8
Attribute::make('title');
12
9
```
13
10
14
- ## Typed Attributes
11
+ ## Attribute Types
15
12
16
- json-api-server includes a selection of typed attribute implementations to match
17
- the data types in the
13
+ Attributes can be configured with a type to automatically perform appropriate
14
+ serialization, deserialization, and validation. json-api-server includes a
15
+ selection of type implementations to match the data types in the
18
16
[ OpenAPI specification] ( https://swagger.io/docs/specification/data-models/data-types/ ) .
19
17
20
18
### Boolean
21
19
22
- The ` Boolean ` attribute serializes values to booleans, and performs validation
23
- to ensure that incoming values are booleans.
20
+ The ` Boolean ` type serializes values to booleans, and performs validation to
21
+ ensure that incoming values are booleans.
24
22
25
23
``` php
26
- use Tobyz\JsonApiServer\Field \Boolean;
24
+ use Tobyz\JsonApiServer\Schema\Type \Boolean;
27
25
28
- Boolean ::make('active');
26
+ Attribute ::make('active')->type(Boolean::make() );
29
27
```
30
28
31
29
### Date and DateTime
32
30
33
- The ` Date ` and ` DateTime ` attributes serialize and deserialize values between
34
- strings using RFC 3339 notation and ` DateTime ` objects, and perform validation
35
- to ensure that incoming values match this format.
31
+ The ` Date ` and ` DateTime ` types serialize and deserialize values between strings
32
+ using RFC 3339 notation and ` DateTime ` objects, and perform validation to ensure
33
+ that incoming values match this format.
36
34
37
35
``` php
38
- use Tobyz\JsonApiServer\Field\Date;
39
- use Tobyz\JsonApiServer\Field\DateTime;
40
-
41
- Date::make('dob');
42
- DateTime::make('publishedAt');
43
- ```
44
-
45
- ### BooleanDateTime
46
-
47
- The ` BooleanDateTime ` attribute behaves like the ` Boolean ` attribute, except
48
- that before setting the value to the model, a ` true ` value is set as the current
49
- date, and a ` false ` value is set as ` null ` . This can be used to represent a
50
- field that is stored internally as a timestamp as a boolean in the API.
36
+ use Tobyz\JsonApiServer\Schema\Type\Date;
37
+ use Tobyz\JsonApiServer\Schema\Type\DateTime;
51
38
52
- ``` php
53
- use Tobyz\JsonApiServer\Field\DateTime;
54
-
55
- BooleanDateTime::make('isDeleted')
56
- ->property('deleted_at')
57
- ->writable();
39
+ Attribute::make('dob')->type(Date::make());
40
+ Attribute::make('publishedAt')->type(DateTime::make());
58
41
```
59
42
60
43
### Number and Integer
61
44
62
- The ` Number ` attribute serializes values to floats, and performs validation to
63
- ensure that incoming values are numeric.
45
+ The ` Number ` type serializes values to floats, and performs validation to ensure
46
+ that incoming values are numeric.
64
47
65
48
``` php
66
- use Tobyz\JsonApiServer\Field \Number;
49
+ use Tobyz\JsonApiServer\Schema\Type \Number;
67
50
68
- Number ::make('weight');
51
+ Attribute ::make('weight')->type(Number::make() );
69
52
```
70
53
71
- The ` Integer ` attribute serializes values to integers, and performs validation
72
- to ensure that incoming values are integers.
54
+ The ` Integer ` type serializes values to integers, and performs validation to
55
+ ensure that incoming values are integers.
73
56
74
57
``` php
75
- use Tobyz\JsonApiServer\Field \Integer;
58
+ use Tobyz\JsonApiServer\Schema\Type \Integer;
76
59
77
- Integer ::make('commentCount');
60
+ Attribute ::make('commentCount')->type(Integer::make() );
78
61
```
79
62
80
63
#### Minimum and Maximum
81
64
82
65
Use the ` minimum ` and ` maximum ` methods to specify the range of possible values.
83
66
84
67
``` php
85
- use Tobyz\JsonApiServer\Field \Number;
68
+ use Tobyz\JsonApiServer\Schema\Type \Number;
86
69
87
- Number::make('number')
88
- ->minimum(1)
89
- ->maximum(20);
70
+ Attribute::make('number')->type(
71
+ Number::make()
72
+ ->minimum(1)
73
+ ->maximum(20),
74
+ );
90
75
```
91
76
92
77
By default, these values are included in the range. To exclude the boundary
93
78
values, you can add a second argument:
94
79
95
80
``` php
96
- use Tobyz\JsonApiServer\Field \Number;
81
+ use Tobyz\JsonApiServer\Schema\Type \Number;
97
82
98
- Number::make('number')
99
- ->minimum(1, exclusive: true)
100
- ->maximum(20, exclusive: true);
83
+ Attribute::make('number')->type(
84
+ Number::make()
85
+ ->minimum(1, exclusive: true)
86
+ ->maximum(20, exclusive: true),
87
+ );
101
88
```
102
89
103
90
#### Multiples
@@ -106,38 +93,40 @@ Use the `multipleOf` method to specify that a number must be the multiple of
106
93
another number:
107
94
108
95
``` php
109
- use Tobyz\JsonApiServer\Field \Integer;
96
+ use Tobyz\JsonApiServer\Schema\Type \Integer;
110
97
111
- Integer ::make('number')->multipleOf(10);
98
+ Attribute ::make('number')->type(Integer::make()-> multipleOf(10) );
112
99
```
113
100
114
101
### String
115
102
116
- The ` Str ` attribute serializes values to strings, and performs validation to
117
- ensure that incoming values are strings.
103
+ The ` Str ` type serializes values to strings, and performs validation to ensure
104
+ that incoming values are strings.
118
105
119
106
``` php
120
- use Tobyz\JsonApiServer\Field \Str;
107
+ use Tobyz\JsonApiServer\Schema\Type \Str;
121
108
122
- Str ::make('name');
109
+ Attribute ::make('name')->type(Str::make() );
123
110
```
124
111
125
112
#### ` minLength ` and ` maxLength `
126
113
127
114
String length can be restricted using the ` minLength ` and ` maxLength ` methods:
128
115
129
116
``` php
130
- Str::make('name')
131
- ->minLength(3)
132
- ->maxLength(20);
117
+ Attribute::make('name')->type(
118
+ Str::make()
119
+ ->minLength(3)
120
+ ->maxLength(20),
121
+ );
133
122
```
134
123
135
124
#### ` enum `
136
125
137
126
You can restrict the string to a set of possible values using the ` enum ` method.
138
127
139
128
``` php
140
- Str ::make('status')->enum(['to do', 'doing', 'done']);
129
+ Attribute ::make('status')->type(Str::make()-> enum(['to do', 'doing', 'done']) );
141
130
```
142
131
143
132
#### ` pattern `
@@ -147,7 +136,7 @@ You can also validate the string against a regular expression using the
147
136
and are case-sensitive.
148
137
149
138
``` php
150
- Str ::make('ssn')->pattern('^\d{3}-\d{2}-\d{4}$');
139
+ Attribute ::make('ssn')->type(Str::make()-> pattern('^\d{3}-\d{2}-\d{4}$') );
151
140
```
152
141
153
142
#### ` format `
@@ -157,10 +146,28 @@ You can mark strings with a
157
146
to serve as a hint in your OpenAPI definition:
158
147
159
148
``` php
160
- Str ::make('email')->format('email');
149
+ Attribute ::make('email')->type(Str::make()-> format('email') );
161
150
```
162
151
163
152
Note that this will not add any additional behaviour (like serialization and
164
153
validation) to the field – you will need to implement this yourself. For the
165
154
` date ` and ` date-time ` formats, you should use the
166
- [ Date and DateTime] ( #date-and-datetime ) attributes instead.
155
+ [ Date and DateTime] ( #date-and-datetime ) types instead.
156
+
157
+ ## Special Attributes
158
+
159
+ ### BooleanDateTime
160
+
161
+ The ` BooleanDateTime ` attribute subclass behaves like a ` Boolean ` -typed
162
+ attribute, except that before setting the value to the model, a ` true ` value is
163
+ set as the current date, and a ` false ` value is set as ` null ` . This can be used
164
+ to represent a field that is stored internally as a timestamp as a boolean in
165
+ the API.
166
+
167
+ ``` php
168
+ use Tobyz\JsonApiServer\Schema\Field\BooleanDateTime;
169
+
170
+ BooleanDateTime::make('isDeleted')
171
+ ->property('deleted_at')
172
+ ->writable();
173
+ ```
0 commit comments