Skip to content

Commit 502f710

Browse files
committed
wip
1 parent 2571b16 commit 502f710

File tree

6 files changed

+345
-25
lines changed

6 files changed

+345
-25
lines changed

docs/constraint.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,31 @@ $someConstraint->removeProperties();
9595

9696
## Options
9797

98-
WIP
98+
Some constraints can be configured via options.
99+
The example uses strings, but all types (arrays, integers etc.) are supported.
100+
101+
```php
102+
// add a single option
103+
$someConstraint->addOption('name', 'value');
104+
105+
// add multiple options
106+
$someConstraint->addOptions([
107+
'other.name' => 'other value',
108+
'some.name' => 'some value'
109+
]);
110+
111+
// check if constraint has option
112+
$someConstraint->hasOption('name');
113+
114+
// get specific option from a constraint
115+
$someConstraint->getOption('name');
116+
117+
// get all options from a constraint
118+
$someConstraint->getOptions();
119+
120+
// remove a specific option from a constraint:
121+
$someConstraint->removeOption('name');
122+
123+
// remove all options from a constraint:
124+
$someConstraint->removeOptions();
125+
```

docs/type.index.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,31 @@ $someIndex->removeProperties();
9696

9797
## Options
9898

99-
WIP
99+
Some indexes can be configured via options.
100+
The example uses strings, but all types (arrays, integers etc.) are supported.
101+
102+
```php
103+
// add a single option
104+
$someIndex->addOption('name', 'value');
105+
106+
// add multiple options
107+
$someIndex->addOptions([
108+
'other.name' => 'other value',
109+
'some.name' => 'some value'
110+
]);
111+
112+
// check if index has option
113+
$someIndex->hasOption('name');
114+
115+
// get specific option from an index
116+
$someIndex->getOption('name');
117+
118+
// get all options from an index
119+
$someIndex->getOptions();
120+
121+
// remove a specific option from an index:
122+
$someIndex->removeOption('name');
123+
124+
// remove all options from an index:
125+
$someIndex->removeOptions();
126+
```

src/Helper/ToStringHelper.php

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ function ($match) {
8484
return $escapedString;
8585
}
8686

87+
/**
88+
* @param array<mixed, mixed> $array
89+
*
90+
* @see https://stackoverflow.com/a/173479
91+
*/
92+
public static function isArrayAssociate(array $array): bool
93+
{
94+
if ([] === $array) {
95+
return false;
96+
}
97+
98+
return array_keys($array) !== range(0, count($array) - 1);
99+
}
100+
87101
public static function valueToString(mixed $value): string
88102
{
89103
if (is_string($value)) {
@@ -99,11 +113,30 @@ public static function valueToString(mixed $value): string
99113
return 'null';
100114
}
101115
if (is_array($value)) {
102-
asort($value);
103116
$parts = [];
104-
foreach ($value as $part) {
105-
$parts[] = self::valueToString($part);
117+
if (self::isArrayAssociate($value)) {
118+
asort($value);
119+
foreach ($value as $name => $part) {
120+
if (self::mustNameBeEscaped($name)) {
121+
$parts[] = sprintf(
122+
"`%s`: %s",
123+
self::escapeString($name),
124+
self::valueToString($part)
125+
);
126+
} else {
127+
$parts[] = sprintf(
128+
"%s: %s",
129+
$name,
130+
self::valueToString($part)
131+
);
132+
}
133+
}
134+
} else {
135+
foreach ($value as $part) {
136+
$parts[] = self::valueToString($part);
137+
}
106138
}
139+
107140
$parts = implode(', ', $parts);
108141

109142
return sprintf("[%s]", $parts);
@@ -120,7 +153,7 @@ public static function valueToString(mixed $value): string
120153
/**
121154
* @param array<string, mixed> $properties
122155
*/
123-
public static function propertyArrayToString(array $properties, bool $escapeAllNames = false): string
156+
public static function propertiesToString(array $properties, bool $escapeAllNames = false): string
124157
{
125158
ksort($properties);
126159
$parts = [];
@@ -177,7 +210,7 @@ public static function nodeToString(NodeInterface $node, bool $identifying = fal
177210
if ($identifying) {
178211
$properties = $node->getIdentifiers();
179212
}
180-
$propertyString = self::propertyArrayToString($properties);
213+
$propertyString = self::propertiesToString($properties);
181214
if (strlen($propertyString) > 0) {
182215
$parts[] = sprintf("{%s}", $propertyString);
183216
}
@@ -207,7 +240,7 @@ public static function relationToString(RelationInterface $relation, bool $ident
207240
if ($identifying) {
208241
$properties = $relation->getIdentifiers();
209242
}
210-
$propertyString = self::propertyArrayToString($properties);
243+
$propertyString = self::propertiesToString($properties);
211244
if (strlen($propertyString) > 0) {
212245
$relationParts[] = sprintf("{%s}", $propertyString);
213246
}
@@ -229,7 +262,7 @@ public static function relationToString(RelationInterface $relation, bool $ident
229262
*/
230263
public static function optionsToString(array $options): string
231264
{
232-
return '';
265+
return self::propertiesToString($options);
233266
}
234267

235268
public static function nodeConstraintToString(NodeConstraintInterface $nodeConstraint): string
@@ -280,7 +313,7 @@ public static function nodeConstraintToString(NodeConstraintInterface $nodeConst
280313
$options = $nodeConstraint->getOptions();
281314
if (count($options) > 0) {
282315
$parts[] = 'OPTIONS';
283-
$parts[] = self::optionsToString($options);
316+
$parts[] = sprintf("{%s}", self::optionsToString($options));
284317
}
285318

286319
return implode(' ', $parts);
@@ -334,7 +367,7 @@ public static function relationConstraintToString(RelationConstraintInterface $r
334367
$options = $relationConstraint->getOptions();
335368
if (count($options) > 0) {
336369
$parts[] = 'OPTIONS';
337-
$parts[] = self::optionsToString($options);
370+
$parts[] = sprintf("{%s}", self::optionsToString($options));
338371
}
339372

340373
return implode(' ', $parts);
@@ -380,7 +413,7 @@ public static function nodeIndexToString(NodeIndexInterface $nodeIndex): string
380413
$options = $nodeIndex->getOptions();
381414
if (count($options) > 0) {
382415
$parts[] = 'OPTIONS';
383-
$parts[] = self::optionsToString($options);
416+
$parts[] = sprintf("{%s}", self::optionsToString($options));
384417
}
385418

386419
return implode(' ', $parts);
@@ -426,7 +459,7 @@ public static function relationIndexToString(RelationIndexInterface $relationInd
426459
$options = $relationIndex->getOptions();
427460
if (count($options) > 0) {
428461
$parts[] = 'OPTIONS';
429-
$parts[] = self::optionsToString($options);
462+
$parts[] = sprintf("{%s}", self::optionsToString($options));
430463
}
431464

432465
return implode(' ', $parts);

0 commit comments

Comments
 (0)