ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ByTrying.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 namespace ILIAS\Refinery;
22 
23 use Exception;
24 use ILIAS\Data;
25 
26 class ByTrying implements Transformation
27 {
30  use ProblemBuilder;
31 
33  private array $transformations;
36  private $error;
37 
42  public function __construct(array $transformations, Data\Factory $data_factory)
43  {
44  $this->transformations = $transformations;
45  $this->data_factory = $data_factory;
46  $this->error = static function (): void {
48  'no valid constraints',
49  'no_valid_constraints'
50  );
51  };
52  }
53 
57  protected function getError(): callable
58  {
59  return $this->error;
60  }
61 
65  public function transform($from)
66  {
67  foreach ($this->transformations as $transformation) {
68  $result = $this->data_factory->ok($from);
69  $result = $transformation->applyTo($result);
70  if ($result->isOK()) {
71  return $result->value();
72  }
73  }
74  throw new Exception($this->getErrorMessage($from));
75  }
76 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: ByTrying.php:21
Data Factory $data_factory
Definition: ByTrying.php:34
__construct(array $transformations, Data\Factory $data_factory)
Definition: ByTrying.php:42
Builds data types.
Definition: Factory.php:20
getErrorMessage($value)
Get the problem message.
A transformation is a function from one datatype to another.