XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
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 
11 //@require 'Swift/KeyCache.php';
12 //@require 'Swift/KeyCacheInputStream.php';
13 //@require 'Swift/InputByteStream.php';
14 //@require 'Swift/OutputByteStrean.php';
15 //@require 'Swift/SwiftException.php';
16 
24 {
25 
31  private $_contents = array();
32 
38  private $_stream;
39 
46  {
47  $this->_stream = $stream;
48  }
49 
58  public function setString($nsKey, $itemKey, $string, $mode)
59  {
60  $this->_prepareCache($nsKey);
61  switch ($mode)
62  {
63  case self::MODE_WRITE:
64  $this->_contents[$nsKey][$itemKey] = $string;
65  break;
66  case self::MODE_APPEND:
67  if (!$this->hasKey($nsKey, $itemKey))
68  {
69  $this->_contents[$nsKey][$itemKey] = '';
70  }
71  $this->_contents[$nsKey][$itemKey] .= $string;
72  break;
73  default:
74  throw new Swift_SwiftException(
75  'Invalid mode [' . $mode . '] used to set nsKey='.
76  $nsKey . ', itemKey=' . $itemKey
77  );
78  }
79  }
80 
89  public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os,
90  $mode)
91  {
92  $this->_prepareCache($nsKey);
93  switch ($mode)
94  {
95  case self::MODE_WRITE:
96  $this->clearKey($nsKey, $itemKey);
97  case self::MODE_APPEND:
98  if (!$this->hasKey($nsKey, $itemKey))
99  {
100  $this->_contents[$nsKey][$itemKey] = '';
101  }
102  while (false !== $bytes = $os->read(8192))
103  {
104  $this->_contents[$nsKey][$itemKey] .= $bytes;
105  }
106  break;
107  default:
108  throw new Swift_SwiftException(
109  'Invalid mode [' . $mode . '] used to set nsKey='.
110  $nsKey . ', itemKey=' . $itemKey
111  );
112  }
113  }
114 
122  public function getInputByteStream($nsKey, $itemKey,
123  Swift_InputByteStream $writeThrough = null)
124  {
125  $is = clone $this->_stream;
126  $is->setKeyCache($this);
127  $is->setNsKey($nsKey);
128  $is->setItemKey($itemKey);
129  if (isset($writeThrough))
130  {
131  $is->setWriteThroughStream($writeThrough);
132  }
133  return $is;
134  }
135 
142  public function getString($nsKey, $itemKey)
143  {
144  $this->_prepareCache($nsKey);
145  if ($this->hasKey($nsKey, $itemKey))
146  {
147  return $this->_contents[$nsKey][$itemKey];
148  }
149  }
150 
157  public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
158  {
159  $this->_prepareCache($nsKey);
160  $is->write($this->getString($nsKey, $itemKey));
161  }
162 
169  public function hasKey($nsKey, $itemKey)
170  {
171  $this->_prepareCache($nsKey);
172  return array_key_exists($itemKey, $this->_contents[$nsKey]);
173  }
174 
180  public function clearKey($nsKey, $itemKey)
181  {
182  unset($this->_contents[$nsKey][$itemKey]);
183  }
184 
189  public function clearAll($nsKey)
190  {
191  unset($this->_contents[$nsKey]);
192  }
193 
194  // -- Private methods
195 
201  private function _prepareCache($nsKey)
202  {
203  if (!array_key_exists($nsKey, $this->_contents))
204  {
205  $this->_contents[$nsKey] = array();
206  }
207  }
208 
209 }