Skip to content

Commit b56fda8

Browse files
authored
Fixed formatting in README.
1 parent eb6bcbd commit b56fda8

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,110 +18,139 @@ its definition.
1818

1919
Data objects can be simple, holding just a scalar value:
2020

21+
```
2122
$simple_data->value = 42;
2223
$simple_data->value = 'cake';
2324
$simple_data->value = TRUE;
25+
```
2426

2527
or complex, with child properties which are in turn simple:
2628

29+
```
2730
$complex_data->alpha = 42;
2831
$complex_data->beta = 'cake;
2932
$complex_data->gamma = TRUE;
33+
```
3034

3135
or can be complex themselves:
3236

37+
```
3338
$complex_data->child->grandchild->alpha = 42;
39+
```
3440

3541
Both simple or complex data may be multiple-valued:
3642

43+
```
3744
$multiple_simple_data[] = 'adding new value';
3845
$multiple_simple_data[0] = 'changing existing value';
3946
4047
$multiple_complex_data[]->alpha = 'adding new value';
4148
$multiple_complex_data[0]->beta = 'changing existing value';
49+
```
4250

4351
Complex data can be mutable, which means that its child properties change in
4452
response to a controlling property's value:
4553

54+
```
4655
$animal->type = 'mammal';
4756
$animal->gestation_period = '12 months';
4857
$animal->type = 'reptile';
4958
// The gestation_period property is removed; other properties may now have been
5059
added.
60+
```
5161

5262
## Accessing data
5363

5464
Simple data is always accessed with the 'value' property:
5565

66+
```
5667
$simple_data->value = 'cake';
5768
print $simple_data->value;
69+
```
5870

5971
Complex data is accessed with the child property names, which can be chained
6072
until reaching the simple values at the tips of the structure:
6173

74+
```
6275
$complex_data->alpha->value = 'cake';
6376
print $complex_data->alpha->value;
6477
6578
$complex_data->also_complex->value = 'cake';
6679
print $complex_data->also_complex->alpha->value;
80+
```
6781

6882
As a shorthand, simple child values can be set with just the property name:
6983

84+
```
7085
$complex_data->alpha = 'cake';
86+
```
7187

7288
Multiple data can be used as a numeric array:
7389

90+
```
7491
$multiple_simple_data[0] = 'zero';
7592
$multiple_simple_data[1] = 'one';
7693
$multiple_simple_data[] = 'append';
94+
```
7795

7896
Note that the deltas must remain consistent:
7997

98+
```
8099
$multiple_simple_data[0] = 'zero';
81100
$multiple_simple_data[42] = 'delta too high'; // throws Exception
101+
```
82102

83103
Whether multiple data is simple or complex, accessing the array produces a
84104
data object:
85105

106+
```
86107
print $multiple_simple_data[0]->value;
87108
print $multiple_complex_data[0]->alpha->value;
109+
```
88110

89111
### Iterating
90112

91113
All data objects can be iterated over.
92114

93115
Simple data has no effect:
94116

117+
```
95118
foreach ($simple_data as $item) {
96119
// Nothing.
97120
}
121+
```
98122

99123
Complex data iterates over its child properties:
100124

125+
```
101126
foreach ($complex_data as $name => $item) {
102127
print $item->value;
103128
}
129+
```
104130

105131
Multiple data iterates over the delta items:
106132

133+
```
107134
foreach ($multiple_data as $delta => $item) {
108135
print $item->value;
109136
}
137+
```
110138

111139
Because iterating simple items has no effect, iterating data items like this is
112140
safe to do recursively into the data structure.
113141

114142
Alternatively, the items() method on data objects allows iteration that is
115143
agnostic of cardinality:
116144

145+
```
117146
foreach ($simple_data->items() as $item) {
118147
print $item->value; // Same as $simple_data->value
119148
}
120149
121150
foreach ($multiple_simple_data->items() as $item) {
122151
print $item->value;
123152
}
124-
153+
```
125154
This is useful when working with a property that may be either single or
126155
multiple, as it gives all the values in either case. However, it should not be
127156
used recursively, as iterating on the value in the iteration for a simple item
@@ -175,12 +204,15 @@ have entered. See Drupal's Module Builder for an example of doing this.
175204

176205
Data is defined with a fluent interface:
177206

207+
```
178208
$definition = \MutableTypedData\Definition\DataDefinition::create('string')
179209
->setLabel('Label')
180210
->setRequired(TRUE);
211+
```
181212

182213
Complex data is defined by nesting properties:
183214

215+
```
184216
$definition = \MutableTypedData\Definition\DataDefinition::create('complex')
185217
->setLabel('Label')
186218
->setProperties([
@@ -190,12 +222,14 @@ $definition = \MutableTypedData\Definition\DataDefinition::create('complex')
190222
'child_property_beta' => \MutableTypedData\Definition\DataDefinition::create('string')
191223
->setLabel('Beta'),
192224
]);
225+
```
193226

194227
### Options
195228

196229
Options can be defined with an array, or with objects, which allow options to
197230
have descriptions as well as labels:
198231

232+
```
199233
$definition = \MutableTypedData\Definition\DataDefinition::create('string')
200234
->setLabel('Label')
201235
->setOptionsArray([
@@ -211,12 +245,14 @@ $definition = \MutableTypedData\Definition\DataDefinition::create('string')
211245
\MutableTypedData\Definition\OptionDefinition::create('red', 'Magenta', 'A deep red'),
212246
\MutableTypedData\Definition\OptionDefinition::create('grey', 'Grey', 'Not very colourful')
213247
);
248+
```
214249

215250
### Mutable data
216251

217252
Mutable data needs a single property to control the variants, and then a
218253
definition for each variant:
219254

255+
```
220256
$definition = \MutableTypedData\Definition\DataDefinition::create('mutable')
221257
->setLabel('Label')
222258
->setProperties([
@@ -241,6 +277,7 @@ $definition = \MutableTypedData\Definition\DataDefinition::create('mutable')
241277
->setLabel('B2'),
242278
]),
243279
]);
280+
```
244281

245282
The variants automatically define the options for the type property.
246283

0 commit comments

Comments
 (0)