/** * Transform the supplied data. * * This method may transform only part of the supplied data. The return * value includes information about how much data was actually consumed. The * transform can be forced to consume all data by passing a boolean true as * the $isEnd argument. * * The $context argument will initially be null, but any value assigned to * this variable will persist until the stream transformation is complete. * It can be used as a place to store state, such as a buffer. * * It is guaranteed that this method will be called with $isEnd = true once, * and only once, at the end of the stream transformation. * * @param string $data The data to transform. * @param mixed &$context An arbitrary context value. * @param boolean $isEnd True if all supplied data must be transformed. * * @return tuple<string,integer,mixed> A 3-tuple of the transformed data, the number of bytes consumed, and any resulting error. */ public function transform($data, &$context, $isEnd = false) { if ($isEnd) { list($output, $consumed, $error) = parent::transform(preg_replace('{[^[:alnum:]+/=]+}', '', $data), $context, true); if (null !== $error) { return array('', 0, new InvalidEncodedDataException('base64mime', $error->data())); } return array($output, strlen($data), $error); } $chunks = preg_split('{([^[:alnum:]+/=]+)}', $data, -1, PREG_SPLIT_OFFSET_CAPTURE); $numChunks = count($chunks); $buffer = ''; $output = ''; $lastFullyConsumed = -1; $consumed = 0; for ($i = 0; $i < $numChunks; $i++) { $buffer .= $chunks[$i][0]; list($thisOutput, $consumed, $error) = parent::transform($buffer, $context); $output .= $thisOutput; if ($consumed === strlen($buffer)) { $buffer = ''; $lastFullyConsumed = $i; } else { $buffer = substr($buffer, $consumed); } } if ($lastFullyConsumed > -1) { if ($lastFullyConsumed < $numChunks - 1) { $consumed = $chunks[$lastFullyConsumed + 1][1]; } else { $consumed = $chunks[$lastFullyConsumed][1] + strlen($chunks[$lastFullyConsumed][0]); } } return array($output, $consumed, null); }
/** * Construct a new base64 codec. * * @param TransformInterface|null $encodeTransform The encode transform to use. * @param TransformInterface|null $decodeTransform The decode transform to use. */ public function __construct(TransformInterface $encodeTransform = null, TransformInterface $decodeTransform = null) { if (null === $encodeTransform) { $encodeTransform = Base64EncodeTransform::instance(); } if (null === $decodeTransform) { $decodeTransform = Base64DecodeTransform::instance(); } parent::__construct($encodeTransform, $decodeTransform); }
/** * Create the transform. * * @return TransformInterface The data transform. */ protected function createTransform() { return Base64DecodeTransform::instance(); }