ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Data.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 
29 class Data implements DataInterface
30 {
34 
35  public function __construct(
36  UtilitiesInterface $utilities,
37  DataHelperInterface $data_helper,
38  VocabulariesPresentation $vocab_presentation
39  ) {
40  $this->utilities = $utilities;
41  $this->data_helper = $data_helper;
42  $this->vocab_presentation = $vocab_presentation;
43  }
44 
45  public function dataValue(ElementsDataInterface $data): string
46  {
47  switch ($data->type()) {
48  case Type::VOCAB_VALUE:
49  case Type::STRING:
50  return $this->vocabularyValue($data->value(), $data->vocabularySlot());
51 
52  case Type::LANG:
53  return $this->language($data->value());
54 
55  case Type::DATETIME:
56  return $this->datetime($data->value());
57 
58  case Type::DURATION:
59  return $this->duration($data->value());
60 
61  default:
62  return $data->value();
63  }
64  }
65 
66  public function vocabularyValue(
67  string $value,
68  SlotIdentifier $vocabulary_slot
69  ): string {
70  return $this->vocab_presentation->presentableLabels(
71  $this->utilities,
72  $vocabulary_slot,
73  false,
74  $value
75  )->current()->label();
76  }
77 
78  public function language(string $language): string
79  {
80  return $this->utilities->txt('meta_l_' . $language);
81  }
82 
83  public function datetime(string $datetime): string
84  {
85  $date = $this->data_helper->datetimeToObject($datetime);
86  return $this->utilities->getUserDateFormat()->applyTo($date);
87  }
88 
89  public function duration(string $duration): string
90  {
91  $labels = [
92  ['years', 'year'],
93  ['months', 'month'],
94  ['days', 'day'],
95  ['hours', 'hour'],
96  ['minutes', 'minute'],
97  ['seconds', 'second'],
98  ];
99  $res_array = [];
100  foreach ($this->data_helper->durationToIterator($duration) as $key => $match) {
101  if (!is_null($match)) {
102  $res_array[] =
103  $match . ' ' .
104  ($match === '1' ?
105  $this->utilities->txt($labels[$key][1]) :
106  $this->utilities->txt($labels[$key][0]));
107  }
108  }
109  return implode(', ', $res_array);
110  }
111 }
$datetime
UtilitiesInterface $utilities
Definition: Data.php:31
$duration
language(string $language)
Definition: Data.php:78
VocabulariesPresentation $vocab_presentation
Definition: Data.php:33
vocabularyValue(string $value, SlotIdentifier $vocabulary_slot)
Definition: Data.php:66
duration(string $duration)
Definition: Data.php:89
DataHelperInterface $data_helper
Definition: Data.php:32
dataValue(ElementsDataInterface $data)
Definition: Data.php:45
__construct(UtilitiesInterface $utilities, DataHelperInterface $data_helper, VocabulariesPresentation $vocab_presentation)
Definition: Data.php:35
datetime(string $datetime)
Definition: Data.php:83