ILIAS  release_8 Revision v8.24
SplitString.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
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}
Builds data types.
Definition: Factory.php:21
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:15
value()
Get the encapsulated value.
A transformation is a function from one datatype to another.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: CaseOfLabel.php:21