helpers.php 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

/**
 * Convert HTML characters to entities.
 *
 * The encoding specified in the application configuration file will be used.
 *
 * @param  string  $value
 * @return string
 */
function e($value)
{
tillsanders committed
13
	return HTML::entities($value);
14 15 16 17 18 19 20 21 22 23
}

/**
 * Retrieve a language line.
 *
 * @param  string  $key
 * @param  array   $replacements
 * @param  string  $language
 * @return string
 */
Taylor Otwell committed
24
function __($key, $replacements = array(), $language = null)
25
{
tillsanders committed
26
	return Lang::line($key, $replacements, $language);
27 28
}

Phill Sparks committed
29
/**
Taylor Otwell committed
30 31 32 33 34 35 36
 * Dump the given value and kill the script.
 *
 * @param  mixed  $value
 * @return void
 */
function dd($value)
{
37 38 39 40
	echo "<pre>";
	var_dump($value);
	echo "</pre>";
	die;
Taylor Otwell committed
41 42 43
}

/**
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 * Get an item from an array using "dot" notation.
 *
 * <code>
 *		// Get the $array['user']['name'] value from the array
 *		$name = array_get($array, 'user.name');
 *
 *		// Return a default from if the specified item doesn't exist
 *		$name = array_get($array, 'user.name', 'Taylor');
 * </code>
 *
 * @param  array   $array
 * @param  string  $key
 * @param  mixed   $default
 * @return mixed
 */
function array_get($array, $key, $default = null)
{
	if (is_null($key)) return $array;

63 64 65 66
	// To retrieve the array item using dot syntax, we'll iterate through
	// each segment in the key and look for that value. If it exists, we
	// will return it, otherwise we will set the depth of the array and
	// look for the next segment.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	foreach (explode('.', $key) as $segment)
	{
		if ( ! is_array($array) or ! array_key_exists($segment, $array))
		{
			return value($default);
		}

		$array = $array[$segment];
	}

	return $array;
}

/**
 * Set an array item to a given value using "dot" notation.
 *
 * If no key is given to the method, the entire array will be replaced.
 *
 * <code>
 *		// Set the $array['user']['name'] value on the array
 *		array_set($array, 'user.name', 'Taylor');
 *
 *		// Set the $array['user']['name']['first'] value on the array
 *		array_set($array, 'user.name.first', 'Michael');
 * </code>
 *
 * @param  array   $array
 * @param  string  $key
 * @param  mixed   $value
 * @return void
 */
function array_set(&$array, $key, $value)
{
	if (is_null($key)) return $array = $value;

	$keys = explode('.', $key);

	// This loop allows us to dig down into the array to a dynamic depth by
	// setting the array value for each level that we dig into. Once there
	// is one key left, we can fall out of the loop and set the value as
107
	// we should be at the proper depth.
108 109 110 111 112 113
	while (count($keys) > 1)
	{
		$key = array_shift($keys);

		// If the key doesn't exist at this depth, we will just create an
		// empty array to hold the next value, allowing us to create the
114
		// arrays to hold the final value.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
		if ( ! isset($array[$key]) or ! is_array($array[$key]))
		{
			$array[$key] = array();
		}

		$array =& $array[$key];
	}

	$array[array_shift($keys)] = $value;
}

/**
 * Remove an array item from a given array using "dot" notation.
 *
 * <code>
 *		// Remove the $array['user']['name'] item from the array
 *		array_forget($array, 'user.name');
 *
 *		// Remove the $array['user']['name']['first'] item from the array
 *		array_forget($array, 'user.name.first');
 * </code>
 *
 * @param  array   $array
 * @param  string  $key
 * @return void
 */
function array_forget(&$array, $key)
{
	$keys = explode('.', $key);

	// This loop functions very similarly to the loop in the "set" method.
	// We will iterate over the keys, setting the array value to the new
	// depth at each iteration. Once there is only one key left, we will
148
	// be at the proper depth in the array.
149 150 151 152 153 154 155
	while (count($keys) > 1)
	{
		$key = array_shift($keys);

		// Since this method is supposed to remove a value from the array,
		// if a value higher up in the chain doesn't exist, there is no
		// need to keep digging into the array, since it is impossible
156
		// for the final value to even exist.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		if ( ! isset($array[$key]) or ! is_array($array[$key]))
		{
			return;
		}

		$array =& $array[$key];
	}

	unset($array[array_shift($keys)]);
}

/**
 * Return the first element in an array which passes a given truth test.
 *
 * <code>
 *		// Return the first array element that equals "Taylor"
 *		$value = array_first($array, function($k, $v) {return $v == 'Taylor';});
 *
 *		// Return a default value if no matching element is found
 *		$value = array_first($array, function($k, $v) {return $v == 'Taylor'}, 'Default');
 * </code>
 *
 * @param  array    $array
 * @param  Closure  $callback
 * @param  mixed    $default
 * @return mixed
 */
function array_first($array, $callback, $default = null)
{
	foreach ($array as $key => $value)
	{
		if (call_user_func($callback, $key, $value)) return $value;
	}

	return value($default);
}

/**
195 196 197 198 199 200 201
 * Recursively remove slashes from array keys and values.
 *
 * @param  array  $array
 * @return array
 */
function array_strip_slashes($array)
{
202 203
	$result = array();

204 205 206 207 208 209 210 211 212
	foreach($array as $key => $value)
	{
		$key = stripslashes($key);

		// If the value is an array, we will just recurse back into the
		// function to keep stripping the slashes out of the array,
		// otherwise we will set the stripped value.
		if (is_array($value))
		{
213
			$result[$key] = array_strip_slashes($value);
214 215 216
		}
		else
		{
217
			$result[$key] = stripslashes($value);
218 219 220
		}
	}

221
	return $result;
222 223 224
}

/**
Taylor Otwell committed
225 226 227 228 229 230 231 232 233 234 235
 * Divide an array into two arrays. One with keys and the other with values.
 *
 * @param  array  $array
 * @return array
 */
function array_divide($array)
{
	return array(array_keys($array), array_values($array));
}

/**
236 237 238 239 240 241 242 243
 * Pluck an array of values from an array.
 *
 * @param  array   $array
 * @param  string  $key
 * @return array
 */
function array_pluck($array, $key)
{
244
	return array_map(function($v) use ($key)
245 246 247 248 249 250 251
	{
		return is_object($v) ? $v->$key : $v[$key];

	}, $array);
}

/**
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
 * Get a subset of the items from the given array.
 *
 * @param  array  $array
 * @param  array  $keys
 * @return array
 */
function array_only($array, $keys)
{
	return array_intersect_key( $array, array_flip((array) $keys) );
}

/**
 * Get all of the given array except for a specified array of items.
 *
 * @param  array  $array
 * @param  array  $keys
 * @return array
 */
function array_except($array, $keys)
{
	return array_diff_key( $array, array_flip((array) $keys) );
}

/**
276 277 278 279 280 281 282
 * Transform Eloquent models to a JSON object.
 *
 * @param  Eloquent|array  $models
 * @return object
 */
function eloquent_to_json($models)
{
283
	if ($models instanceof Laravel\Database\Eloquent\Model)
284 285 286 287 288 289 290 291
	{
		return json_encode($models->to_array());
	}

	return json_encode(array_map(function($m) { return $m->to_array(); }, $models));
}

/**
292 293 294 295 296 297 298 299 300 301
 * Determine if "Magic Quotes" are enabled on the server.
 *
 * @return bool
 */
function magic_quotes()
{
	return function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc();
}

/**
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
 * Return the first element of an array.
 *
 * This is simply a convenient wrapper around the "reset" method.
 *
 * @param  array  $array
 * @return mixed
 */
function head($array)
{
	return reset($array);
}

/**
 * Generate an application URL.
 *
 * <code>
 *		// Create a URL to a location within the application
319
 *		$url = url('user/profile');
320 321
 *
 *		// Create a HTTPS URL to a location within the application
322
 *		$url = url('user/profile', true);
323 324 325 326 327 328
 * </code>
 *
 * @param  string  $url
 * @param  bool    $https
 * @return string
 */
329
function url($url = '', $https = null)
330 331 332 333 334 335 336 337 338 339 340
{
	return Laravel\URL::to($url, $https);
}

/**
 * Generate an application URL to an asset.
 *
 * @param  string  $url
 * @param  bool    $https
 * @return string
 */
341
function asset($url, $https = null)
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
{
	return Laravel\URL::to_asset($url, $https);
}

/**
 * Generate a URL to a controller action.
 *
 * <code>
 *		// Generate a URL to the "index" method of the "user" controller
 *		$url = action('user@index');
 *
 *		// Generate a URL to http://example.com/user/profile/taylor
 *		$url = action('user@profile', array('taylor'));
 * </code>
 *
 * @param  string  $action
 * @param  array   $parameters
 * @return string
 */
361
function action($action, $parameters = array())
362
{
363
	return Laravel\URL::to_action($action, $parameters);
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
}

/**
 * Generate a URL from a route name.
 *
 * <code>
 *		// Create a URL to the "profile" named route
 *		$url = route('profile');
 *
 *		// Create a URL to the "profile" named route with wildcard parameters
 *		$url = route('profile', array($username));
 * </code>
 *
 * @param  string  $name
 * @param  array   $parameters
 * @return string
 */
381
function route($name, $parameters = array())
382
{
383
	return Laravel\URL::to_route($name, $parameters);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
}

/**
 * Determine if a given string begins with a given value.
 *
 * @param  string  $haystack
 * @param  string  $needle
 * @return bool
 */
function starts_with($haystack, $needle)
{
	return strpos($haystack, $needle) === 0;
}

/**
399 400 401 402 403 404 405 406 407 408 409 410
 * Determine if a given string ends with a given value.
 *
 * @param  string  $haystack
 * @param  string  $needle
 * @return bool
 */
function ends_with($haystack, $needle)
{
	return $needle == substr($haystack, strlen($haystack) - strlen($needle));
}

/**
411 412
 * Determine if a given string contains a given sub-string.
 *
413 414
 * @param  string        $haystack
 * @param  string|array  $needle
415 416 417 418
 * @return bool
 */
function str_contains($haystack, $needle)
{
419 420 421 422 423 424
	foreach ((array) $needle as $n)
	{
		if (strpos($haystack, $n) !== false) return true;
	}

	return false;
425 426 427
}

/**
428 429 430 431 432 433 434 435 436 437 438 439
 * Cap a string with a single instance of the given string.
 *
 * @param  string  $value
 * @param  string  $cap
 * @return string
 */
function str_finish($value, $cap)
{
	return rtrim($value, $cap).$cap;
}

/**
Taylor Otwell committed
440 441 442 443 444 445 446 447 448 449 450
 * Determine if the given object has a toString method.
 *
 * @param  object  $value
 * @return bool
 */
function str_object($value)
{
	return is_object($value) and method_exists($value, '__toString');
}

/**
451 452 453
 * Get the root namespace of a given class.
 *
 * @param  string  $class
454
 * @param  string  $separator
455 456
 * @return string
 */
457
function root_namespace($class, $separator = '\\')
458
{
459
	if (str_contains($class, $separator))
460
	{
461
		return head(explode($separator, $class));
462 463 464 465
	}
}

/**
466 467
 * Get the "class basename" of a class or object.
 *
Chris Berthe committed
468
 * The basename is considered to be the name of the class minus all namespaces.
469 470 471 472 473 474 475 476
 *
 * @param  object|string  $class
 * @return string
 */
function class_basename($class)
{
	if (is_object($class)) $class = get_class($class);

477
	return basename(str_replace('\\', '/', $class));
478 479 480
}

/**
481 482 483 484 485 486 487 488 489
 * Return the value of the given item.
 *
 * If the given item is a Closure the result of the Closure will be returned.
 *
 * @param  mixed  $value
 * @return mixed
 */
function value($value)
{
490
	return (is_callable($value) and ! is_string($value)) ? call_user_func($value) : $value;
Taylor Otwell committed
491 492 493 494 495 496 497 498 499 500 501
}

/**
 * Short-cut for constructor method chaining.
 *
 * @param  mixed  $object
 * @return mixed
 */
function with($object)
{
	return $object;
Taylor Otwell committed
502 503 504 505 506 507 508 509 510 511 512
}

/**
 * Determine if the current version of PHP is at least the supplied version.
 *
 * @param  string  $version
 * @return bool
 */
function has_php($version)
{
	return version_compare(PHP_VERSION, $version) >= 0;
513 514 515
}

/**
Taylor Otwell committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529
 * Get a view instance.
 *
 * @param  string  $view
 * @param  array   $data
 * @return View
 */
function view($view, $data = array())
{
	if (is_null($view)) return '';

	return Laravel\View::make($view, $data);
}

/**
530 531 532 533 534 535 536 537
 * Render the given view.
 *
 * @param  string  $view
 * @param  array   $data
 * @return string
 */
function render($view, $data = array())
{
538 539
	if (is_null($view)) return '';

540
	return Laravel\View::make($view, $data)->render();
541 542 543 544 545
}

/**
 * Get the rendered contents of a partial from a loop.
 *
Sergii Grebeniuk committed
546
 * @param  string  $partial
547 548 549 550 551 552 553 554
 * @param  array   $data
 * @param  string  $iterator
 * @param  string  $empty
 * @return string
 */
function render_each($partial, array $data, $iterator, $empty = 'raw|')
{
	return Laravel\View::render_each($partial, $data, $iterator, $empty);
555 556 557 558 559 560 561 562 563 564 565
}

/**
 * Get the string contents of a section.
 *
 * @param  string  $section
 * @return string
 */
function yield($section)
{
	return Laravel\Section::yield($section);
Taylor Otwell committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
}

/**
 * Get a CLI option from the argv $_SERVER variable.
 *
 * @param  string  $option
 * @param  mixed   $default
 * @return string
 */
function get_cli_option($option, $default = null)
{
	foreach (Laravel\Request::foundation()->server->get('argv') as $argument)
	{
		if (starts_with($argument, "--{$option}="))
		{
			return substr($argument, strlen($option) + 3);
		}
	}

	return value($default);
586 587 588 589 590 591 592 593 594 595 596 597
}
	
/**
 * Calculate the human-readable file size (with proper units).
 *
 * @param  int     $size
 * @return string
 */
function get_file_size($size)
{
	$units = array('Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB');
	return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2).' '.$units[$i];
598
}