Skip to content

Commit 9ca8c10

Browse files
Write additional tests and refactor parts
1 parent 572903c commit 9ca8c10

File tree

2 files changed

+229
-67
lines changed

2 files changed

+229
-67
lines changed

src/Literals/Literal.php

Lines changed: 135 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public static function dateTime($timezone = null): DateTimeType
263263
*/
264264
public static function dateTimeYMD($year, $month = null, $day = null, $hour = null, $minute = null, $second = null, $millisecond = null, $microsecond = null, $nanosecond = null, $timezone = null): DateTimeType
265265
{
266-
$setVariables = self::checkOrderAndConvert2Numeral([
266+
$setVariables = self::checkOrderAndConvertToNumeral([
267267
"month"=> $month,
268268
"day" => $day,
269269
"hour" => $hour,
@@ -298,35 +298,6 @@ public static function dateTimeYMD($year, $month = null, $day = null, $hour = nu
298298
return FunctionCall::datetime(Query::map($map));
299299
}
300300

301-
/**
302-
* checks the order of the array items, and converts the items to NumeralType
303-
*
304-
* @param array $array
305-
* @return array
306-
*/
307-
private static function checkOrderAndConvert2Numeral(array $array): array {
308-
$previous = true;
309-
310-
foreach ($array as $key => $setVariable) {
311-
// Only the least significant components may be omitted; check whether this is the case
312-
if (isset($setVariable) === true && $previous === false) {
313-
throw new \LogicException("Only the least significant components may be omitted");
314-
}
315-
316-
// check if variable has been set and convert always to NumeralType
317-
if (isset($setVariable)) {
318-
if (!($setVariable instanceof NumeralType)) {
319-
$setVariable = self::decimal($setVariable);
320-
$array[$key] = $setVariable;
321-
}
322-
}
323-
324-
$previous = isset($setVariable);
325-
}
326-
327-
return $array;
328-
}
329-
330301
/**
331302
* Creates a datetime with the specified year, week, dayOfWeek, hour, minute, second, millisecond, microsecond, nanosecond and timezone component values.
332303
*
@@ -346,7 +317,7 @@ private static function checkOrderAndConvert2Numeral(array $array): array {
346317
*/
347318
public static function datetimeYWD($year, $week = null, $dayOfWeek = null, $hour = null, $minute = null, $second = null, $millisecond = null, $microsecond = null, $nanosecond = null, $timezone = null): DateTimeType
348319
{
349-
$setVariables = self::checkOrderAndConvert2Numeral([
320+
$variables = self::checkOrderAndConvertToNumeral([
350321
"week" => $week,
351322
"dayOfWeek" => $dayOfWeek,
352323
"hour" => $hour,
@@ -363,18 +334,43 @@ public static function datetimeYWD($year, $week = null, $dayOfWeek = null, $hour
363334

364335
$map = ["year" => $year];
365336

366-
if ($week !== null) $map["week"] = $setVariables["week"];
367-
if ($dayOfWeek !== null) $map["dayOfWeek"] = $setVariables["dayOfWeek"];
368-
if ($hour !== null) $map["hour"] = $setVariables["hour"];
369-
if ($minute !== null) $map["minute"] = $setVariables["minute"];
370-
if ($second !== null) $map["second"] = $setVariables["second"];
371-
if ($millisecond !== null) $map["millisecond"] = $setVariables["millisecond"];
372-
if ($microsecond !== null) $map["microsecond"] = $setVariables["microsecond"];
373-
if ($nanosecond !== null) $map["nanosecond"] = $setVariables["nanosecond"];
337+
if ($week !== null) {
338+
$map["week"] = $variables["week"];
339+
}
340+
341+
if ($dayOfWeek !== null) {
342+
$map["dayOfWeek"] = $variables["dayOfWeek"];
343+
}
344+
345+
if ($hour !== null) {
346+
$map["hour"] = $variables["hour"];
347+
}
348+
349+
if ($minute !== null) {
350+
$map["minute"] = $variables["minute"];
351+
}
352+
353+
if ($second !== null) {
354+
$map["second"] = $variables["second"];
355+
}
356+
357+
if ($millisecond !== null) {
358+
$map["millisecond"] = $variables["millisecond"];
359+
}
360+
361+
if ($microsecond !== null) {
362+
$map["microsecond"] = $variables["microsecond"];
363+
}
364+
365+
if ($nanosecond !== null) {
366+
$map["nanosecond"] = $variables["nanosecond"];
367+
}
368+
374369
if ($timezone !== null) {
375370
if (!($timezone instanceof StringType)) {
376371
$timezone = self::string($timezone);
377372
}
373+
378374
$map["timezone"] = $timezone;
379375
}
380376

@@ -399,7 +395,7 @@ public static function datetimeYWD($year, $week = null, $dayOfWeek = null, $hour
399395
* @see https://neo4j.com/docs/cypher-manual/current/functions/temporal/#functions-datetime-quarter
400396
*/
401397
public static function datetimeYQD($year, $quarter = null, $dayOfQuarter = null, $hour = null, $minute = null, $second = null, $millisecond = null, $microsecond = null, $nanosecond = null, $timezone = null): DateTimeType {
402-
$setVariables = self::checkOrderAndConvert2Numeral([
398+
$variables = self::checkOrderAndConvertToNumeral([
403399
"quarter" => $quarter,
404400
"dayOfQuarter" => $dayOfQuarter,
405401
"hour" => $hour,
@@ -416,18 +412,43 @@ public static function datetimeYQD($year, $quarter = null, $dayOfQuarter = null,
416412

417413
$map = ["year" => $year];
418414

419-
if ($quarter !== null) $map["quarter"] = $setVariables["quarter"];
420-
if ($dayOfQuarter !== null) $map["dayOfQuarter"] = $setVariables["dayOfQuarter"];
421-
if ($hour !== null) $map["hour"] = $setVariables["hour"];
422-
if ($minute !== null) $map["minute"] = $setVariables["minute"];
423-
if ($second !== null) $map["second"] = $setVariables["second"];
424-
if ($millisecond !== null) $map["millisecond"] = $setVariables["millisecond"];
425-
if ($microsecond !== null) $map["microsecond"] = $setVariables["microsecond"];
426-
if ($nanosecond !== null) $map["nanosecond"] = $setVariables["nanosecond"];
415+
if ($quarter !== null) {
416+
$map["quarter"] = $variables["quarter"];
417+
}
418+
419+
if ($dayOfQuarter !== null) {
420+
$map["dayOfQuarter"] = $variables["dayOfQuarter"];
421+
}
422+
423+
if ($hour !== null) {
424+
$map["hour"] = $variables["hour"];
425+
}
426+
427+
if ($minute !== null) {
428+
$map["minute"] = $variables["minute"];
429+
}
430+
431+
if ($second !== null) {
432+
$map["second"] = $variables["second"];
433+
}
434+
435+
if ($millisecond !== null) {
436+
$map["millisecond"] = $variables["millisecond"];
437+
}
438+
439+
if ($microsecond !== null) {
440+
$map["microsecond"] = $variables["microsecond"];
441+
}
442+
443+
if ($nanosecond !== null) {
444+
$map["nanosecond"] = $variables["nanosecond"];
445+
}
446+
427447
if ($timezone !== null) {
428448
if (!($timezone instanceof StringType)) {
429449
$timezone = self::string($timezone);
430450
}
451+
431452
$map["timezone"] = $timezone;
432453
}
433454

@@ -451,7 +472,7 @@ public static function datetimeYQD($year, $quarter = null, $dayOfQuarter = null,
451472
* @see https://neo4j.com/docs/cypher-manual/current/functions/temporal/#functions-datetime-ordinal
452473
*/
453474
public static function datetimeYD($year, $ordinalDay = null, $hour = null, $minute = null, $second = null, $millisecond = null, $microsecond = null, $nanosecond = null, $timezone = null): DateTimeType {
454-
$setVariables = self::checkOrderAndConvert2Numeral([
475+
$variables = self::checkOrderAndConvertToNumeral([
455476
"ordinalDay" => $ordinalDay,
456477
"hour" => $hour,
457478
"minute" => $minute,
@@ -467,17 +488,39 @@ public static function datetimeYD($year, $ordinalDay = null, $hour = null, $minu
467488

468489
$map = ["year" => $year];
469490

470-
if ($ordinalDay !== null) $map["ordinalDay"] = $setVariables["ordinalDay"];
471-
if ($hour !== null) $map["hour"] = $setVariables["hour"];
472-
if ($minute !== null) $map["minute"] = $setVariables["minute"];
473-
if ($second !== null) $map["second"] = $setVariables["second"];
474-
if ($millisecond !== null) $map["millisecond"] = $setVariables["millisecond"];
475-
if ($microsecond !== null) $map["microsecond"] = $setVariables["microsecond"];
476-
if ($nanosecond !== null) $map["nanosecond"] = $setVariables["nanosecond"];
491+
if ($ordinalDay !== null) {
492+
$map["ordinalDay"] = $variables["ordinalDay"];
493+
}
494+
495+
if ($hour !== null) {
496+
$map["hour"] = $variables["hour"];
497+
}
498+
499+
if ($minute !== null) {
500+
$map["minute"] = $variables["minute"];
501+
}
502+
503+
if ($second !== null) {
504+
$map["second"] = $variables["second"];
505+
}
506+
507+
if ($millisecond !== null) {
508+
$map["millisecond"] = $variables["millisecond"];
509+
}
510+
511+
if ($microsecond !== null) {
512+
$map["microsecond"] = $variables["microsecond"];
513+
}
514+
515+
if ($nanosecond !== null) {
516+
$map["nanosecond"] = $variables["nanosecond"];
517+
}
518+
477519
if ($timezone !== null) {
478520
if (!($timezone instanceof StringType)) {
479521
$timezone = self::string($timezone);
480522
}
523+
481524
$map["timezone"] = $timezone;
482525
}
483526

@@ -533,7 +576,7 @@ public static function localDatetime($timezone = null): LocalDateTimeType {
533576
* @see https://neo4j.com/docs/cypher-manual/current/functions/temporal/#functions-localdatetime-calendar
534577
*/
535578
public static function localDatetimeYMD($year, $month = null, $day = null, $hour = null, $minute = null, $second = null, $millisecond = null, $microsecond = null, $nanosecond = null): LocalDateTimeType {
536-
$setVariables = self::checkOrderAndConvert2Numeral([
579+
$setVariables = self::checkOrderAndConvertToNumeral([
537580
"month" => $month,
538581
"day" => $day,
539582
"hour" => $hour,
@@ -580,7 +623,7 @@ public static function localDatetimeYMD($year, $month = null, $day = null, $hour
580623
* @see https://neo4j.com/docs/cypher-manual/current/functions/temporal/#functions-localdatetime-week
581624
*/
582625
public static function localDatetimeYWD($year, $week = null, $dayOfWeek = null, $hour = null, $minute = null, $second = null, $millisecond = null, $microsecond = null, $nanosecond = null): LocalDateTimeType {
583-
$setVariables = self::checkOrderAndConvert2Numeral([
626+
$setVariables = self::checkOrderAndConvertToNumeral([
584627
"week" => $week,
585628
"dayOfWeek" => $dayOfWeek,
586629
"hour" => $hour,
@@ -626,7 +669,7 @@ public static function localDatetimeYWD($year, $week = null, $dayOfWeek = null,
626669
* @see https://neo4j.com/docs/cypher-manual/current/functions/temporal/#functions-localdatetime-quarter
627670
*/
628671
public static function localDatetimeYQD($year, $quarter = null, $dayOfQuarter = null, $hour = null, $minute = null, $second = null, $millisecond = null, $microsecond = null, $nanosecond = null): LocalDateTimeType {
629-
$setVariables = self::checkOrderAndConvert2Numeral([
672+
$setVariables = self::checkOrderAndConvertToNumeral([
630673
"quarter" => $quarter,
631674
"dayOfQuarter" => $dayOfQuarter,
632675
"hour" => $hour,
@@ -671,7 +714,7 @@ public static function localDatetimeYQD($year, $quarter = null, $dayOfQuarter =
671714
* @see https://neo4j.com/docs/cypher-manual/current/functions/temporal/#functions-localdatetime-ordinal
672715
*/
673716
public static function localDatetimeYD($year, $ordinalDay = null, $hour = null, $minute = null, $second = null, $millisecond = null, $microsecond = null, $nanosecond = null): LocalDateTimeType {
674-
$setVariables = self::checkOrderAndConvert2Numeral([
717+
$setVariables = self::checkOrderAndConvertToNumeral([
675718
"ordinalDay" => $ordinalDay,
676719
"hour" => $hour,
677720
"minute" => $minute,
@@ -747,7 +790,7 @@ public static function localTimeCurrent($timezone = null): LocalTimeType {
747790
* @see https://neo4j.com/docs/cypher-manual/current/functions/temporal/#functions-localtime-create
748791
*/
749792
public static function localTime($hour, $minute = null, $second = null, $millisecond = null, $microsecond = null, $nanosecond = null): LocalTimeType {
750-
$setVariables = self::checkOrderAndConvert2Numeral([
793+
$setVariables = self::checkOrderAndConvertToNumeral([
751794
"minute" => $minute,
752795
"second" => $second,
753796
"millisecond" => $millisecond,
@@ -818,7 +861,7 @@ public static function timeCurrent($timezone = null): TimeType {
818861
* @see https://neo4j.com/docs/cypher-manual/current/functions/temporal/#functions-time-create
819862
*/
820863
public static function time($hour, $minute = null, $second = null, $millisecond = null, $microsecond = null, $nanosecond = null, $timezone = null): TimeType {
821-
$setVariables = self::checkOrderAndConvert2Numeral([
864+
$setVariables = self::checkOrderAndConvertToNumeral([
822865
"minute" => $minute,
823866
"second" => $second,
824867
"millisecond" => $millisecond,
@@ -993,4 +1036,34 @@ public static function point3dWGS84($longitude, $latitude, $height): PointType
9931036

9941037
return FunctionCall::point(Query::map($map));
9951038
}
1039+
1040+
/**
1041+
* Checks the order of the array items, and converts the items to NumeralType.
1042+
*
1043+
* @param array $variables
1044+
* @return array
1045+
*/
1046+
private static function checkOrderAndConvertToNumeral(array $variables): array {
1047+
$previousExists = true;
1048+
1049+
foreach ($variables as $key => $variable) {
1050+
$currentExists = $variable !== null;
1051+
1052+
// Only the least significant components may be omitted; check whether this is the case
1053+
if ($currentExists === true && $previousExists === false) {
1054+
throw new \LogicException("Only the least significant components may be omitted");
1055+
}
1056+
1057+
// Check if variable has been set and convert always to NumeralType
1058+
if ($currentExists) {
1059+
if (!($variable instanceof NumeralType)) {
1060+
$variables[$key] = self::decimal($variable);
1061+
}
1062+
}
1063+
1064+
$previousExists = $variable !== null;
1065+
}
1066+
1067+
return $variables;
1068+
}
9961069
}

0 commit comments

Comments
 (0)