Need access to central object throughout tree

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Need access to central object throughout tree

Post by Luke »

I am back at work on my iCalendar library. One requirement in the format is that if a property specifies a "TZID" parameter, there must be a corresponding VTIMEZONE component in the iCalendar object somewhere. My iCalendar object is a tree. It has nested components, each with properties and parameters (meta-info about properties). Picture something like:

Code: Select all

<?php
// an event that takes place in the park from 10-11am on August 15th, 2009 (US-Eastern time)
$event = new qCal_Component_Vevent(array(
    new qCal_Property_Dtstart('20090815T100000', array('tzid' => 'US-Eastern')),
    new qCal_Property_Dtend('20090815T110000'), array('tzid' => 'US-Eastern')),
    'summary' => 'A picnic',
    'description' => 'A picnic in the park',
    'location' => 'The Park'
));
 
This component will eventually be attached to a qCal_Component_Vcalendar object and be a part of the component tree:

Code: Select all

$cal = new qCal_Component_Vcalendar(array(
    'prodid' => '-//Some Product That Makes iCalendar Files//somehost@host.com',
    'method' => 'PUBLISH'
));
$cal->attach($event);
In order for the event to know how to handle "US-Eastern" time, there must be a qCal_Component_Vtimezone component in the tree somewhere.

Code: Select all

$timezone = new qCal_Component_Vtimezone(array(
    'tzid' => 'US-Eastern',
    // timezone properties here...
));
$cal->attach($timezone);
Components can be nested within other components. For instance, let's add an alarm to our event:

Code: Select all

$alarm = new qCal_Component_Valarm(array(
    new qCal_Property_Trigger('20090815T100000', array('tzid' => 'US-Eastern')),
    'action' => 'audio',
    'attach' => 'http://www.example.com/sounds/alarm.wav',
));
$event->attach($alarm);
My question is, how do I go about ensuring that all tzids specified throughout the tree have associated vtimezone components? It seems that I need some kind of central object that stores this kind of data. What do you guys think?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Need access to central object throughout tree

Post by Luke »

Would it be a bad idea to use something like sqlite with this library as sort of an intermediary storage device? Sometimes icalendar files can be pretty large, and making queries on them would be much faster and easier if I stored them in a database, wouldn't it? I'm really on the fence about it, but it does seem like it would make things easier, no?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Need access to central object throughout tree

Post by Luke »

One of the "appropriate uses for sqlite" listed on the sqlite docs is:
Internal or temporary databases

For programs that have a lot of data that must be sifted and sorted in diverse ways, it is often easier and quicker to load the data into an in-memory SQLite database and use queries with joins and ORDER BY clauses to extract the data in the form and order needed rather than to try to code the same operations manually. Using an SQL database internally in this way also gives the program greater flexibility since new columns and indices can be added without having to recode every query.
Seems like exactly what I need to do...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Need access to central object throughout tree

Post by Christopher »

Luke wrote:My question is, how do I go about ensuring that all tzids specified throughout the tree have associated vtimezone components? It seems that I need some kind of central object that stores this kind of data. What do you guys think?
It seems like qCal_Component_Vcalendar::attach() needs to register some way for these sub-compenents to get the timezone object when they need it, or report that it is not available.

SQLite is excellent for fast read/write where you want a SQL interface. It will limit you to a single server though.
(#10850)
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: Need access to central object throughout tree

Post by arjan.top »

why not something like this?

Code: Select all

 
new qCal_Property_Trigger('20090815T100000', array('tzid' => $timezone))
 
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Need access to central object throughout tree

Post by Luke »

arborint wrote:
Luke wrote:My question is, how do I go about ensuring that all tzids specified throughout the tree have associated vtimezone components? It seems that I need some kind of central object that stores this kind of data. What do you guys think?
It seems like qCal_Component_Vcalendar::attach() needs to register some way for these sub-compenents to get the timezone object when they need it, or report that it is not available.

SQLite is excellent for fast read/write where you want a SQL interface. It will limit you to a single server though.
I agree, I am just not sure how I should go about it. For now, I suppose, I can use a registry, but I really am not happy with that. I may as well just throw everything into $GLOBALS if I'm going to use a registry for this. It's a hack. Ideally, each component would just be aware of its root object and use that to find all of the available time zone objects. Any ideas?

For now I guess I'll just use a registry and store references to everything in the tree so I can easily find them.

@arjan.top

I don't see how that solves my problem.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Need access to central object throughout tree

Post by alex.barylski »

My question is, how do I go about ensuring that all tzids specified throughout the tree have associated vtimezone components? It seems that I need some kind of central object that stores this kind of data. What do you guys think?
I am not sure I fully understand your problem but it seems to me, that a validator object might simply traverse the 'tree' object and using RTTI check to ensure that associated objects exist?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Need access to central object throughout tree

Post by Luke »

Can you elaborate?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Need access to central object throughout tree

Post by alex.barylski »

Well it sounds like the structure you have is a tree that stores timezones, each node in the tree could be checked by a validator to ensure nodes which had object of type 'TimeZone' had a relevant 'TZID' or whatever the objects are.

RTTI = Runtime type information

Assume an array like

Code: Select all

array
(
  array
  (
    'tzid' => TZID_Object
    'timezone' => 'Chicago/America'
 
  )
)
I'm not sure what your tree structure looks like or what it is your even doing so pardon my attempt...

Basically the validator would traverse the tree assuming it's a series of nested arrays starting at root node, you step through each array and check if leaf nodes have required objects...
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Need access to central object throughout tree

Post by Luke »

Oh ok. Yea, that's basically what I've done. It now looks through the tree any time a tzid is attached and makes sure that there is a corresponding vtimezone component. Thanks man.

I still am not 100% satisfied with how it works, but it will at least allow me to go on with my beta release so I can start getting some feedback.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Need access to central object throughout tree

Post by alex.barylski »

I still am not 100% satisfied with how it works, but it will at least allow me to go on with my beta release so I can start getting some feedback.
What are you not satisfied with?

Why are you using a tree and not a linear vector/array?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Need access to central object throughout tree

Post by Luke »

Meh, the library is just a little more messy that I'd like. That's OK though, it will get cleaner and cleaner with every release. As for why it's in a tree, it's because it is hierarchal data. Components can be nested within eachother, so a tree is what makes sense.
Post Reply