Specifying filter order

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
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Specifying filter order

Post by Ambush Commander »

Hello folks. I have a number of filters that need to be run on text. Sometimes order is important, sometimes it isn't. So, I need some way for users to arbitrarily order the filters. Some possibilities:

1. Assign each filter an integer weight, when the filter is added you stuff it in an array in an array, with the integer as the index

2. Assign each filter a float weight, the use array_multisort on a temporary array of all the filter's weights in order to get things in order

3. Require all filters to be specified at once, removing the need for weights

4. ???

What would you do?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I think the most understandable and simple is simply an integer weight/priority. At some point it comes down to order of addition, but for the most part if they are grouped correctly, that part is moot.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Depending on your needs it might be enough to have add() that appends and another method to add to the front of the list. That often handles enough other cases to be sufficient. If the filters are named/grouped/typed then you can also have insertBefore() and insertAfter(), which I find more specific than weights.
(#10850)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Hello folks. I have a number of filters that need to be run on text. Sometimes order is important, sometimes it isn't. So, I need some way for users to arbitrarily order the filters. Some possibilities:
Priority seems the simplest - is it possible to allow filters check their own priority in comparison to others? Depending on the filter, it might need to run before, but never after another to ensure the text remains readable. Having rules for such filters would make it more difficult to accidentally put them in the wrong order. insertBefore() and insertAfter() as suggested by arborint would be a cool way of applying such rules.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

While weights are the most common implementation, I think I'll test drive arborint's idea and see how well it works for me.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yeah I'd recommend going with arborint's idea. In fact I've done it in the past.
Possibly make sure you write in a way that will allow you to stick the weight idea on top as another layer or anything else for that matter.
Post Reply