📜 ⬆️ ⬇️

Method that returns the entire object

Hello, dear Knowledgeable people. Sorry if I put my question in the wrong place - for the first time.

So I created a class that allows, as it seems to me, more convenient to work with lists of data. Convenience is that the object in most methods returns itself, which allows you to continue to play with it. Using Zend for Eclipse and comments on return asList methods, I get a buzz when working with data lists. Example:

  $ obj = new asList;
 print_r ($ obj->
	 set (array ("my" => "lol", "dear" => "rofl", "mom" => "hehe")) ->
	 intersect ("hello, my, dear, friend") ->
	 merge ("test, yo") ->
	 fillValuesFrom (array ("yo" => "good test")) ->
	 asArray () 
 ); 

Returns:
  Array ([my] => [dear] => [test] => [yo] => good test) 

For javascript, this is normal when the object returns itself, but for some strange reason I could not find such classes in PHP in the same google. It made me wonder if I was doing something wrong? Can such an implementation have a big loss in speed or eat a lot of resources? Question to the experts, is it worth using such an approach (and by the way, what is it properly called?)
')
Below is the class code.

  <? php

 / **
  * Associative List class
  * 
  * /
 class asList {
	
	 private $ _data;
	
	 / **
	  * New associative list
	  *
	  * @param array || string $ data (optional)
	  * @param string $ clue (optional), if $ data type == string
	  * /
	 function __construct ($ data = NULL, $ clue = ",") {
		 if (! is_null ($ data)) $ this-> set ($ data, $ clue);
	 }
	
	 / **
	  * Private function to convert array || string to array
	  *
	  * @param array || string $ array_or_string
	  * @param string (optional) $ clue
	  * @return array
	  * /
	 private function _toArray ($ array_or_string, $ clue = ",") {
		 if (is_array ($ array_or_string)) {
			 $ array = $ array_or_string;
		 } elseif (is_string ($ array_or_string)) {
			 $ array_or_string = trim ($ array_or_string, $ clue. "");
			 $ keys = @explode ($ clue, $ array_or_string);
			 $ array = array_fill_keys ($ keys, '');
		 }
		 if (! is_array ($ array)) $ array = array ();
		 unset ($ array [""]);  // data [] not allowed
		 return $ array;
	 }
	
	 / **
	  * Set data to work with
	  *
	  * @param arr || str $ array_or_string
	  * @param string $ clue
	  * @return asList
	  * /
	 public function set ($ array_or_string, $ clue = ",") {
		 $ array = $ this -> _ toArray ($ array_or_string, $ clue);
		 $ this -> _ data = $ array;
		 return $ this;
	 }
	
	 / **
	  * Export data as array
	  *
	  * @return array
	  * /
	 public function asArray () {
		 return $ this -> _ data;
	 }
	
	 / **
	  * Export data keys as string
	  *
	  * @param string (optional) $ clue
	  * @return string
	  * /
	 public function asString ($ clue = ",") {
		 return @implode ($ clue, @array_keys ($ this -> _ data));
	 }
	
	 / **
	  * Make string using $ form
	  *
	  * @param string $ selected
	  * @param string $ form - use <key> <value> <selected> 
	  * @param string $ clue
	  * @return string
	  * /
	 public function asStringAdvanced ($ selected = NULL, $ form = "<option value = '<key>' <selected >> <value> </ option>", $ clue = NULL) {
		 $ data = (is_array ($ this -> _ data))?  $ this -> _ data: array ();
		 while (list ($ key, $ val) = each ($ data)) {
			 $ data [$ key] = str_replace ( 
				 array ("<key>", "<value>", "<selected>"), 
				 array ($ key, $ val, ($ key == $ selected)? "selected": ""), 
				 $ form 
				 );
		 }
		 reset ($ data);
		 return @implode ($ clue, $ data);
	 }
	
	
	 / **
	  * Merge data with another data
	  *
	  * @param arr || str $ array_or_string
	  * @param string (optional) $ clue
	  * @return asList
	  * /
	 public function merge ($ array_or_string, $ clue = ",") {
		 $ array1 = (is_array ($ this -> _ data))?  $ this -> _ data: array ();
		 $ array2 = $ this -> _ toArray ($ array_or_string, $ clue);
		 $ this -> _ data = array_merge ($ array1, $ array2);
		 unset ($ this -> _ data [""]);  // data [] not allowed
		 return $ this;
	 }

	 / **
	  * Intersect data with another data
	  * If array, forgot about keys, only values ​​willbe used
	  *
	  * @param arr || string $ array_or_string
	  * @param string (optional) $ clue
	  * @return asList
	  * /
	 public function intersect ($ array_or_string, $ clue = ",") {
		 if (! is_array ($ array_or_string)) {
			 $ array = $ this -> _ toArray ($ array_or_string, $ clue);
			 $ array = array_keys ($ array);
		 } else {
			 $ array = array_values ​​($ array_or_string);
		 }
		 foreach ($ array as $ val) {
			 if (isset ($ this -> _ data [$ val])) $ ret [$ val] = $ this -> _ data [$ val];
		 }
		 $ this -> _ data = $ ret;
		 return $ this;
	 }
	
	 / **
	  * Fill data with datas from $ array
	  *
	  * @param array $ array - associative array where to get new data from
	  * @param bool $ preserve_original_if_not_exists - preserves the original value if there are no such key
	  * @return asList
	  * /
	 public function fillValuesFrom ($ array, $ preserve_original_if_not_exists = true) {
		 while (list ($ key,) = @each ($ this -> _ data)) {
			 if ($ preserve_original_if_not_exists &&! array_key_exists ($ key, $ array)) {
				 // preserve original
			 } else {
				 $ this -> _ data [$ key] = $ array [$ key];
			 }
		 }
		 reset ($ this -> _ data);
		 return $ this;
	 }

	 / **
	  * Remove data with given keys
	  * If array in values, the values ​​will be used
	  *
	  * @param arr || string $ array_or_string - list of keys
	  * @param string (optional) $ clue
	  * @return asList
	  * /
	 public function remove ($ array_or_string, $ clue = ",") {
		 if (! is_array ($ array_or_string)) {
			 $ array = $ this -> _ toArray ($ array_or_string, $ clue);
			 $ array = array_keys ($ array);
		 } else {
			 $ array = array_values ​​($ array_or_string);
		 }
		 foreach ($ array as $ item) {
			 unset ($ this -> _ data [$ item]);
		 }
		 return $ this;
	 }
	
	 public function exists ($ key) {
		 return (isset ($ this -> _ data [$ key]))?  true: false;
	 }
 }

 ?> 

Source: https://habr.com/ru/post/40338/


All Articles