TYPO3 CMS  TYPO3_8-7
FinisherVariableProvider.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
27 final class FinisherVariableProvider implements \ArrayAccess
28 {
29 
36  protected $objects = [];
37 
46  public function add(string $finisherIdentifier, string $key, $value)
47  {
48  $this->addOrUpdate($finisherIdentifier, $key, $value);
49  }
50 
59  public function addOrUpdate(string $finisherIdentifier, string $key, $value)
60  {
61  if (!array_key_exists($finisherIdentifier, $this->objects)) {
62  $this->objects[$finisherIdentifier] = [];
63  }
64  $this->objects[$finisherIdentifier] = ArrayUtility::setValueByPath(
65  $this->objects[$finisherIdentifier],
66  $key,
67  $value,
68  '.'
69  );
70  }
71 
81  public function get(string $finisherIdentifier, string $key, $default = null)
82  {
83  if ($this->exists($finisherIdentifier, $key)) {
84  return ArrayUtility::getValueByPath($this->objects[$finisherIdentifier], $key, '.');
85  }
86  return $default;
87  }
88 
97  public function exists($finisherIdentifier, $key): bool
98  {
99  try {
100  ArrayUtility::getValueByPath($this->objects[$finisherIdentifier], $key, '.');
101  } catch (\RuntimeException $e) {
102  return false;
103  }
104  return true;
105  }
106 
114  public function remove(string $finisherIdentifier, string $key)
115  {
116  if ($this->exists($finisherIdentifier, $key)) {
117  $this->objects[$finisherIdentifier] = ArrayUtility::removeByPath(
118  $this->objects[$finisherIdentifier],
119  $key,
120  '.'
121  );
122  }
123  }
124 
130  public function __sleep()
131  {
132  return ['objects'];
133  }
134 
142  public function offsetExists($offset)
143  {
144  return isset($this->objects[$offset]);
145  }
146 
154  public function offsetGet($offset)
155  {
156  return $this->objects[$offset];
157  }
158 
166  public function offsetSet($offset, $value)
167  {
168  $this->objects[$offset] = $value;
169  }
170 
177  public function offsetUnset($offset)
178  {
179  unset($this->objects[$offset]);
180  }
181 }
static setValueByPath(array $array, $path, $value, $delimiter='/')
add(string $finisherIdentifier, string $key, $value)
static getValueByPath(array $array, $path, $delimiter='/')
addOrUpdate(string $finisherIdentifier, string $key, $value)
static removeByPath(array $array, $path, $delimiter='/')