ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
Data.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 
27 class Data implements DataInterface
28 {
31 
32  public function __construct(
33  UtilitiesInterface $utilities,
34  DataHelperInterface $data_helper
35  ) {
36  $this->utilities = $utilities;
37  $this->data_helper = $data_helper;
38  }
39 
40  public function dataValue(ElementsDataInterface $data): string
41  {
42  switch ($data->type()) {
43  case Type::VOCAB_VALUE:
44  return $this->vocabularyValue($data->value());
45 
46  case Type::LANG:
47  return $this->language($data->value());
48 
49  case Type::DATETIME:
50  return $this->datetime($data->value());
51 
52  case Type::DURATION:
53  return $this->duration($data->value());
54 
55  default:
56  return $data->value();
57  }
58  }
59 
60  public function vocabularyValue(string $value): string
61  {
62  $value = $this->camelCaseToSpaces($value);
63  $exceptions = [
64  'ispartof' => 'is_part_of', 'haspart' => 'has_part',
65  'isversionof' => 'is_version_of', 'hasversion' => 'has_version',
66  'isformatof' => 'is_format_of', 'hasformat' => 'has_format',
67  'references' => 'references',
68  'isreferencedby' => 'is_referenced_by',
69  'isbasedon' => 'is_based_on', 'isbasisfor' => 'is_basis_for',
70  'requires' => 'requires', 'isrequiredby' => 'is_required_by',
71  'graphical designer' => 'graphicaldesigner',
72  'technical implementer' => 'technicalimplementer',
73  'content provider' => 'contentprovider',
74  'technical validator' => 'technicalvalidator',
75  'educational validator' => 'educationalvalidator',
76  'script writer' => 'scriptwriter',
77  'instructional designer' => 'instructionaldesigner',
78  'subject matter expert' => 'subjectmatterexpert',
79  'diagram' => 'diagramm'
80  ];
81  if (array_key_exists($value, $exceptions)) {
82  $value = $exceptions[$value];
83  }
84 
85  return $this->utilities->txt('meta_' . $this->fillSpaces($value));
86  }
87 
88  public function language(string $language): string
89  {
90  return $this->utilities->txt('meta_l_' . $language);
91  }
92 
93  public function datetime(string $datetime): string
94  {
95  $date = $this->data_helper->datetimeToObject($datetime);
96  return $this->utilities->getUserDateFormat()->applyTo($date);
97  }
98 
99  public function duration(string $duration): string
100  {
101  $labels = [
102  ['years', 'year'],
103  ['months', 'month'],
104  ['days', 'day'],
105  ['hours', 'hour'],
106  ['minutes', 'minute'],
107  ['seconds', 'second'],
108  ];
109  $res_array = [];
110  foreach ($this->data_helper->durationToIterator($duration) as $key => $match) {
111  if (!is_null($match)) {
112  $res_array[] =
113  $match . ' ' .
114  ($match === '1' ?
115  $this->utilities->txt($labels[$key][1]) :
116  $this->utilities->txt($labels[$key][0]));
117  }
118  }
119  return implode(', ', $res_array);
120  }
121 
122  protected function fillSpaces(string $string): string
123  {
124  $string = str_replace(' ', '_', $string);
125  return strtolower($string);
126  }
127 
128  protected function camelCaseToSpaces(string $string): string
129  {
130  $string = preg_replace('/(?<=[a-z])(?=[A-Z])/', ' ', $string);
131  return strtolower($string);
132  }
133 }
__construct(UtilitiesInterface $utilities, DataHelperInterface $data_helper)
Definition: Data.php:32
$datetime
UtilitiesInterface $utilities
Definition: Data.php:29
$duration
language(string $language)
Definition: Data.php:88
camelCaseToSpaces(string $string)
Definition: Data.php:128
string $key
Consumer key/client ID value.
Definition: System.php:193
duration(string $duration)
Definition: Data.php:99
vocabularyValue(string $value)
Definition: Data.php:60
DataHelperInterface $data_helper
Definition: Data.php:30
dataValue(ElementsDataInterface $data)
Definition: Data.php:40
datetime(string $datetime)
Definition: Data.php:93
fillSpaces(string $string)
Definition: Data.php:122