ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
AddLabels.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
28 
32 class AddLabels implements Transformation
33 {
35 
37  private array $labels;
38  private Factory $factory;
39 
44  public function __construct(array $labels, Factory $factory)
45  {
46  $this->labels = $labels;
47  $this->factory = $factory;
48  }
49 
54  public function transform($from): array
55  {
56  if (!is_array($from)) {
57  throw new InvalidArgumentException(__METHOD__ . " argument is not an array.");
58  }
59 
60  if (count($from) !== count($this->labels)) {
61  throw new InvalidArgumentException(__METHOD__ . " number of items in arrays are not equal.");
62  }
63 
64  return array_combine($this->labels, $from);
65  }
66 
70  public function applyTo(Result $result): Result
71  {
72  $dataValue = $result->value();
73  if (false === is_array($dataValue)) {
74  $exception = new InvalidArgumentException(__METHOD__ . " argument is not an array.");
75  return $this->factory->error($exception);
76  }
77 
78  if (count($dataValue) !== count($this->labels)) {
79  $exception = new InvalidArgumentException(__METHOD__ . " number of items in arrays are not equal.");
80  return $this->factory->error($exception);
81  }
82 
83  $value = array_combine($this->labels, $dataValue);
84  $result = $this->factory->ok($value);
85 
86  return $result;
87  }
88 }
value()
Get the encapsulated value.
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:14
Adds to any array keys for each value.
Definition: AddLabels.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: AddLabels.php:21
A transformation is a function from one datatype to another.
__construct(array $labels, Factory $factory)
Definition: AddLabels.php:44