11# Attributes
22
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.
74
85``` php
9- use Tobyz\JsonApiServer\Field\Attribute;
6+ use Tobyz\JsonApiServer\Schema\ Field\Attribute;
107
118Attribute::make('title');
129```
1310
14- ## Typed Attributes
11+ ## Attribute Types
1512
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
1816[ OpenAPI specification] ( https://swagger.io/docs/specification/data-models/data-types/ ) .
1917
2018### Boolean
2119
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.
2422
2523``` php
26- use Tobyz\JsonApiServer\Field \Boolean;
24+ use Tobyz\JsonApiServer\Schema\Type \Boolean;
2725
28- Boolean ::make('active');
26+ Attribute ::make('active')->type(Boolean::make() );
2927```
3028
3129### Date and DateTime
3230
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.
3634
3735``` 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;
5138
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());
5841```
5942
6043### Number and Integer
6144
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.
6447
6548``` php
66- use Tobyz\JsonApiServer\Field \Number;
49+ use Tobyz\JsonApiServer\Schema\Type \Number;
6750
68- Number ::make('weight');
51+ Attribute ::make('weight')->type(Number::make() );
6952```
7053
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.
7356
7457``` php
75- use Tobyz\JsonApiServer\Field \Integer;
58+ use Tobyz\JsonApiServer\Schema\Type \Integer;
7659
77- Integer ::make('commentCount');
60+ Attribute ::make('commentCount')->type(Integer::make() );
7861```
7962
8063#### Minimum and Maximum
8164
8265Use the ` minimum ` and ` maximum ` methods to specify the range of possible values.
8366
8467``` php
85- use Tobyz\JsonApiServer\Field \Number;
68+ use Tobyz\JsonApiServer\Schema\Type \Number;
8669
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+ );
9075```
9176
9277By default, these values are included in the range. To exclude the boundary
9378values, you can add a second argument:
9479
9580``` php
96- use Tobyz\JsonApiServer\Field \Number;
81+ use Tobyz\JsonApiServer\Schema\Type \Number;
9782
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+ );
10188```
10289
10390#### Multiples
@@ -106,38 +93,40 @@ Use the `multipleOf` method to specify that a number must be the multiple of
10693another number:
10794
10895``` php
109- use Tobyz\JsonApiServer\Field \Integer;
96+ use Tobyz\JsonApiServer\Schema\Type \Integer;
11097
111- Integer ::make('number')->multipleOf(10);
98+ Attribute ::make('number')->type(Integer::make()-> multipleOf(10) );
11299```
113100
114101### String
115102
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.
118105
119106``` php
120- use Tobyz\JsonApiServer\Field \Str;
107+ use Tobyz\JsonApiServer\Schema\Type \Str;
121108
122- Str ::make('name');
109+ Attribute ::make('name')->type(Str::make() );
123110```
124111
125112#### ` minLength ` and ` maxLength `
126113
127114String length can be restricted using the ` minLength ` and ` maxLength ` methods:
128115
129116``` 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+ );
133122```
134123
135124#### ` enum `
136125
137126You can restrict the string to a set of possible values using the ` enum ` method.
138127
139128``` php
140- Str ::make('status')->enum(['to do', 'doing', 'done']);
129+ Attribute ::make('status')->type(Str::make()-> enum(['to do', 'doing', 'done']) );
141130```
142131
143132#### ` pattern `
@@ -147,7 +136,7 @@ You can also validate the string against a regular expression using the
147136and are case-sensitive.
148137
149138``` 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}$') );
151140```
152141
153142#### ` format `
@@ -157,10 +146,28 @@ You can mark strings with a
157146to serve as a hint in your OpenAPI definition:
158147
159148``` php
160- Str ::make('email')->format('email');
149+ Attribute ::make('email')->type(Str::make()-> format('email') );
161150```
162151
163152Note that this will not add any additional behaviour (like serialization and
164153validation) to the field – you will need to implement this yourself. For the
165154` 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