ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
SplitString.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
9
14{
18 protected $delimiter;
19
23 private $factory;
24
30 {
31 $this->delimiter = $delimiter;
32 $this->factory = $factory;
33 }
34
38 public function transform($from)
39 {
40 if (!is_string($from)) {
41 throw new \InvalidArgumentException(__METHOD__ . " the argument is not a string.");
42 }
43
44 return explode($this->delimiter, $from);
45 }
46
50 public function __invoke($from)
51 {
52 return $this->transform($from);
53 }
54
58 public function applyTo(Result $data) : Result
59 {
60 $dataValue = $data->value();
61 if (false === is_string($dataValue)) {
62 $exception = new \InvalidArgumentException(__METHOD__ . " the argument is not a string.");
63 return $this->factory->error($exception);
64 }
65
66 $value = explode($this->delimiter, $dataValue);
67 return $this->factory->ok($value);
68 }
69}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
Split a string by delimiter into array.
Definition: SplitString.php:14
__invoke($from)
Transformations should be callable.This MUST do the same as transform.InvalidArgumentException if the...
Definition: SplitString.php:50
__construct($delimiter, Factory $factory)
Definition: SplitString.php:29
applyTo(Result $data)
Perform the transformation and reify possible failures.If $data->isError(), the method MUST do nothin...
Definition: SplitString.php:58
transform($from)
Perform the transformation.Please use this for transformations. It's more performant than calling inv...
Definition: SplitString.php:38
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