1: <?php
2:
3: namespace intouch\ical;
4:
5: use \ArrayAccess;
6: use \Countable;
7: use \IteratorAggregate;
8:
9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class Line implements ArrayAccess, Countable, IteratorAggregate {
24: protected $ident;
25: protected $data;
26: protected $params = array();
27:
28: protected $replacements = array('from'=>array('\\,', '\\n', '\\;', '\\:', '\\"'), 'to'=>array(',', "\n", ';', ':', '"'));
29:
30: 31: 32:
33: public function __construct( $line ) {
34: $split = strpos($line, ':');
35: $idents = explode(';', substr($line, 0, $split));
36: $ident = strtolower(array_shift($idents));
37:
38: $data = trim(substr($line, $split+1));
39: $data = str_replace($this->replacements['from'], $this->replacements['to'], $data);
40:
41: $params = array();
42: foreach( $idents AS $v) {
43: list($k, $v) = explode('=', $v);
44: $params[ strtolower($k) ] = $v;
45: }
46:
47: $this->ident = $ident;
48: $this->params = $params;
49: $this->data = $data;
50: }
51:
52: 53: 54: 55:
56: public function isBegin() {
57: return $this->ident == 'begin';
58: }
59:
60: 61: 62: 63:
64: public function isEnd() {
65: return $this->ident == 'end';
66: }
67:
68: 69: 70: 71:
72: public function getIdent() {
73: return $this->ident;
74: }
75:
76: 77: 78: 79:
80: public function getData() {
81: return $this->data;
82: }
83:
84: 85: 86: 87:
88: public function getDataAsArray() {
89: if (strpos($this->data,",") !== false) {
90: return explode(",",$this->data);
91: }
92: else
93: return array($this->data);
94: }
95:
96: 97: 98: 99: 100: 101: 102:
103: public static function Remove_Line($arr) {
104: $rtn = array();
105: foreach( $arr AS $k => $v ) {
106: if(is_array($v)) {
107: $rtn[$k] = self::Remove_Line($v);
108: } elseif( $v instanceof Line ) {
109: $rtn[$k] = $v->getData();
110: } else {
111: $rtn[$k] = $v;
112: }
113: }
114: return $rtn;
115: }
116:
117: 118: 119:
120: public function offsetExists( $param ) {
121: return isset($this->params[ strtolower($param) ]);
122: }
123:
124: 125: 126:
127: public function offsetGet( $param ) {
128: $index = strtolower($param);
129: if (isset($this->params[ $index ])) {
130: return $this->params[ $index ];
131: }
132: }
133:
134: 135: 136: 137:
138: public function offsetSet( $param, $val ) {
139: return false;
140: }
141:
142: 143: 144: 145:
146: public function offsetUnset( $param ) {
147: return false;
148: }
149:
150: 151: 152: 153:
154: public function __toString() {
155: return $this->getData();
156: }
157:
158: 159: 160:
161: public function count() {
162: return count($this->params);
163: }
164:
165: 166: 167:
168: public function getIterator() {
169: return new ArrayIterator($this->params);
170: }
171: }
172: