ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
4 namespace ILIAS\Refinery\String;
5 
9 
13 class SplitString implements Transformation
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 }
value()
Get the encapsulated value.
__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
$data
Definition: storeScorm.php:23
Split a string by delimiter into array.
Definition: SplitString.php:13
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:11
transform($from)
Perform the transformation.Please use this for transformations. It&#39;s more performant than calling inv...
Definition: SplitString.php:38
__invoke($from)
Transformations should be callable.This MUST do the same as transform.
Definition: SplitString.php:50
Builds data types.
Definition: Factory.php:19
A transformation is a function from one datatype to another.