Skip to content

Instantly share code, notes, and snippets.

@berkes
Created November 3, 2011 15:28
Show Gist options
  • Save berkes/1336778 to your computer and use it in GitHub Desktop.
Save berkes/1336778 to your computer and use it in GitHub Desktop.
Purrfect Drupal Coding Guideline Helper: Completely randomise your array and object usage.
<?php
/**
* Usage
* $inconsistent_view_thingy = views_get_view("some_view_name");
* $more_inconsistent_view_thingy = rand_obj_or_array($inconsistent_view_thingy);
*/
function rand_obj_or_array($thing) {
$det = rand(0,2);
switch ($det) {
case 0: # Leave as is
return $thing;
case 1: # Turn into a stdclass object.
foreach($thing as $key => $value) {
$other_thing->$key = $value;
}
return $other_thing;
case 2: # Turn into an array.
return (array) $thing;
case 3: # Turn into an array with drupalish '#keys'.
$other_thing = array();
$thing = (array) $thing;
foreach($thing as $key => $value) {
$other_thing["#{$key}"] = $value;
}
return $other_thing;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment