In this episode of “What does that do?” we’re going to look at a few helpers and methods of (probably) well known classes that may or may not be that well known, as well as enlighten you to a few gotchas that I’ve discovered during my Cake usage.
First is the Set::extract method. When I first started using Cake, I was using the dot notation in my extract and combine calls, that is until I discovered the XPath notation that was also allowed which offers more control over your returned values. One gotcha that I discovered early on, was that to emulate the dot notation {n}.Foo, you can’t just use /Foo, you have to add a /. to the end to make it work properly. So you end up with /Foo/.. One other thing I noticed while poking around in the source, was that the argument order for extract is opposite from that of combine. With combine, you put $data, then $path[12]. With extract, you put in $path first, then $data. I was doing it wrong for a long time, but the Cake developers thought of this, and silently flip them in extract if you get them backwards.
Another method in Set that I never knew about, is enum. With enum, you give it a value, and then a list to select from. The cool thing is, if you don’t give it a select list, it defaults to yes/no. I have used this many times since I discovered it. I often use an active field in my models, and when outputting to my index pages, I would like to see Yes/No, instead of 1/0. So I just wrap the output in Set::enum($value), and it’s good to go.
Set has many methods that are really useful. One that I haven’t used yet, but would if I needed it, is normalize. I have a similar function in my little bag of tricks that I use in my own code. What it does is takes a separated string, or an array, and converts them both to an array that has been trimmed. One gotcha with this one is the $assoc argument (defaults to true). If this is on and you feed it something like:
Set::normalize('This, is, my, list');
What you’ll get back is the following:
array( 'This' => null, 'is' => null, 'my' => null, 'list' => null );
which is not what I was expecting the first time I ran this function. What I was expecting was:
array( 0 => 'This', 1 => 'is', 2 => 'my', 3 => 'list' );
and to get that, just set $assoc to false:
Set::normalize('This, is, my, list', false);
A helper that I discovered recently is the number helper. It doesn’t have many methods, but I’ve already used currency on more than one occasion, which adds commas and decimals as well as the dollar sign to the output. It also has toReadableSize, which converts a raw file size in bytes to one in kB, MB, GB, etc. based on the size given. There is a toPercentage method, which while it sounds like it might convert decimal numbers to a percentage (e.g.- 0.75 to 75%), all it does is round to the place you give it, and add a percentage sign on the end. It would make more sense if it did the conversion to percentage as well, instead of just adding a percentage sign, but I digress.
A class that I hadn’t heard of until a colleague of mine cleaned up some of my code with it, is the HttpSocket class. This class is really cool, it takes all that cURL type stuff, and then some, and wraps it all up in a nice little package. The methods here that interest me the most are get, and post. Both of which do pretty much what you would imagine them to do.
So there are a few of my new favorite unknown functions, what are some that you’ve discovered and now can’t live without?




