1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Validation\Rules\Converters; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Validation\Contracts\Errors\ErrorCodes; |
22
|
|
|
use Limoncello\Validation\Contracts\Execution\ContextInterface; |
23
|
|
|
use Limoncello\Validation\I18n\Messages; |
24
|
|
|
use Limoncello\Validation\Rules\ExecuteRule; |
25
|
|
|
use function assert; |
26
|
|
|
use function explode; |
27
|
|
|
use function is_string; |
28
|
|
|
use function strlen; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @package Limoncello\Validation |
32
|
|
|
*/ |
33
|
|
|
final class StringToArray extends ExecuteRule |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Property key. |
37
|
|
|
*/ |
38
|
|
|
const PROPERTY_DELIMITER = self::PROPERTY_LAST + 1; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Property key. |
42
|
|
|
*/ |
43
|
|
|
const PROPERTY_LIMIT = self::PROPERTY_DELIMITER + 1; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $delimiter |
47
|
|
|
* @param int $limit |
48
|
|
|
*/ |
49
|
1 |
|
public function __construct(string $delimiter, int $limit = PHP_INT_MAX) |
50
|
|
|
{ |
51
|
1 |
|
assert(strlen($delimiter) > 0); |
52
|
1 |
|
assert($limit >= 0); |
53
|
|
|
|
54
|
1 |
|
parent::__construct([ |
55
|
1 |
|
self::PROPERTY_DELIMITER => $delimiter, |
56
|
1 |
|
self::PROPERTY_LIMIT => $limit, |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param mixed $value |
62
|
|
|
* @param ContextInterface $context |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
* |
66
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
67
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
68
|
|
|
*/ |
69
|
1 |
|
public static function execute($value, ContextInterface $context): array |
70
|
|
|
{ |
71
|
1 |
|
$reply = null; |
72
|
|
|
|
73
|
1 |
|
$result = []; |
74
|
1 |
|
if (is_string($value) === true) { |
75
|
1 |
|
$properties = $context->getProperties(); |
76
|
1 |
|
$delimiter = $properties->getProperty(static::PROPERTY_DELIMITER); |
77
|
1 |
|
$limit = $properties->getProperty(static::PROPERTY_LIMIT); |
78
|
1 |
|
$result = explode($delimiter, $value, $limit); |
79
|
|
|
} else { |
80
|
1 |
|
$reply = static::createErrorReply($context, $value, ErrorCodes::IS_STRING, Messages::IS_STRING, []); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $reply !== null ? $reply : static::createSuccessReply($result); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|