Refactor Plausi from musdb to move the logic into this subrepository
See #948
This commit is contained in:
99
src/Checks/Plausi/MDPlausiEvent.php
Normal file
99
src/Checks/Plausi/MDPlausiEvent.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?PHP
|
||||
/**
|
||||
* An event as required for the plausibility checker for events.
|
||||
*
|
||||
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
||||
*/
|
||||
declare(strict_types = 1);
|
||||
|
||||
/**
|
||||
* An event as required for the plausibility checker for events.
|
||||
*/
|
||||
final class MDPlausiEvent {
|
||||
|
||||
public readonly int $event_type;
|
||||
public readonly MDEventCategory $event_category;
|
||||
|
||||
public readonly string $time_name;
|
||||
public readonly string $time_start;
|
||||
public readonly string $time_end;
|
||||
public readonly int|false $time_start_normalized;
|
||||
public readonly int|false $time_end_normalized;
|
||||
|
||||
public readonly string $actor_name;
|
||||
public readonly string $actor_birth;
|
||||
public readonly string $actor_death;
|
||||
public readonly int|false $actor_birth_normalized;
|
||||
public readonly int|false $actor_death_normalized;
|
||||
|
||||
/**
|
||||
* Attempts to parse a time.
|
||||
*
|
||||
* @param string $time Time to parse.
|
||||
*
|
||||
* @return array{earliest: int|false, latest: int|false}
|
||||
*/
|
||||
private function _attemptParsingTime(string $time):array {
|
||||
|
||||
if (empty($time)) return ['earliest' => false, 'latest' => false];
|
||||
|
||||
// 2005
|
||||
if (in_array(strlen($time), [4, 5], true) and is_numeric(substr($time, 1))) {
|
||||
return [
|
||||
'earliest' => strtotime($time . '-01-01'),
|
||||
'latest' => strtotime($time . '-12-31'),
|
||||
];
|
||||
}
|
||||
|
||||
// 2005-2006
|
||||
if (strlen($time) === 9) {
|
||||
$parts = explode('-', $time);
|
||||
if (count($parts) === 2) {
|
||||
return [
|
||||
'earliest' => strtotime($parts[0]. '-01-01'),
|
||||
'latest' => strtotime($parts[1]. '-12-31'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$strtotime = strtotime($time);
|
||||
return [
|
||||
'earliest' => $strtotime,
|
||||
'latest' => $strtotime,
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param integer $event_type Event type.
|
||||
* @param string $time_name Time.
|
||||
* @param string $time_start Start.
|
||||
* @param string $time_end End.
|
||||
* @param string $actor_name Name of the linked actor.
|
||||
* @param string $actor_birth Birth of the linked actor.
|
||||
* @param string $actor_death Time of death of the linked actor.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(int $event_type, string $time_name, string $time_start, string $time_end, string $actor_name, string $actor_birth, string $actor_death) {
|
||||
|
||||
$this->event_type = $event_type;
|
||||
$this->time_name = $time_name;
|
||||
$this->time_start = $time_start;
|
||||
$this->time_end = $time_end;
|
||||
$this->actor_name = $actor_name;
|
||||
$this->actor_birth = $actor_birth;
|
||||
$this->actor_death = $actor_death;
|
||||
|
||||
$this->time_start_normalized = $this->_attemptParsingTime($time_start)['earliest'];
|
||||
$this->time_end_normalized = $this->_attemptParsingTime($time_end)['latest'];
|
||||
|
||||
$this->actor_birth_normalized = $this->_attemptParsingTime($actor_birth)['earliest'];
|
||||
$this->actor_death_normalized = $this->_attemptParsingTime($actor_death)['latest'];
|
||||
|
||||
$this->event_category = MDEventCategory::fromEventType($event_type);
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user