Mark V
1: <?php
2: namespace MOC\V\Component\Document\Vendor\UniversalXml\Source;
3:
4: /**
5: * Class Mask
6: *
7: * @package MOC\V\Component\Document\Vendor\UniversalXml\Source
8: */
9: abstract class Mask
10: {
11:
12: const PATTERN_COMMENT = '!(?<=\!--).*?(?=//-->)!is';
13: const PATTERN_CDATA = '!(?<=\!\[CDATA\[).*?(?=\]\]>)!is';
14:
15: /**
16: * @param string $Payload
17: * @param string $Pattern
18: *
19: * @return mixed
20: */
21: protected function encodePayload($Payload, $Pattern)
22: {
23:
24: if ($Pattern == self::PATTERN_COMMENT) {
25: return preg_replace($Pattern, '', $Payload);
26: } else {
27: return preg_replace_callback($Pattern, array($this, 'encodeMethod'), $Payload);
28: }
29: }
30:
31: /**
32: * @param Node $Node
33: * @param string $Pattern
34: */
35: protected function decodePayload(Node &$Node, $Pattern)
36: {
37:
38: $Match = array();
39: preg_match($Pattern, $Node->getContent(), $Match);
40: $Node->setContent($this->decodeMethod(empty( $Match ) ? '' : $Match[0]));
41: }
42:
43: /**
44: * @param $Payload
45: *
46: * @return string
47: */
48: private function decodeMethod($Payload)
49: {
50:
51: return base64_decode($Payload);
52: }
53:
54: /**
55: * @param $Payload
56: *
57: * @return string
58: */
59: private function encodeMethod($Payload)
60: {
61:
62: return base64_encode($Payload[0]);
63: }
64: }
65: