Skip to content

Latest commit

 

History

History
160 lines (108 loc) · 3.49 KB

helpers.md

File metadata and controls

160 lines (108 loc) · 3.49 KB

Helpers

Helpers are framework utility functions you can use anywhere. They are functions that run on the global scope.

e($value)

  • param string Raw data.
  • return string Escaped data.

Escapes HTML entities in a string. This function uses the htmlentities() PHP core function.

$escaped = e('<p>Raw input</p>');

starts_with($haystack, $needles)

  • param string, string|array
  • return bool

Checks whether a given string starts with a given substring.

starts_with('foo bar', 'foo') // true

starts_with('foo bar', ['non-existing-string', 'foo']) // true

array_get($array, $key, $default)

  • param array, string, mixed
  • return mixed

Get an item of an array using a "." notation

$array = ['items' => [
        'first-item' => 'I am the first item', 
        'second-item' => 'I am the second Item'
    ]
]

$item = array_get($array, 'items.first-item', 'no key found') // 'I am the first item'

$item = array_get($array, 'items.third-item', 'No item found') // 'No item found'

array_set($array, $key, $value)

  • param array $array
  • param string $key
  • param mixed $value
  • 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.

$array = array_set($array, 'items.first', 'blue');

array_except($array, $keys)

  • param array $array
  • param string $keys
  • return array

Get all of the given array except for a specified array of items (keys).

$array = array_except(['a' => 1, 'b' => 2, 'c' => 3], ['b']);

array_is_sequential($array)

  • param array $array
  • return bool

Check if an array is sequential (have keys from 0 to n) or not.

array_is_sequential(['red', 'green', 'blue']); return true
array_is_sequential([3 => 'red', 42 => 'green', 119 => 'blue']); return false

value($value)

  • param mixed $value
  • return mixed

Return the default value of the given value.

$value = value($var);

with($object)

  • param mixed $object
  • return mixed

Return the given object. Useful for chaining.

$val = with($instance)->get();

str_contains($haystack, $needles)

  • param string $haystack
  • param string | array $needles
  • return bool

Determine if a given string contains a given substring.

str_contains('This is a comment', 'comment'); // return true
str_contains('This is a comment', ['is', 'comment']); // return true
str_contains('This is a comment', 'post'); // return false

app($instance) & container($instance)

  • param string $instance

Helper function to return any instance registerd into the service container.

$config = container('config'); // return the config instance
$factory = container('view'); // return the view factory instance
$twig = container('twig'); // return the twig environment instance

view($name, $data, $mergeData)

  • param string $name
  • param array $data
  • param array $mergeData
  • return string

Helper function to build views.

$view = view('pages', ['title' => 'Custom page']);

meta($key, $id, $context, $single)

  • param string $key
  • param int $id
  • param string $context
  • param bool $single
  • return mixed|string

Helper function to get any meta data from objects.

$value = meta('_wp_page_template', $id);
$term_order = meta('order', $term_id, 'term');