Advertisement
soywiz

access_value_dot / access_value_dot_set

Apr 5th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. function & access_value_dot($key, &$array) {
  2.     if (is_array($key)) {
  3.         $values = array();
  4.         foreach ($key as $single_key) $values[] = access_value_dot($single_key, $array);
  5.         return $values;
  6.     }
  7.     if (empty($key)) return $array;
  8.     $slices = explode('.', $key);
  9.     foreach ($slices as $slice) $array = &$array[$slice];
  10.     return $array;
  11. }
  12.  
  13. function access_value_dot_set($key, &$array, $value) {
  14.     if (is_array($key)) {
  15.         $values = array();
  16.         $value = array_values($value);
  17.         foreach (array_values($key) as $k => $single_key) {
  18.             access_value_dot_set($single_key, $array, $value[$k]);
  19.         }
  20.         return;
  21.     }
  22.     $ref = &access_value_dot($key, $array);
  23.     $ref = $value;
  24. }
  25.  
  26.  
  27. ///////////////////////////////////////////////////////////////
  28. ///////////////////////////////////////////////////////////////
  29.  
  30. class UtilsTest extends PHPUnit_Framework_TestCase {
  31.     public function testAccessValueDot() {
  32.         $data = array(
  33.             'level1' => array(
  34.                 'level2b' => array(
  35.                     'level3' => array(
  36.                         'level4a' => 1,
  37.                         'level4b' => 'Hello World',
  38.                     ),
  39.                 ),
  40.                 'key' => 2,
  41.             ),
  42.             'level1a' => array(),
  43.         );
  44.         $this->assertEquals(access_value_dot('level1.level2b.level3.level4b', $data), 'Hello World');
  45.         $this->assertEquals(access_value_dot(array(
  46.             'level1.level2b.level3.level4a',
  47.             'level1.level2b.level3.level4b',
  48.             'level1.key',
  49.             'level1a',
  50.             'level1.level2b.level3',
  51.         ), $data), array(
  52.             '1',
  53.             'Hello World',
  54.             '2',
  55.             array(),
  56.             array(
  57.                 'level4a' => 1,
  58.                 'level4b' => 'Hello World',
  59.             )
  60.         ));
  61.     }
  62.    
  63.     public function testAccessValueDotSet() {
  64.         $object = (object)array(
  65.             'bbcode' => array(
  66.                 'title' => 'Hello [b]World[/b]',
  67.                 'descriptions' => array(
  68.                     'short' => 'This is the [b]short[/b] description.',
  69.                     'long'  => 'This is the [b]LONG[/b] description.',
  70.                 ),
  71.             ),
  72.             'html' => array(
  73.             ),
  74.         );
  75.  
  76.         $field_keys = array(
  77.             'title',
  78.             'descriptions.short',
  79.             'descriptions.long',
  80.         );
  81.        
  82.         // Accessing bbcode values.
  83.         $extracted_values = access_value_dot($field_keys, $object->bbcode);
  84.        
  85.         {
  86.             // Converting bbcode to html.
  87.             $modified_values = array_map(function($value) {
  88.                 return preg_replace_callback('@\\[(\\w+)\\](.*)\\[/\\1\\]@Umsi', function($captures) {
  89.                     list(, $bbcode, $text) = $captures;
  90.                     switch ($bbcode) {
  91.                         case 'b': return '<strong>' . $text . '</strong>';
  92.                         default: throw(new Exception("Unknown bbcode '{$bbcode}'"));
  93.                     }
  94.                 }, htmlspecialchars($value));
  95.             }, $extracted_values);
  96.         }
  97.        
  98.         // Writting bbcode values.
  99.         access_value_dot_set($field_keys, $object->html, $modified_values);
  100.        
  101.         $this->assertEquals(
  102.             $object->html,
  103.             array(
  104.                 'title' => 'Hello <strong>World</strong>',
  105.                 'descriptions' => array(
  106.                     'short' => 'This is the <strong>short</strong> description.',
  107.                     'long' => 'This is the <strong>LONG</strong> description.',
  108.                 ),
  109.             )
  110.         );
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement