Welcome to the Cookbook

loading...

8.5.18 map

object Set::map ($class = 'stdClass', $tmp = 'stdClass')

Cette méthode mappe le contenu de l'objet Set vers une hiérarchie d'objets, tout en gardant les clés numériques dans un tableau d'objets.

Pour faire simple, la fonction map transforme les éléments d'un tableau en des objets de classe initialisés. Par défaut, il transforme un tableau en un objet stdClass, cependant vous pouvez mapper des valeurs dans n'importe quel type de classe. Exemple: Set::map($array_of_values, 'nameOfYourClass');

$data = array(
	array(
		"IndexedPage" => array(
			"id" => 1,
			"url" => 'http://blah.com/',
			'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
			'get_vars' => '',
			'redirect' => '',
			'created' => "1195055503",
			'updated' => "1195055503",
		)
	),
	array(
		"IndexedPage" => array(
			"id" => 2,
			"url" => 'http://blah.com/',
			'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
			'get_vars' => '',
			'redirect' => '',
			'created' => "1195055503",
			'updated' => "1195055503",
		),
	)
);
$mapped = Set::map($data);

/* $mapped ressemble maintenant à cela:

	Array
	(
	    [0] => stdClass Object
	        (
	            [_name_] => IndexedPage
	            [id] => 1
	            [url] => http://blah.com/
	            [hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
	            [get_vars] => 
	            [redirect] => 
	            [created] => 1195055503
	            [updated] => 1195055503
	        )

	    [1] => stdClass Object
	        (
	            [_name_] => IndexedPage
	            [id] => 2
	            [url] => http://blah.com/
	            [hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
	            [get_vars] => 
	            [redirect] => 
	            [created] => 1195055503
	            [updated] => 1195055503
	        )

	)

*/
  1. $data = array(
  2. array(
  3. "IndexedPage" => array(
  4. "id" => 1,
  5. "url" => 'http://blah.com/',
  6. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  7. 'get_vars' => '',
  8. 'redirect' => '',
  9. 'created' => "1195055503",
  10. 'updated' => "1195055503",
  11. )
  12. ),
  13. array(
  14. "IndexedPage" => array(
  15. "id" => 2,
  16. "url" => 'http://blah.com/',
  17. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  18. 'get_vars' => '',
  19. 'redirect' => '',
  20. 'created' => "1195055503",
  21. 'updated' => "1195055503",
  22. ),
  23. )
  24. );
  25. $mapped = Set::map($data);
  26. /* $mapped ressemble maintenant à cela:
  27. Array
  28. (
  29. [0] => stdClass Object
  30. (
  31. [_name_] => IndexedPage
  32. [id] => 1
  33. [url] => http://blah.com/
  34. [hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
  35. [get_vars] =>
  36. [redirect] =>
  37. [created] => 1195055503
  38. [updated] => 1195055503
  39. )
  40. [1] => stdClass Object
  41. (
  42. [_name_] => IndexedPage
  43. [id] => 2
  44. [url] => http://blah.com/
  45. [hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
  46. [get_vars] =>
  47. [redirect] =>
  48. [created] => 1195055503
  49. [updated] => 1195055503
  50. )
  51. )
  52. */

Set::map() utilisé avec une classe personnalisée comme second paramètre:

class MyClass {
    function sayHi() {
        echo 'Hi!';
    }
}

$mapped = Set::map($data, 'MyClass');
//Maintenant, vous pouvez accéder à toutes les propriétés comme dans l'exemple ci-dessus,
//mais vous pouvez aussi appeler la méthode MyClass.
$mapped->[0]->sayHi();
  1. class MyClass {
  2. function sayHi() {
  3. echo 'Hi!';
  4. }
  5. }
  6. $mapped = Set::map($data, 'MyClass');
  7. //Maintenant, vous pouvez accéder à toutes les propriétés comme dans l'exemple ci-dessus,
  8. //mais vous pouvez aussi appeler la méthode MyClass.
  9. $mapped->[0]->sayHi();