datatable.directory codebase
				https://datatable.directory/
			
			
		
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							152 lines
						
					
					
						
							4.0 KiB
						
					
					
				
			
		
		
	
	
							152 lines
						
					
					
						
							4.0 KiB
						
					
					
				| <?php
 | |
| 
 | |
| namespace MightyPork\Providers;
 | |
| 
 | |
| use Illuminate\Support\Arr;
 | |
| use Illuminate\Support\Collection;
 | |
| use Illuminate\Support\ServiceProvider;
 | |
| 
 | |
| /**
 | |
|  * Register macros
 | |
|  */
 | |
| class MacroServiceProvider extends ServiceProvider
 | |
| {
 | |
| 	/**
 | |
| 	 * Bootstrap the application services.
 | |
| 	 *
 | |
| 	 * @return void
 | |
| 	 */
 | |
| 	public function boot()
 | |
| 	{
 | |
| 		/** Natural ordering */
 | |
| 		Collection::macro('natSortBy', function ($callback, $caseSensitive = false) {
 | |
| 			$results = [];
 | |
| 
 | |
| 			$callback = $this->valueRetriever($callback);
 | |
| 
 | |
| 			// First we will loop through the items and get the comparator from a callback
 | |
| 			// function which we were given. Then, we will sort the returned values and
 | |
| 			// and grab the corresponding values for the sorted keys from this array.
 | |
| 			try {
 | |
| 				foreach ($this->items as $key => $value) {
 | |
| 					$results[$key] = iconv('UTF-8', 'ASCII//TRANSLIT', $callback($value, $key));
 | |
| 				}
 | |
| 			} catch (\ErrorException $e) {
 | |
| 				// some iconv versions do not support translit of UTF-8
 | |
| 				foreach ($this->items as $key => $value) {
 | |
| 					$results[$key] = simple_translit($callback($value, $key));
 | |
| 				}
 | |
| 			}
 | |
| 
 | |
| 			uasort($results, $caseSensitive ? 'strnatcmp' : 'strnatcasecmp');
 | |
| 
 | |
| 			// Once we have sorted all of the keys in the array, we will loop through them
 | |
| 			// and grab the corresponding model so we can set the underlying items list
 | |
| 			// to the sorted version. Then we'll just return the collection instance.
 | |
| 			foreach (array_keys($results) as $key) {
 | |
| 				$results[$key] = $this->items[$key];
 | |
| 			}
 | |
| 
 | |
| 			return new static($results);
 | |
| 		});
 | |
| 
 | |
| 
 | |
| 		/** Natural ordering - for strings */
 | |
| 		Collection::macro('natSort', function ($caseSensitive = false) {
 | |
| 			$results = [];
 | |
| 
 | |
| 			// First we will loop through the items and get the comparator from a callback
 | |
| 			// function which we were given. Then, we will sort the returned values and
 | |
| 			// and grab the corresponding values for the sorted keys from this array.
 | |
| 			try {
 | |
| 				foreach ($this->items as $key => $value) {
 | |
| 					$results[$key] = iconv('UTF-8', 'ASCII//TRANSLIT', $value);
 | |
| 				}
 | |
| 			} catch (\ErrorException $e) {
 | |
| 				// some iconv versions do not support translit of UTF-8
 | |
| 				foreach ($this->items as $key => $value) {
 | |
| 					$results[$key] = simple_translit($value);
 | |
| 				}
 | |
| 			}
 | |
| 
 | |
| 			uasort($results, $caseSensitive ? 'strnatcmp' : 'strnatcasecmp');
 | |
| 
 | |
| 			// Once we have sorted all of the keys in the array, we will loop through them
 | |
| 			// and grab the corresponding model so we can set the underlying items list
 | |
| 			// to the sorted version. Then we'll just return the collection instance.
 | |
| 			foreach (array_keys($results) as $key) {
 | |
| 				$results[$key] = $this->items[$key];
 | |
| 			}
 | |
| 
 | |
| 			return new static($results);
 | |
| 		});
 | |
| 
 | |
| 
 | |
| 		/** Use this array as keys, callback to produce values */
 | |
| 		Collection::macro('asKeys', function ($callback) {
 | |
| 			$results = [];
 | |
| 
 | |
| 			$callback = $this->valueRetriever($callback);
 | |
| 
 | |
| 			foreach ($this->items as $value) {
 | |
| 				$results[$value] = $callback($value);
 | |
| 			}
 | |
| 
 | |
| 			return new static($results);
 | |
| 		});
 | |
| 
 | |
| 
 | |
| 		/** Trim all strings */
 | |
| 		Collection::macro('trim', function () {
 | |
| 			return $this->map(function($x) {
 | |
| 				return trim($x);
 | |
| 			});
 | |
| 		});
 | |
| 
 | |
| 		/** Trim all strings */
 | |
| 		Collection::macro('without', function ($keys) {
 | |
| 			if (!is_array($keys)) $keys = [$keys];
 | |
| 			return $this->filter(function($v, $k) use($keys) {
 | |
| 				return !in_array($k, $keys);
 | |
| 			});
 | |
| 		});
 | |
| 
 | |
| 		// --- kpluck ---
 | |
| 
 | |
| 		Arr::macro('kpluck', function ($array, $value, $key) {
 | |
| 			$results = [];
 | |
| 
 | |
| 			list($value, $key) = static::explodePluckParameters($value, $key);
 | |
| 
 | |
| 			foreach ($array as $origKey => $item) {
 | |
| 				$itemValue = data_get($item, $value);
 | |
| 
 | |
| 				// THIS was changed to use origKey
 | |
| 				if (is_null($key)) {
 | |
| 					$results[$origKey] = $itemValue;
 | |
| 				} else {
 | |
| 					$itemKey = data_get($item, $key);
 | |
| 
 | |
| 					$results[$itemKey] = $itemValue;
 | |
| 				}
 | |
| 			}
 | |
| 
 | |
| 			return $results;
 | |
| 		});
 | |
| 
 | |
| 		/** Pluck, but preserving keys. Single level only! */
 | |
| 		Collection::macro('kpluck', function ($value, $key = null) {
 | |
| 			return new Collection(Arr::kpluck($this->items, $value, $key));
 | |
| 		});
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * Register the application services.
 | |
| 	 *
 | |
| 	 * @return void
 | |
| 	 */
 | |
| 	public function register()
 | |
| 	{
 | |
| 		//
 | |
| 	}
 | |
| }
 | |
| 
 |