ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
SplitString.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Refinery\String;
22
27use InvalidArgumentException;
28
33{
35
36 private string $delimiter;
38
39 public function __construct(string $delimiter, Factory $factory)
40 {
41 $this->delimiter = $delimiter;
42 $this->factory = $factory;
43 }
44
49 public function transform($from): array
50 {
51 if (!is_string($from)) {
52 throw new InvalidArgumentException(__METHOD__ . " the argument is not a string.");
53 }
54
55 return explode($this->delimiter, $from);
56 }
57
61 public function applyTo(Result $result): Result
62 {
63 $dataValue = $result->value();
64 if (false === is_string($dataValue)) {
65 $exception = new InvalidArgumentException(__METHOD__ . " the argument is not a string.");
66 return $this->factory->error($exception);
67 }
68
69 $value = explode($this->delimiter, $dataValue);
70 return $this->factory->ok($value);
71 }
72}
factory()
Builds data types.
Definition: Factory.php:36
Split a string by delimiter into array.
Definition: SplitString.php:33
__construct(string $delimiter, Factory $factory)
Definition: SplitString.php:39
applyTo(Result $result)
@inheritDoc
Definition: SplitString.php:61
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29
value()
Get the encapsulated value.
A transformation is a function from one datatype to another.