Пример #1
0
 /**
  * @covers ::unitValue
  */
 public function testUnitValue()
 {
     $this->assertEquals(['year', 1970], Era::find(1)->unitValue(['value' => 1970, 'code' => 'ad']));
     $this->assertEquals(['year', -1969], Era::find(1)->unitValue(['value' => 1970, 'code' => 'bc']));
     $this->assertEquals(['year', 1970], Era::find(1)->unitValue(['value' => 1970]));
     $this->assertEquals(['year', 0], Era::find(1)->unitValue([]));
 }
Пример #2
0
 /**
  * Parse date/time string into unit => value array
  *
  * @param string $date The date/time string to parse
  * @param string $format An optional date format string; must be a raw format
  *                       string, which is calendar-specific
  * @return array
  */
 protected function parseDate($date, $format = '')
 {
     $formats = method_exists($this->formats, 'pluck') ? $this->formats->pluck('format_string') : $this->formats->fetch('format_string');
     $formats->prepend($format);
     foreach ($formats as $format) {
         $unitArray = [];
         $fragments = [];
         $parseArray = [];
         $parseString = '';
         for ($i = 0; $i < mb_strlen($format); $i++) {
             $char = mb_substr($format, $i, 1);
             if ($char == '\\') {
                 $parseString .= mb_substr($format, ++$i, 1);
                 continue;
             } elseif ($char == '%') {
                 $parseString .= '%%';
                 continue;
             } elseif (in_array($char, ['_'])) {
                 $parseString .= $char;
                 continue;
             }
             if (with($fmtObj = $this->fragments()->where('format_code', 'like binary', $char))->count() > 0) {
                 $parseArray[] = $fmtObj->first();
                 $parseString .= $fmtObj->first()->getParseString();
             } else {
                 $parseString .= $char;
             }
         }
         $parsed = sscanf(str_replace(' ', '\\ ', $date), str_replace(' ', '\\ ', $parseString));
         if (is_array($parsed) && !empty($parsed) && !in_array(null, $parsed, true) && count($parsed) == count($parseArray)) {
             foreach ($parsed as $index => $value) {
                 $values = $parseArray[$index]->parseValue($value);
                 foreach ($values as $value) {
                     Arr::set($fragments, $value[0], $value[1]);
                 }
             }
             foreach (Arr::get($fragments, 'unit', []) as $unit => $value) {
                 if (array_key_exists('value', $value)) {
                     $unitArray[$unit] = $value['value'];
                 }
             }
             foreach (Arr::get($fragments, 'era', []) as $era => $value) {
                 $eraObj = Era::where('internal_name', $era)->first();
                 list($unit, $value) = $eraObj->unitValue($value);
                 $unitArray[$unit] = $value;
             }
             return $unitArray;
         }
     }
     return $this->getEpochUnitArray(true);
 }