rlanvin / php-rrule

Lightweight and fast recurring dates library for PHP (RFC 5545)
Other
606 stars 83 forks source link

Error when parsing rrule strings from a database. #31

Closed orlando-paredes-hamsho closed 7 years ago

orlando-paredes-hamsho commented 7 years ago

I find my rrule strings losing formatting upon storing and retrieving them from my db.

In my code I was able to get around this by doing a string replace for space against "\n" right before sending them into the constructor for an RRule.

Just thought this repository could benefit from adding that little check for proper formatting.

rlanvin commented 7 years ago

Could you be a bit more specific, provide examples of the string before and after the DB and what error you get from the lib? I'm sorry but I don't understand your bug report at the moment.

orlando-paredes-hamsho commented 7 years ago

Of course!

Let's assume I have an Rule String... let's use the one on the documentation:

DTSTART;TZID=America/New_York:19970901T090000 RRULE:FREQ=DAILY;UNTIL=19971224T000000Z;WKST=SU;BYDAY=MO,WE,FR;BYMONTH=1

Which I then proceed to store into my database, through code or even just copy paste, into a Wordpress or ProcessWire field; and why would I do that? Cause I want to store it, of course, they're pretty low weight for their power ;).

Let's also assume that I then try to retrieve it from the database and get [What appears to be] the same string:

$rrule_string = DTSTART;TZID=America/New_York:19970901T090000 RRULE:FREQ=DAILY;UNTIL=19971224T000000Z;WKST=SU;BYDAY=MO,WE,FR;BYMONTH=1

Which I decide to use to instantiate an RRule: new RRule($rrule_string).

After following these steps, i'll be greeted with an error saying: 'Failed to parse RFC string, invalid property parameters: "TZID=America/New_York:19970901T090000RRULE..."'

Which surprised me, because it looked like my string was right. But upon closer inspection I could see that it was trying to convert certain lines together, yet I wasn't sure of why

I already dragged this on for too long (sorry) so, after much research I was able to find this line of code on the parseRfcString function: foreach ( explode("\n", $string) as $line ) { meaning that, even thought I believed my string to be properly formatted, it really wasn't.

The format was expecting new lines instead of spaces. Which may be me having not read enough of either documentation to notice that the proper format required them, but it looked like a space to me.

Anyway, the fix (for me) was really as simple as adding this line of code previous to the for each loop:

if(is_string($config)) { //If the string comes from the db it may have changed the newline to a space $config = str_replace(" ", "\n", $config); }

I'm sorry I wasn't able to properly explain myself before, and I really hope this helps! It's a wonderful library that I really love using, and I specially appreciate your effort in making it :)

rlanvin commented 7 years ago

Ok thanks, this is much more detailed now. It looks to me that the problem comes from the way you store and/or retrieve it. Does Wordpress or Processwire convert \n to spaces maybe?

I won't change the parser, the RFC states that DTSTART and RRULE must be on 2 different lines. A space is not RFC-compliant.

orlando-paredes-hamsho commented 7 years ago

Most CMS' do some weird mumbo jumbo when storing strings, I guess it's just the way that it is.

I won't change the parser, the RFC states that DTSTART and RRULE must be on 2 different lines. A space is not RFC-compliant.

Seems about fair, I know there's an official documentation, but I wouldn't mind adding the specifics of an RFC string, or a link to them in the README.MD or wherever you feel would be appropriate. Just to help out anybody who runs into the same problem as me.

Other than that tho, thank you for your time and attention in hearing me out man, I guess this issue's closed ;)

rlanvin commented 7 years ago

Done, wiki has been updated.