1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 Formz project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Service\Traits; |
15
|
|
|
|
16
|
|
|
use TYPO3\CMS\Core\Utility\ArrayUtility; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This trait can be used by any class which needs usage of data which must be |
20
|
|
|
* accessible by external classes. |
21
|
|
|
* |
22
|
|
|
* See description of the functions `setData()` and `getData()` for more |
23
|
|
|
* information. |
24
|
|
|
* |
25
|
|
|
* @internal This trait is for Formz internal usage only: it may change at any moment, so do not use it in your own scripts! |
26
|
|
|
*/ |
27
|
|
|
trait StoreDataTrait |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $internalData = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns the asked data. If `$key` is null, the full data array is |
37
|
|
|
* returned. The argument `$key` can be a path to a value in the array, with |
38
|
|
|
* a dot (`.`) being the keys separator. |
39
|
|
|
* |
40
|
|
|
* Example: `$key = foo.bar` - the result will be the value stored in |
41
|
|
|
* `$data['foo']['bar']` (if it is found). |
42
|
|
|
* |
43
|
|
|
* @param string|null $key |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function getData($key = null) |
47
|
|
|
{ |
48
|
|
|
$result = null; |
49
|
|
|
|
50
|
|
|
if (null === $key) { |
51
|
|
|
$result = $this->internalData; |
52
|
|
|
} else { |
53
|
|
|
if (false !== strpos($key, '.')) { |
54
|
|
|
if (ArrayUtility::isValidPath($this->internalData, $key, '.')) { |
55
|
|
|
$result = ArrayUtility::getValueByPath($this->internalData, $key, '.'); |
56
|
|
|
} |
57
|
|
|
} elseif (isset($this->internalData[$key])) { |
58
|
|
|
$result = $this->internalData[$key]; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $result; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Stores the given value at the given key. The argument `$key` can be a |
67
|
|
|
* path to a value in an array, with a dot (`.`) being the keys separator. |
68
|
|
|
* |
69
|
|
|
* Example: `$key = foo.bar` - the value will be stored in |
70
|
|
|
* `$data['foo']['bar']` |
71
|
|
|
* |
72
|
|
|
* @param string $key |
73
|
|
|
* @param mixed $value |
74
|
|
|
*/ |
75
|
|
|
public function setData($key, $value) |
76
|
|
|
{ |
77
|
|
|
if (false !== strpos($key, '.')) { |
78
|
|
|
$this->internalData = ArrayUtility::setValueByPath($this->internalData, $key, $value, '.'); |
79
|
|
|
} else { |
80
|
|
|
$this->internalData[$key] = $value; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|