ILIAS  release_7 Revision v7.30-3-g800a261c036
AddLabels.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 2017 Stefan Hecken <stefan.hecken@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3
5
10
14class AddLabels implements Transformation
15{
17
21 protected $labels;
22
26 private $factory;
27
32 public function __construct(array $labels, Factory $factory)
33 {
34 $this->labels = $labels;
35 $this->factory = $factory;
36 }
37
41 public function transform($from)
42 {
43 if (!is_array($from)) {
44 throw new \InvalidArgumentException(__METHOD__ . " argument is not an array.");
45 }
46
47 if (count($from) != count($this->labels)) {
48 throw new \InvalidArgumentException(__METHOD__ . " number of items in arrays are not equal.");
49 }
50
51 return array_combine($this->labels, $from);
52 }
53
57 public function applyTo(Result $data) : Result
58 {
59 $dataValue = $data->value();
60 if (false === is_array($dataValue)) {
61 $exception = new \InvalidArgumentException(__METHOD__ . " argument is not an array.");
62 return $this->factory->error($exception);
63 }
64
65 if (count($dataValue) != count($this->labels)) {
66 $exception = new \InvalidArgumentException(__METHOD__ . " number of items in arrays are not equal.");
67 return $this->factory->error($exception);
68 }
69
70 $value = array_combine($this->labels, $dataValue);
71 $result = $this->factory->ok($value);
72
73 return $result;
74 }
75}
$result
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
Adds to any array keys for each value.
Definition: AddLabels.php:15
applyTo(Result $data)
Perform the transformation and reify possible failures.If $data->isError(), the method MUST do nothin...
Definition: AddLabels.php:57
transform($from)
Perform the transformation.Please use this for transformations. It's more performant than calling inv...
Definition: AddLabels.php:41
__construct(array $labels, Factory $factory)
Definition: AddLabels.php:32
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:12
A transformation is a function from one datatype to another.
$data
Definition: storeScorm.php:23