Tag archives for set

PowerSet::def() API

Often my functions accepts an array of options to drive its behavior:

function foo( $data, $options = array() ) {
  if ( $options['filter'] = 'int' ) return intval($data);
  return $data;
}

The plus of accepting an array of options is your method can grow up without change it’s params API.

I often allow to accepts more than array type for my $options param:

function foo( $data, $options = array() ) {
  if ( is_string($options) ) $options = array( 'filter'=>$options );
  ...
}

This way my method API should be something like:

// full api
echo foo( $data, array('filter'=>'int'));

// compact api
echo foo( $data, 'int' );

I like DRY code so much!

Make it easy!

What PowerSet::def() does is to simplify the logic that convert different $options types into specific property names:

function foo( $data, $options = array() ) {
  $options = PowerSet::def($options, null, array(
    'string'=>'filter', 
    'integer'=>'length'
  ));
  ...
}

This code should be translated into:

"if $options type is -string- then put it value into -filter- key"
"if $options type is -integer- then put it value into -length- key"

In every cake $options will be converted into an array.

PowerSet::def('aaa', null, 'class');
-> array( 'class'=>'aaa' );

PowerSet::def(24, null, 'length');
-> array( 'length'=> (int) 24 );

PowerSet::def('aaa', null, array( 'string'=>'class', 'integer'=>'length'));
-> array( 'class'=>'aaa' );

PowerSet::def(24, null, array( 'string'=>'class', 'integer'=>'length'));
-> array( 'length'=> (int) 24 );

Why “null” as second param?

You can set up a default array to be extended with given values:

// useful to apply default values to an array
PowerSet::def('container', array(
  'id => '',
  'class => '',
  'style => '',
), 'class');
Posted in CakePOWER, Script | Leave a comment

PowerSet – powerful array management

CakePHP’s Set class introduces a lot of useful methods for array handling jobs.

PowerSet extends this class inheriting all Set’s methods and adding some new and very useful ones.

The Source :: GitHub Repo

You can look at the fully documented source code in CakePower GitHub repository:

https://github.com/CakePower/CakePower/blob/master/Vendor/PowerSet.php

Methods Reference

todef( (string)$value, (mixed)$types )

apply a default array format to an input value.

PowerSet::todef( 'aaa', 'class' ) -> array( 'class'=>'aaa' )

Full documentation here

is_vector( (array)$set )

check if $set’s keys are digits only.

is_assoc( (array)$set )

!is_vector()

dots2array( (array()$set )

transform a dotted string into an array.

"path1.path2.path3"
-> array(
    'path1',
    'path2',
    'path3'
)

array2dots( (array)$set )

reverse of dots2array(), transform a mono dimensional string array into a dotted string.

beforeAssoc( (array)$set, (string)$matchKey, (string)$newKey, (mixed)$newVal )

Insert a new array before the $matchKey element inside the array:

// Origina array:
$set = array(
    'name' => 'Mark',
    'surname' => 'Sheepkeeper',
    'nick' => 'mpeg'
);

// Append new value:
$set = PowerSet::beforeAssoc( $set, 'nick', 'age', 30 );
-> array(
    'name' => 'Mark',
    'surname' => 'Sheepkeeper',
    'age' => 30,
    'nick' => 'mpeg'
);

// Another way:
$set = PowerSet::beforeAssoc( $set, 'age', array( 'birthDay'=>'1981-06-30' ));
-> array(
    'name' => 'Mark',
    'surname' => 'Sheepkeeper',
    'birthDay' => 1981-06-30',
    'age' => 30,
    'nick' => 'mpeg'
);

afterAssoc( (array)$set, (string)$matchKey, (string)$newKey, (mixed)$newVal )

It works like PowerSet::beforeAssoc() but append the new item after the $matchKey.

beforeVector( (array)$set, (mixed)$key, (mixed)$val)

Insert a new item in a vectorial array before a desired item identified by value or index.

// Original array:
$set = array( 'red', 'white', 'green' );

$set = PowerSet::beforeVector( $set, 'white', 'yellow' );
-> array( 'red', 'yellow', 'white', 'green' );

$set = PowerSet::beforeVector( $set, '{2}', 'fuchsia' );
-> array( 'red', 'yellow', 'fuchsia', 'white', 'green' );

afterVector( (array)$set, (mixed)$key, (mixed)$val)

It works like PowerSet::beforeVector() but append the new item after the $key.

before(), after()

These methods are shortcuts to the relative assoc and vector aliases.

The $set you pass to is tested with PowerSet::is_vector() to invoke the correct method.

NOTICE: if you know the type of your vector your are asked to use the typed methods because they are more efficient.

Posted in CakePHP | Leave a comment

Swedish Greys - a WordPress theme from Nordic Themepark.