|
6 | 6 |
|
7 | 7 | use Exception;
|
8 | 8 | use Stringable;
|
| 9 | +use Syndesi\CypherDataStructures\Contract\NodeConstraintInterface; |
| 10 | +use Syndesi\CypherDataStructures\Contract\NodeIndexInterface; |
9 | 11 | use Syndesi\CypherDataStructures\Contract\NodeInterface;
|
| 12 | +use Syndesi\CypherDataStructures\Contract\RelationConstraintInterface; |
| 13 | +use Syndesi\CypherDataStructures\Contract\RelationIndexInterface; |
10 | 14 | use Syndesi\CypherDataStructures\Contract\RelationInterface;
|
11 | 15 | use Syndesi\CypherDataStructures\Exception\InvalidArgumentException;
|
12 | 16 |
|
@@ -221,4 +225,212 @@ public static function relationToString(RelationInterface $relation, bool $ident
|
221 | 225 |
|
222 | 226 | return implode('', $parts);
|
223 | 227 | }
|
| 228 | + |
| 229 | + /** |
| 230 | + * @param array<string, mixed> $options |
| 231 | + */ |
| 232 | + public static function optionsToString(array $options): string |
| 233 | + { |
| 234 | + return ''; |
| 235 | + } |
| 236 | + |
| 237 | + public static function nodeConstraintToString(NodeConstraintInterface $nodeConstraint): string |
| 238 | + { |
| 239 | + $parts = []; |
| 240 | + |
| 241 | + $parts[] = 'CONSTRAINT'; |
| 242 | + |
| 243 | + if ($name = $nodeConstraint->getName()) { |
| 244 | + $parts[] = $name; |
| 245 | + } |
| 246 | + |
| 247 | + $parts[] = 'FOR'; |
| 248 | + |
| 249 | + $for = $nodeConstraint->getFor(); |
| 250 | + if (!$for) { |
| 251 | + throw new InvalidArgumentException('For can not be null'); |
| 252 | + } |
| 253 | + $parts[] = sprintf("(node:%s)", $for); |
| 254 | + |
| 255 | + $parts[] = 'REQUIRE'; |
| 256 | + |
| 257 | + $properties = array_keys($nodeConstraint->getProperties()); |
| 258 | + sort($properties); |
| 259 | + $propertyCount = count($properties); |
| 260 | + if (0 === $propertyCount) { |
| 261 | + throw new InvalidArgumentException('At least one property is required'); |
| 262 | + } |
| 263 | + if (1 === $propertyCount) { |
| 264 | + $parts[] = sprintf("node.%s", array_shift($properties)); |
| 265 | + } |
| 266 | + if ($propertyCount > 1) { |
| 267 | + $propertyParts = []; |
| 268 | + foreach ($properties as $property) { |
| 269 | + $propertyParts[] = sprintf("node.%s", $property); |
| 270 | + } |
| 271 | + $parts[] = sprintf("(%s)", implode(', ', $propertyParts)); |
| 272 | + } |
| 273 | + |
| 274 | + $parts[] = 'IS'; |
| 275 | + |
| 276 | + $type = $nodeConstraint->getType(); |
| 277 | + if (!$type) { |
| 278 | + throw new InvalidArgumentException('Type can not be null'); |
| 279 | + } |
| 280 | + $parts[] = $type; |
| 281 | + |
| 282 | + $options = $nodeConstraint->getOptions(); |
| 283 | + if (count($options) > 0) { |
| 284 | + $parts[] = 'OPTIONS'; |
| 285 | + $parts[] = self::optionsToString($options); |
| 286 | + } |
| 287 | + |
| 288 | + return implode(' ', $parts); |
| 289 | + } |
| 290 | + |
| 291 | + public static function relationConstraintToString(RelationConstraintInterface $relationConstraint): string |
| 292 | + { |
| 293 | + $parts = []; |
| 294 | + |
| 295 | + $parts[] = 'CONSTRAINT'; |
| 296 | + |
| 297 | + if ($name = $relationConstraint->getName()) { |
| 298 | + $parts[] = $name; |
| 299 | + } |
| 300 | + |
| 301 | + $parts[] = 'FOR'; |
| 302 | + |
| 303 | + $for = $relationConstraint->getFor(); |
| 304 | + if (!$for) { |
| 305 | + throw new InvalidArgumentException('For can not be null'); |
| 306 | + } |
| 307 | + $parts[] = sprintf("()-[relation:%s]-()", $for); |
| 308 | + |
| 309 | + $parts[] = 'REQUIRE'; |
| 310 | + |
| 311 | + $properties = array_keys($relationConstraint->getProperties()); |
| 312 | + sort($properties); |
| 313 | + $propertyCount = count($properties); |
| 314 | + if (0 === $propertyCount) { |
| 315 | + throw new InvalidArgumentException('At least one property is required'); |
| 316 | + } |
| 317 | + if (1 === $propertyCount) { |
| 318 | + $parts[] = sprintf("relation.%s", array_shift($properties)); |
| 319 | + } |
| 320 | + if ($propertyCount > 1) { |
| 321 | + $propertyParts = []; |
| 322 | + foreach ($properties as $property) { |
| 323 | + $propertyParts[] = sprintf("relation.%s", $property); |
| 324 | + } |
| 325 | + $parts[] = sprintf("(%s)", implode(', ', $propertyParts)); |
| 326 | + } |
| 327 | + |
| 328 | + $parts[] = 'IS'; |
| 329 | + |
| 330 | + $type = $relationConstraint->getType(); |
| 331 | + if (!$type) { |
| 332 | + throw new InvalidArgumentException('Type can not be null'); |
| 333 | + } |
| 334 | + $parts[] = $type; |
| 335 | + |
| 336 | + $options = $relationConstraint->getOptions(); |
| 337 | + if (count($options) > 0) { |
| 338 | + $parts[] = 'OPTIONS'; |
| 339 | + $parts[] = self::optionsToString($options); |
| 340 | + } |
| 341 | + |
| 342 | + return implode(' ', $parts); |
| 343 | + } |
| 344 | + |
| 345 | + public static function nodeIndexToString(NodeIndexInterface $nodeIndex): string |
| 346 | + { |
| 347 | + $parts = []; |
| 348 | + |
| 349 | + $type = $nodeIndex->getType(); |
| 350 | + if ($type) { |
| 351 | + $parts[] = $type; |
| 352 | + } |
| 353 | + |
| 354 | + $parts[] = 'INDEX'; |
| 355 | + |
| 356 | + if ($name = $nodeIndex->getName()) { |
| 357 | + $parts[] = $name; |
| 358 | + } |
| 359 | + |
| 360 | + $parts[] = 'FOR'; |
| 361 | + |
| 362 | + $for = $nodeIndex->getFor(); |
| 363 | + if (!$for) { |
| 364 | + throw new InvalidArgumentException('For can not be null'); |
| 365 | + } |
| 366 | + $parts[] = sprintf("(node:%s)", $for); |
| 367 | + |
| 368 | + $parts[] = 'ON'; |
| 369 | + |
| 370 | + $properties = array_keys($nodeIndex->getProperties()); |
| 371 | + sort($properties); |
| 372 | + $propertyCount = count($properties); |
| 373 | + if (0 === $propertyCount) { |
| 374 | + throw new InvalidArgumentException('At least one property is required'); |
| 375 | + } |
| 376 | + $propertyParts = []; |
| 377 | + foreach ($properties as $property) { |
| 378 | + $propertyParts[] = sprintf("node.%s", $property); |
| 379 | + } |
| 380 | + $parts[] = sprintf("(%s)", implode(', ', $propertyParts)); |
| 381 | + |
| 382 | + $options = $nodeIndex->getOptions(); |
| 383 | + if (count($options) > 0) { |
| 384 | + $parts[] = 'OPTIONS'; |
| 385 | + $parts[] = self::optionsToString($options); |
| 386 | + } |
| 387 | + |
| 388 | + return implode(' ', $parts); |
| 389 | + } |
| 390 | + |
| 391 | + public static function relationIndexToString(RelationIndexInterface $relationIndex): string |
| 392 | + { |
| 393 | + $parts = []; |
| 394 | + |
| 395 | + $type = $relationIndex->getType(); |
| 396 | + if ($type) { |
| 397 | + $parts[] = $type; |
| 398 | + } |
| 399 | + |
| 400 | + $parts[] = 'INDEX'; |
| 401 | + |
| 402 | + if ($name = $relationIndex->getName()) { |
| 403 | + $parts[] = $name; |
| 404 | + } |
| 405 | + |
| 406 | + $parts[] = 'FOR'; |
| 407 | + |
| 408 | + $for = $relationIndex->getFor(); |
| 409 | + if (!$for) { |
| 410 | + throw new InvalidArgumentException('For can not be null'); |
| 411 | + } |
| 412 | + $parts[] = sprintf("()-[relation:%s]-()", $for); |
| 413 | + |
| 414 | + $parts[] = 'ON'; |
| 415 | + |
| 416 | + $properties = array_keys($relationIndex->getProperties()); |
| 417 | + sort($properties); |
| 418 | + $propertyCount = count($properties); |
| 419 | + if (0 === $propertyCount) { |
| 420 | + throw new InvalidArgumentException('At least one property is required'); |
| 421 | + } |
| 422 | + $propertyParts = []; |
| 423 | + foreach ($properties as $property) { |
| 424 | + $propertyParts[] = sprintf("relation.%s", $property); |
| 425 | + } |
| 426 | + $parts[] = sprintf("(%s)", implode(', ', $propertyParts)); |
| 427 | + |
| 428 | + $options = $relationIndex->getOptions(); |
| 429 | + if (count($options) > 0) { |
| 430 | + $parts[] = 'OPTIONS'; |
| 431 | + $parts[] = self::optionsToString($options); |
| 432 | + } |
| 433 | + |
| 434 | + return implode(' ', $parts); |
| 435 | + } |
224 | 436 | }
|
0 commit comments