ILIAS  release_7 Revision v7.30-3-g800a261c036
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 
10 
14 class SplitString implements Transformation
15 {
17 
21  protected $delimiter;
22 
26  private $factory;
27 
33  {
34  $this->delimiter = $delimiter;
35  $this->factory = $factory;
36  }
37 
41  public function transform($from)
42  {
43  if (!is_string($from)) {
44  throw new \InvalidArgumentException(__METHOD__ . " the argument is not a string.");
45  }
46 
47  return explode($this->delimiter, $from);
48  }
49 
53  public function applyTo(Result $data) : Result
54  {
55  $dataValue = $data->value();
56  if (false === is_string($dataValue)) {
57  $exception = new \InvalidArgumentException(__METHOD__ . " the argument is not a string.");
58  return $this->factory->error($exception);
59  }
60 
61  $value = explode($this->delimiter, $dataValue);
62  return $this->factory->ok($value);
63  }
64 }
value()
Get the encapsulated value.
__construct($delimiter, Factory $factory)
Definition: SplitString.php:32
applyTo(Result $data)
Perform the transformation and reify possible failures.If $data->isError(), the method MUST do nothin...
Definition: SplitString.php:53
$data
Definition: storeScorm.php:23
Split a string by delimiter into array.
Definition: SplitString.php:14
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:41
Builds data types.
Definition: Factory.php:19
A transformation is a function from one datatype to another.