ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Elements.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 
30 class Elements implements ElementsInterface
31 {
32  protected const string SEPARATOR = ': ';
33 
35 
36  public function __construct(
37  UtilitiesInterface $utilities,
38  ) {
39  $this->utilities = $utilities;
40  }
41 
42  public function name(
43  BaseElementInterface $element,
44  bool $plural = false
45  ): string {
46  $name = $element->getDefinition()->name();
47  $exceptions = [
48  'metadataSchema' => 'metadatascheme', 'lifeCycle' => 'lifecycle',
49  'otherPlatformRequirements' => 'otherPlattformRequirements'
50  ];
51  $name = $exceptions[$name] ?? $name;
52 
53  $lang_key = 'meta_' . $this->camelCaseToSnakeCase($name);
54  if ($plural) {
55  $lang_key .= '_plural';
56  }
57  return $this->utilities->txt($lang_key);
58  }
59 
60  public function nameWithParents(
61  BaseElementInterface $element,
62  ?BaseElementInterface $cut_off = null,
63  bool $plural = false,
64  bool $skip_initial = false
65  ): string {
66  $names = [];
67  $el = $element;
68 
69  $initial = true;
70  while (!$el->isRoot()) {
71  if ($el === $cut_off) {
72  break;
73  }
74  if ($initial && $skip_initial) {
75  $el = $el->getSuperElement();
76  $initial = false;
77  continue;
78  }
79  array_unshift($names, $this->name($el, $initial && $plural));
80  $el = $el->getSuperElement();
81  $initial = false;
82  }
83  if (empty($names)) {
84  return $this->name($element, $plural);
85  }
86  return implode(self::SEPARATOR, $names);
87  }
88 
89  protected function camelCaseToSnakeCase(string $string): string
90  {
91  $string = preg_replace('/(?<=[a-z])(?=[A-Z])/', '_', $string);
92  return strtolower($string);
93  }
94 }
camelCaseToSnakeCase(string $string)
Definition: Elements.php:89
name(BaseElementInterface $element, bool $plural=false)
Definition: Elements.php:42
__construct(UtilitiesInterface $utilities,)
Definition: Elements.php:36
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
nameWithParents(BaseElementInterface $element, ?BaseElementInterface $cut_off=null, bool $plural=false, bool $skip_initial=false)
Definition: Elements.php:60
getDefinition()
Defining properties of the metadata element.