TYPO3  7.6
ArrayKeyCache.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
17 {
23  private $_contents = array();
24 
30  private $_stream;
31 
39  {
40  $this->_stream = $stream;
41  }
42 
53  public function setString($nsKey, $itemKey, $string, $mode)
54  {
55  $this->_prepareCache($nsKey);
56  switch ($mode) {
57  case self::MODE_WRITE:
58  $this->_contents[$nsKey][$itemKey] = $string;
59  break;
60  case self::MODE_APPEND:
61  if (!$this->hasKey($nsKey, $itemKey)) {
62  $this->_contents[$nsKey][$itemKey] = '';
63  }
64  $this->_contents[$nsKey][$itemKey] .= $string;
65  break;
66  default:
67  throw new Swift_SwiftException(
68  'Invalid mode ['.$mode.'] used to set nsKey='.
69  $nsKey.', itemKey='.$itemKey
70  );
71  }
72  }
73 
84  public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode)
85  {
86  $this->_prepareCache($nsKey);
87  switch ($mode) {
88  case self::MODE_WRITE:
89  $this->clearKey($nsKey, $itemKey);
90  case self::MODE_APPEND:
91  if (!$this->hasKey($nsKey, $itemKey)) {
92  $this->_contents[$nsKey][$itemKey] = '';
93  }
94  while (false !== $bytes = $os->read(8192)) {
95  $this->_contents[$nsKey][$itemKey] .= $bytes;
96  }
97  break;
98  default:
99  throw new Swift_SwiftException(
100  'Invalid mode ['.$mode.'] used to set nsKey='.
101  $nsKey.', itemKey='.$itemKey
102  );
103  }
104  }
105 
117  public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $writeThrough = null)
118  {
119  $is = clone $this->_stream;
120  $is->setKeyCache($this);
121  $is->setNsKey($nsKey);
122  $is->setItemKey($itemKey);
123  if (isset($writeThrough)) {
124  $is->setWriteThroughStream($writeThrough);
125  }
126 
127  return $is;
128  }
129 
138  public function getString($nsKey, $itemKey)
139  {
140  $this->_prepareCache($nsKey);
141  if ($this->hasKey($nsKey, $itemKey)) {
142  return $this->_contents[$nsKey][$itemKey];
143  }
144  }
145 
153  public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
154  {
155  $this->_prepareCache($nsKey);
156  $is->write($this->getString($nsKey, $itemKey));
157  }
158 
167  public function hasKey($nsKey, $itemKey)
168  {
169  $this->_prepareCache($nsKey);
170 
171  return array_key_exists($itemKey, $this->_contents[$nsKey]);
172  }
173 
180  public function clearKey($nsKey, $itemKey)
181  {
182  unset($this->_contents[$nsKey][$itemKey]);
183  }
184 
190  public function clearAll($nsKey)
191  {
192  unset($this->_contents[$nsKey]);
193  }
194 
200  private function _prepareCache($nsKey)
201  {
202  if (!array_key_exists($nsKey, $this->_contents)) {
203  $this->_contents[$nsKey] = array();
204  }
205  }
206 }