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: /**
12: * Provides fixed-width byte sizes for reading fixed-width character sets.
13: * @package Swift
14: * @subpackage Encoder
15: * @author Chris Corbyn
16: * @author Xavier De Cock <xdecock@gmail.com>
17: */
18: class Swift_CharacterReader_GenericFixedWidthReader implements Swift_CharacterReader
19: {
20: /**
21: * The number of bytes in a single character.
22: * @var int
23: * @access private
24: */
25: private $_width;
26:
27: /**
28: * Creates a new GenericFixedWidthReader using $width bytes per character.
29: * @param int $width
30: */
31: public function __construct($width)
32: {
33: $this->_width = $width;
34: }
35:
36: /**
37: * Returns the complete charactermap
38: *
39: * @param string $string
40: * @param int $startOffset
41: * @param array $currentMap
42: * @param mixed $ignoredChars
43: * @return $int
44: */
45: public function getCharPositions($string, $startOffset, &$currentMap, &$ignoredChars)
46: {
47: $strlen = strlen($string);
48: // % and / are CPU intensive, so, maybe find a better way
49: $ignored = $strlen%$this->_width;
50: $ignoredChars = substr($string, - $ignored);
51: $currentMap = $this->_width;
52:
53: return ($strlen - $ignored)/$this->_width;
54: }
55:
56: /**
57: * Returns mapType
58: * @return int mapType
59: */
60: public function getMapType()
61: {
62: return self::MAP_TYPE_FIXED_LEN;
63: }
64:
65: /**
66: * Returns an integer which specifies how many more bytes to read.
67: * A positive integer indicates the number of more bytes to fetch before invoking
68: * this method again.
69: * A value of zero means this is already a valid character.
70: * A value of -1 means this cannot possibly be a valid character.
71: * @param string $bytes
72: * @return int
73: */
74: public function validateByteSequence($bytes, $size)
75: {
76: $needed = $this->_width - $size;
77:
78: return ($needed > -1) ? $needed : -1;
79: }
80:
81: /**
82: * Returns the number of bytes which should be read to start each character.
83: * @return int
84: */
85: public function getInitialByteSize()
86: {
87: return $this->_width;
88: }
89: }
90: