| Total Complexity | 112 |
| Total Lines | 740 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Fact often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Fact, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class Fact |
||
| 47 | { |
||
| 48 | private const FACT_ORDER = [ |
||
| 49 | 'BIRT', |
||
| 50 | '_HNM', |
||
| 51 | 'ALIA', |
||
| 52 | '_AKA', |
||
| 53 | '_AKAN', |
||
| 54 | 'ADOP', |
||
| 55 | '_ADPF', |
||
| 56 | '_ADPF', |
||
| 57 | '_BRTM', |
||
| 58 | 'CHR', |
||
| 59 | 'BAPM', |
||
| 60 | 'FCOM', |
||
| 61 | 'CONF', |
||
| 62 | 'BARM', |
||
| 63 | 'BASM', |
||
| 64 | 'EDUC', |
||
| 65 | 'GRAD', |
||
| 66 | '_DEG', |
||
| 67 | 'EMIG', |
||
| 68 | 'IMMI', |
||
| 69 | 'NATU', |
||
| 70 | '_MILI', |
||
| 71 | '_MILT', |
||
| 72 | 'ENGA', |
||
| 73 | 'MARB', |
||
| 74 | 'MARC', |
||
| 75 | 'MARL', |
||
| 76 | '_MARI', |
||
| 77 | '_MBON', |
||
| 78 | 'MARR', |
||
| 79 | '_COML', |
||
| 80 | '_STAT', |
||
| 81 | '_SEPR', |
||
| 82 | 'DIVF', |
||
| 83 | 'MARS', |
||
| 84 | 'DIV', |
||
| 85 | 'ANUL', |
||
| 86 | 'CENS', |
||
| 87 | 'OCCU', |
||
| 88 | 'RESI', |
||
| 89 | 'PROP', |
||
| 90 | 'CHRA', |
||
| 91 | 'RETI', |
||
| 92 | 'FACT', |
||
| 93 | 'EVEN', |
||
| 94 | '_NMR', |
||
| 95 | '_NMAR', |
||
| 96 | 'NMR', |
||
| 97 | 'NCHI', |
||
| 98 | 'WILL', |
||
| 99 | '_HOL', |
||
| 100 | '_????_', |
||
| 101 | 'DEAT', |
||
| 102 | '_FNRL', |
||
| 103 | 'CREM', |
||
| 104 | 'BURI', |
||
| 105 | '_INTE', |
||
| 106 | '_YART', |
||
| 107 | '_NLIV', |
||
| 108 | 'PROB', |
||
| 109 | 'TITL', |
||
| 110 | 'COMM', |
||
| 111 | 'NATI', |
||
| 112 | 'CITN', |
||
| 113 | 'CAST', |
||
| 114 | 'RELI', |
||
| 115 | 'SSN', |
||
| 116 | 'IDNO', |
||
| 117 | 'TEMP', |
||
| 118 | 'SLGC', |
||
| 119 | 'BAPL', |
||
| 120 | 'CONL', |
||
| 121 | 'ENDL', |
||
| 122 | 'SLGS', |
||
| 123 | 'NO', |
||
| 124 | 'ADDR', |
||
| 125 | 'PHON', |
||
| 126 | 'EMAIL', |
||
| 127 | '_EMAIL', |
||
| 128 | 'EMAL', |
||
| 129 | 'FAX', |
||
| 130 | 'WWW', |
||
| 131 | 'URL', |
||
| 132 | '_URL', |
||
| 133 | '_FSFTID', |
||
| 134 | 'AFN', |
||
| 135 | 'REFN', |
||
| 136 | '_PRMN', |
||
| 137 | 'REF', |
||
| 138 | 'RIN', |
||
| 139 | '_UID', |
||
| 140 | 'OBJE', |
||
| 141 | 'NOTE', |
||
| 142 | 'SOUR', |
||
| 143 | 'CREA', |
||
| 144 | 'CHAN', |
||
| 145 | '_TODO', |
||
| 146 | ]; |
||
| 147 | |||
| 148 | // Unique identifier for this fact (currently implemented as a hash of the raw data). |
||
| 149 | private string $id; |
||
| 150 | |||
| 151 | // The GEDCOM record from which this fact is taken |
||
| 152 | private GedcomRecord $record; |
||
| 153 | |||
| 154 | // The raw GEDCOM data for this fact |
||
| 155 | private string $gedcom; |
||
| 156 | |||
| 157 | // The GEDCOM tag for this record |
||
| 158 | private string $tag; |
||
| 159 | |||
| 160 | private bool $pending_deletion = false; |
||
| 161 | |||
| 162 | private bool $pending_addition = false; |
||
| 163 | |||
| 164 | private Date $date; |
||
| 165 | |||
| 166 | private Place $place; |
||
| 167 | |||
| 168 | // Used to sort facts |
||
| 169 | public int $sortOrder; |
||
| 170 | |||
| 171 | // Used by anniversary calculations |
||
| 172 | public int $jd; |
||
| 173 | public int $anniv; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Create an event object from a gedcom fragment. |
||
| 177 | * We need the parent object (to check privacy) and a (pseudo) fact ID to |
||
| 178 | * identify the fact within the record. |
||
| 179 | * |
||
| 180 | * @param string $gedcom |
||
| 181 | * @param GedcomRecord $parent |
||
| 182 | * @param string $id |
||
| 183 | * |
||
| 184 | * @throws InvalidArgumentException |
||
| 185 | */ |
||
| 186 | public function __construct(string $gedcom, GedcomRecord $parent, string $id) |
||
| 187 | { |
||
| 188 | if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) { |
||
| 189 | $this->gedcom = $gedcom; |
||
| 190 | $this->record = $parent; |
||
| 191 | $this->id = $id; |
||
| 192 | $this->tag = $match[1]; |
||
| 193 | } else { |
||
| 194 | throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ',' . $parent->xref() . ')'); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get the value of level 1 data in the fact |
||
| 200 | * Allow for multi-line values |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function value(): string |
||
| 205 | { |
||
| 206 | if (preg_match('/^1 ' . $this->tag . ' ?(.*(?:\n2 CONT ?.*)*)/', $this->gedcom, $match)) { |
||
| 207 | return preg_replace("/\n2 CONT ?/", "\n", $match[1]); |
||
| 208 | } |
||
| 209 | |||
| 210 | return ''; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get the record to which this fact links |
||
| 215 | * |
||
| 216 | * @return Family|GedcomRecord|Individual|Location|Media|Note|Repository|Source|Submission|Submitter|null |
||
| 217 | */ |
||
| 218 | public function target() |
||
| 219 | { |
||
| 220 | if (!preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $this->value(), $match)) { |
||
| 221 | return null; |
||
| 222 | } |
||
| 223 | |||
| 224 | $xref = $match[1]; |
||
| 225 | |||
| 226 | switch ($this->tag) { |
||
| 227 | case 'FAMC': |
||
| 228 | case 'FAMS': |
||
| 229 | return Registry::familyFactory()->make($xref, $this->record->tree()); |
||
| 230 | case 'HUSB': |
||
| 231 | case 'WIFE': |
||
| 232 | case 'ALIA': |
||
| 233 | case 'CHIL': |
||
| 234 | case '_ASSO': |
||
| 235 | return Registry::individualFactory()->make($xref, $this->record->tree()); |
||
| 236 | case 'ASSO': |
||
| 237 | return |
||
| 238 | Registry::individualFactory()->make($xref, $this->record->tree()) ?? |
||
| 239 | Registry::submitterFactory()->make($xref, $this->record->tree()); |
||
| 240 | case 'SOUR': |
||
| 241 | return Registry::sourceFactory()->make($xref, $this->record->tree()); |
||
| 242 | case 'OBJE': |
||
| 243 | return Registry::mediaFactory()->make($xref, $this->record->tree()); |
||
| 244 | case 'REPO': |
||
| 245 | return Registry::repositoryFactory()->make($xref, $this->record->tree()); |
||
| 246 | case 'NOTE': |
||
| 247 | return Registry::noteFactory()->make($xref, $this->record->tree()); |
||
| 248 | case 'ANCI': |
||
| 249 | case 'DESI': |
||
| 250 | case 'SUBM': |
||
| 251 | return Registry::submitterFactory()->make($xref, $this->record->tree()); |
||
| 252 | case 'SUBN': |
||
| 253 | return Registry::submissionFactory()->make($xref, $this->record->tree()); |
||
| 254 | case '_LOC': |
||
| 255 | return Registry::locationFactory()->make($xref, $this->record->tree()); |
||
| 256 | default: |
||
| 257 | return Registry::gedcomRecordFactory()->make($xref, $this->record->tree()); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get the value of level 2 data in the fact |
||
| 263 | * |
||
| 264 | * @param string $tag |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function attribute(string $tag): string |
||
| 269 | { |
||
| 270 | if (preg_match('/\n2 ' . $tag . '\b ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) { |
||
| 271 | $value = preg_replace("/\n3 CONT ?/", "\n", $match[1]); |
||
| 272 | |||
| 273 | return Registry::elementFactory()->make($this->tag() . ':' . $tag)->canonical($value); |
||
| 274 | } |
||
| 275 | |||
| 276 | return ''; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get the PLAC:MAP:LATI for the fact. |
||
| 281 | * |
||
| 282 | * @return float|null |
||
| 283 | */ |
||
| 284 | public function latitude(): ?float |
||
| 285 | { |
||
| 286 | if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) { |
||
| 287 | $gedcom_service = new GedcomService(); |
||
| 288 | |||
| 289 | return $gedcom_service->readLatitude($match[1]); |
||
| 290 | } |
||
| 291 | |||
| 292 | return null; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get the PLAC:MAP:LONG for the fact. |
||
| 297 | * |
||
| 298 | * @return float|null |
||
| 299 | */ |
||
| 300 | public function longitude(): ?float |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Do the privacy rules allow us to display this fact to the current user |
||
| 313 | * |
||
| 314 | * @param int|null $access_level |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | public function canShow(int $access_level = null): bool |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Check whether this fact is protected against edit |
||
| 361 | * |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | public function canEdit(): bool |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * The place where the event occured. |
||
| 380 | * |
||
| 381 | * @return Place |
||
| 382 | */ |
||
| 383 | public function place(): Place |
||
| 384 | { |
||
| 385 | $this->place ??= new Place($this->attribute('PLAC'), $this->record->tree()); |
||
| 386 | |||
| 387 | return $this->place; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Get the date for this fact. |
||
| 392 | * We can call this function many times, especially when sorting, |
||
| 393 | * so keep a copy of the date. |
||
| 394 | * |
||
| 395 | * @return Date |
||
| 396 | */ |
||
| 397 | public function date(): Date |
||
| 398 | { |
||
| 399 | $this->date ??= new Date($this->attribute('DATE')); |
||
| 400 | |||
| 401 | return $this->date; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * The raw GEDCOM data for this fact |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public function gedcom(): string |
||
| 410 | { |
||
| 411 | return $this->gedcom; |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get a (pseudo) primary key for this fact. |
||
| 416 | * |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | public function id(): string |
||
| 420 | { |
||
| 421 | return $this->id; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * What is the tag (type) of this fact, such as BIRT, MARR or DEAT. |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function tag(): string |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * The GEDCOM record where this Fact came from |
||
| 436 | * |
||
| 437 | * @return GedcomRecord |
||
| 438 | */ |
||
| 439 | public function record(): GedcomRecord |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Get the name of this fact type, for use as a label. |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function label(): string |
||
| 450 | { |
||
| 451 | if (str_ends_with($this->tag(), ':NOTE') && preg_match('/^@' . Gedcom::REGEX_XREF . '@$/', $this->value())) { |
||
| 452 | return I18N::translate('Shared note'); |
||
| 453 | } |
||
| 454 | |||
| 455 | // Marriages |
||
| 456 | if ($this->tag() === 'FAM:MARR') { |
||
| 457 | $element = Registry::elementFactory()->make('FAM:MARR:TYPE'); |
||
| 458 | $type = $this->attribute('TYPE'); |
||
| 459 | |||
| 460 | if ($type !== '') { |
||
| 461 | return $element->value($type, $this->record->tree()); |
||
| 462 | } |
||
| 463 | } |
||
| 464 | |||
| 465 | // Custom FACT/EVEN - with a TYPE |
||
| 466 | if ($this->tag === 'FACT' || $this->tag === 'EVEN') { |
||
| 467 | $type = $this->attribute('TYPE'); |
||
| 468 | |||
| 469 | if ($type !== '') { |
||
| 470 | if (!str_contains($type, '%')) { |
||
| 471 | // Allow user-translations of custom types. |
||
| 472 | $translated = I18N::translate($type); |
||
| 473 | |||
| 474 | if ($translated !== $type) { |
||
| 475 | return $translated; |
||
| 476 | } |
||
| 477 | } |
||
| 478 | |||
| 479 | return e($type); |
||
| 480 | } |
||
| 481 | } |
||
| 482 | |||
| 483 | return Registry::elementFactory()->make($this->tag())->label(); |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * This is a newly deleted fact, pending approval. |
||
| 488 | * |
||
| 489 | * @return void |
||
| 490 | */ |
||
| 491 | public function setPendingDeletion(): void |
||
| 492 | { |
||
| 493 | $this->pending_deletion = true; |
||
| 494 | $this->pending_addition = false; |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Is this a newly deleted fact, pending approval. |
||
| 499 | * |
||
| 500 | * @return bool |
||
| 501 | */ |
||
| 502 | public function isPendingDeletion(): bool |
||
| 503 | { |
||
| 504 | return $this->pending_deletion; |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * This is a newly added fact, pending approval. |
||
| 509 | * |
||
| 510 | * @return void |
||
| 511 | */ |
||
| 512 | public function setPendingAddition(): void |
||
| 513 | { |
||
| 514 | $this->pending_addition = true; |
||
| 515 | $this->pending_deletion = false; |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Is this a newly added fact, pending approval. |
||
| 520 | * |
||
| 521 | * @return bool |
||
| 522 | */ |
||
| 523 | public function isPendingAddition(): bool |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * A one-line summary of the fact - for charts, etc. |
||
| 530 | * |
||
| 531 | * @return string |
||
| 532 | */ |
||
| 533 | public function summary(): string |
||
| 534 | { |
||
| 535 | $attributes = []; |
||
| 536 | $target = $this->target(); |
||
| 537 | if ($target instanceof GedcomRecord) { |
||
| 538 | $attributes[] = $target->fullName(); |
||
| 539 | } else { |
||
| 540 | // Fact value |
||
| 541 | $value = $this->value(); |
||
| 542 | if ($value !== '' && $value !== 'Y') { |
||
| 543 | $attributes[] = '<bdi>' . e($value) . '</bdi>'; |
||
| 544 | } |
||
| 545 | // Fact date |
||
| 546 | $date = $this->date(); |
||
| 547 | if ($date->isOK()) { |
||
| 548 | if ($this->record instanceof Individual && in_array($this->tag, Gedcom::BIRTH_EVENTS, true) && $this->record->tree()->getPreference('SHOW_PARENTS_AGE')) { |
||
| 549 | $attributes[] = $date->display() . view('fact-parents-age', ['individual' => $this->record, 'birth_date' => $date]); |
||
| 550 | } else { |
||
| 551 | $attributes[] = $date->display(); |
||
| 552 | } |
||
| 553 | } |
||
| 554 | // Fact place |
||
| 555 | if ($this->place()->gedcomName() !== '') { |
||
| 556 | $attributes[] = $this->place()->shortName(); |
||
| 557 | } |
||
| 558 | } |
||
| 559 | |||
| 560 | $class = 'fact_' . $this->tag; |
||
| 561 | if ($this->isPendingAddition()) { |
||
| 562 | $class .= ' wt-new'; |
||
| 563 | } elseif ($this->isPendingDeletion()) { |
||
| 564 | $class .= ' wt-old'; |
||
| 565 | } |
||
| 566 | |||
| 567 | $label = '<span class="label">' . $this->label() . '</span>'; |
||
| 568 | $value = '<span class="field" dir="auto">' . implode(' — ', $attributes) . '</span>'; |
||
| 569 | |||
| 570 | /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */ |
||
| 571 | return '<div class="' . $class . '">' . I18N::translate('%1$s: %2$s', $label, $value) . '</div>'; |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * A one-line summary of the fact - for the clipboard, etc. |
||
| 576 | * |
||
| 577 | * @return string |
||
| 578 | */ |
||
| 579 | public function name(): string |
||
| 580 | { |
||
| 581 | $items = [$this->label()]; |
||
| 582 | $target = $this->target(); |
||
| 583 | |||
| 584 | if ($target instanceof GedcomRecord) { |
||
| 585 | $items[] = '<bdi>' . $target->fullName() . '</bdi>'; |
||
| 586 | } else { |
||
| 587 | // Fact value |
||
| 588 | $value = $this->value(); |
||
| 589 | if ($value !== '' && $value !== 'Y') { |
||
| 590 | $items[] = '<bdi>' . e($value) . '</bdi>'; |
||
| 591 | } |
||
| 592 | |||
| 593 | // Fact date |
||
| 594 | if ($this->date()->isOK()) { |
||
| 595 | $items[] = $this->date()->minimumDate()->format('%Y'); |
||
| 596 | } |
||
| 597 | |||
| 598 | // Fact place |
||
| 599 | if ($this->place()->gedcomName() !== '') { |
||
| 600 | $items[] = $this->place()->shortName(); |
||
| 601 | } |
||
| 602 | } |
||
| 603 | |||
| 604 | return implode(' — ', $items); |
||
| 605 | } |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Helper functions to sort facts |
||
| 609 | * |
||
| 610 | * @return Closure |
||
| 611 | */ |
||
| 612 | private static function dateComparator(): Closure |
||
| 613 | { |
||
| 614 | return static function (Fact $a, Fact $b): int { |
||
| 615 | if ($a->date()->isOK() && $b->date()->isOK()) { |
||
| 616 | // If both events have dates, compare by date |
||
| 617 | $ret = Date::compare($a->date(), $b->date()); |
||
| 618 | |||
| 619 | if ($ret === 0) { |
||
| 620 | // If dates overlap, compare by fact type |
||
| 621 | $ret = self::typeComparator()($a, $b); |
||
| 622 | |||
| 623 | // If the fact type is also the same, retain the initial order |
||
| 624 | if ($ret === 0) { |
||
| 625 | $ret = $a->sortOrder <=> $b->sortOrder; |
||
| 626 | } |
||
| 627 | } |
||
| 628 | |||
| 629 | return $ret; |
||
| 630 | } |
||
| 631 | |||
| 632 | // One or both events have no date - retain the initial order |
||
| 633 | return $a->sortOrder <=> $b->sortOrder; |
||
| 634 | }; |
||
| 635 | } |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Helper functions to sort facts. |
||
| 639 | * |
||
| 640 | * @return Closure |
||
| 641 | */ |
||
| 642 | public static function typeComparator(): Closure |
||
| 643 | { |
||
| 644 | static $factsort = []; |
||
| 645 | |||
| 646 | if ($factsort === []) { |
||
| 647 | $factsort = array_flip(self::FACT_ORDER); |
||
| 648 | } |
||
| 649 | |||
| 650 | return static function (Fact $a, Fact $b) use ($factsort): int { |
||
| 651 | // Facts from same families stay grouped together |
||
| 652 | // Keep MARR and DIV from the same families from mixing with events from other FAMs |
||
| 653 | // Use the original order in which the facts were added |
||
| 654 | if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) { |
||
| 655 | return $a->sortOrder <=> $b->sortOrder; |
||
| 656 | } |
||
| 657 | |||
| 658 | $atag = $a->tag; |
||
| 659 | $btag = $b->tag; |
||
| 660 | |||
| 661 | // Events not in the above list get mapped onto one that is. |
||
| 662 | if (!array_key_exists($atag, $factsort)) { |
||
| 663 | $atag = '_????_'; |
||
| 664 | } |
||
| 665 | |||
| 666 | if (!array_key_exists($btag, $factsort)) { |
||
| 667 | $btag = '_????_'; |
||
| 668 | } |
||
| 669 | |||
| 670 | // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI |
||
| 671 | // - Treat dated after BURI facts as BURI instead |
||
| 672 | if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) { |
||
| 673 | $atag = 'BURI'; |
||
| 674 | } |
||
| 675 | |||
| 676 | if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) { |
||
| 677 | $btag = 'BURI'; |
||
| 678 | } |
||
| 679 | |||
| 680 | // If facts are the same then put dated facts before non-dated facts |
||
| 681 | if ($atag === $btag) { |
||
| 682 | if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') { |
||
| 683 | return -1; |
||
| 684 | } |
||
| 685 | |||
| 686 | if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') { |
||
| 687 | return 1; |
||
| 688 | } |
||
| 689 | |||
| 690 | // If no sorting preference, then keep original ordering |
||
| 691 | return $a->sortOrder <=> $b->sortOrder; |
||
| 692 | } |
||
| 693 | |||
| 694 | return $factsort[$atag] <=> $factsort[$btag]; |
||
| 695 | }; |
||
| 696 | } |
||
| 697 | |||
| 698 | /** |
||
| 699 | * A multi-key sort |
||
| 700 | * 1. First divide the facts into two arrays one set with dates and one set without dates |
||
| 701 | * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated |
||
| 702 | * using the compare type function |
||
| 703 | * 3. Then merge the arrays back into the original array using the compare type function |
||
| 704 | * |
||
| 705 | * @param Collection<int,Fact> $unsorted |
||
| 706 | * |
||
| 707 | * @return Collection<int,Fact> |
||
| 708 | */ |
||
| 709 | public static function sortFacts(Collection $unsorted): Collection |
||
| 710 | { |
||
| 711 | $dated = []; |
||
| 712 | $nondated = []; |
||
| 713 | $sorted = []; |
||
| 714 | |||
| 715 | // Split the array into dated and non-dated arrays |
||
| 716 | $order = 0; |
||
| 717 | |||
| 718 | foreach ($unsorted as $fact) { |
||
| 719 | $fact->sortOrder = $order; |
||
| 720 | $order++; |
||
| 721 | |||
| 722 | if ($fact->date()->isOK()) { |
||
| 723 | $dated[] = $fact; |
||
| 724 | } else { |
||
| 725 | $nondated[] = $fact; |
||
| 726 | } |
||
| 727 | } |
||
| 728 | |||
| 729 | usort($dated, self::dateComparator()); |
||
| 730 | usort($nondated, self::typeComparator()); |
||
| 731 | |||
| 732 | // Merge the arrays |
||
| 733 | $dc = count($dated); |
||
| 734 | $nc = count($nondated); |
||
| 735 | $i = 0; |
||
| 736 | $j = 0; |
||
| 737 | |||
| 738 | // while there is anything in the dated array continue merging |
||
| 739 | while ($i < $dc) { |
||
| 740 | // compare each fact by type to merge them in order |
||
| 741 | if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) { |
||
| 742 | $sorted[] = $nondated[$j]; |
||
| 743 | $j++; |
||
| 744 | } else { |
||
| 745 | $sorted[] = $dated[$i]; |
||
| 746 | $i++; |
||
| 747 | } |
||
| 748 | } |
||
| 749 | |||
| 750 | // get anything that might be left in the nondated array |
||
| 751 | while ($j < $nc) { |
||
| 752 | $sorted[] = $nondated[$j]; |
||
| 753 | $j++; |
||
| 754 | } |
||
| 755 | |||
| 756 | return new Collection($sorted); |
||
| 757 | } |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Sort fact/event tags using the same order that we use for facts. |
||
| 761 | * |
||
| 762 | * @param Collection<int,string> $unsorted |
||
| 763 | * |
||
| 764 | * @return Collection<int,string> |
||
| 765 | */ |
||
| 766 | public static function sortFactTags(Collection $unsorted): Collection |
||
| 775 | }); |
||
| 776 | } |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Allow native PHP functions such as array_unique() to work with objects |
||
| 780 | * |
||
| 781 | * @return string |
||
| 782 | */ |
||
| 783 | public function __toString(): string |
||
| 786 | } |
||
| 787 | } |
||
| 788 |