first() static public method

I always have to lookup the names of that function so I decided to make this shortcut which is easier to remember.
static public first ( array $array ) : mixed
$array array The source array
return mixed The first element
Exemplo n.º 1
0
 function crawl()
 {
     $path = url::strip_query($this->raw);
     $path = (array) str::split($path, '/');
     if (a::first($path) == 'index.php') {
         array_shift($path);
     }
     // parse params
     foreach ($path as $p) {
         if (str::contains($p, ':')) {
             $parts = explode(':', $p);
             if (count($parts) < 2) {
                 continue;
             }
             $this->params->{$parts}[0] = $parts[1];
         } else {
             $this->path->_[] = $p;
         }
     }
     // get the extension from the last part of the path
     $this->extension = f::extension($this->path->last());
     if ($this->extension != false) {
         // remove the last part of the path
         $last = array_pop($this->path->_);
         $this->path->_[] = f::name($last);
     }
     return $this->path;
 }
Exemplo n.º 2
0
 public function testSort()
 {
     $users = $this->users;
     $users = a::sort($users, 'username', 'asc');
     $first = a::first($users);
     $last = a::last($users);
     $this->assertEquals('mary', $first['username']);
     $this->assertEquals('peter', $last['username']);
 }
Exemplo n.º 3
0
 public function avatar()
 {
     if (isset($this->cache['avatar'])) {
         return $this->cache['avatar'];
     }
     // try to find the avatar
     $root = c::get('root') . DS . 'assets' . DS . 'avatars' . DS . $this->username() . '.{jpg,png}';
     if ($avatar = a::first((array) glob($root, GLOB_BRACE))) {
         return $this->cache['avatar'] = new Media($avatar, url('assets/avatars/' . f::filename($avatar)));
     } else {
         return $this->cache['avatar'] = false;
     }
 }
Exemplo n.º 4
0
 function find()
 {
     global $site;
     $args = func_get_args();
     // find multiple pages
     if (count($args) > 1) {
         $result = array();
         foreach ($args as $arg) {
             $page = $this->find($arg);
             if ($page) {
                 $result[] = $page;
             }
         }
         return empty($result) ? false : new relatedPages($result);
     }
     return $site->pages()->find(a::first($args));
 }
Exemplo n.º 5
0
function randomimage($dir)
{
    $files = dir::read(c::get('root') . '/' . $dir);
    shuffle($files);
    return url($dir . '/' . a::first($files));
}
Exemplo n.º 6
0
 /**
  * Parses the hashed value from a cookie
  * and tries to extract the value 
  * 
  * @param string $hash
  * @return mixed
  */
 protected static function parse($string)
 {
     // extract hash and value
     $parts = str::split($string, '+');
     $hash = a::first($parts);
     $value = a::last($parts);
     // if the hash or the value is missing at all return null
     if (empty($hash) or empty($value)) {
         return null;
     }
     // compare the extracted hash with the hashed value
     if ($hash !== static::hash($value)) {
         return null;
     }
     return $value;
 }
Exemplo n.º 7
0
 function find()
 {
     $args = func_get_args();
     // find multiple pages
     if (count($args) > 1) {
         $result = array();
         foreach ($args as $arg) {
             $page = $this->find($arg);
             if ($page) {
                 $result[$page->uid] = $page;
             }
         }
         return empty($result) ? false : new pages($result);
     }
     // find a single page
     $path = a::first($args);
     $array = str::split($path, '/');
     $obj = $this;
     $page = false;
     foreach ($array as $p) {
         $next = $obj->{$p};
         if (!$next) {
             return $page;
         }
         $page = $next;
         $obj = $next->children();
     }
     return $page;
 }
Exemplo n.º 8
0
 /**
  * Generates an "a mailto" tag
  *
  * @param string $email The url for the a tag
  * @param mixed $text The optional text. If null, the url will be used as text
  * @param array $attr Additional attributes for the tag
  * @return string the generated html
  */
 public static function email($email, $text = null, $attr = array())
 {
     if (empty($text)) {
         // show only the eMail address without additional parameters (if the 'text' argument is empty)
         $text = str::encode(a::first(str::split($email, '?')));
     }
     $email = str::encode($email);
     $attr = array_merge(array('href' => 'mailto:' . $email), $attr);
     return static::tag('a', $text, $attr);
 }
Exemplo n.º 9
0
 /**
  * Returns a number between 0 and 1 that defines how "accepted"
  * a specified MIME type is for the visitor's browser using the
  * HTTP Accept header
  *
  * @param  string $type MIME type like "text/html"
  * @return float        Number between 0 (not accepted) and 1 (very accepted)
  */
 public static function acceptance($type)
 {
     $accept = a::get($_SERVER, 'HTTP_ACCEPT');
     // if there is no header, everything is accepted
     if (!$accept) {
         return 1;
     }
     // check each type in the Accept header
     foreach (str::split($accept, ',') as $item) {
         $item = str::split($item, ';');
         $mime = a::first($item);
         // $item now only contains params
         // check if the type matches
         if (!fnmatch($mime, $type, FNM_PATHNAME)) {
             continue;
         }
         // check for the q param ("quality" of the type)
         foreach ($item as $param) {
             $param = str::split($param, '=');
             if (a::get($param, 0) === 'q' && ($value = a::get($param, 1))) {
                 return (double) $value;
             }
         }
         // no quality param, default to a quality of 1
         return 1;
     }
     // no match at all, the type is not accepted
     return 0;
 }
Exemplo n.º 10
0
 function languageSetup()
 {
     // check for activated language support
     if (!c::get('lang.support')) {
         return false;
     }
     // get the available languages
     $available = c::get('lang.available');
     // sanitize the available languages
     if (!is_array($available)) {
         // switch off language support
         c::set('lang.support', false);
         return false;
     }
     // get the raw uri
     $uri = uri::raw();
     // get the current language code
     $code = a::first(explode('/', $uri));
     // try to detect the language code if the code is empty
     if (empty($code)) {
         if (c::get('lang.detect')) {
             // detect the current language
             $detected = str::split(server::get('http_accept_language'), '-');
             $detected = str::trim(a::first($detected));
             $detected = !in_array($detected, $available) ? c::get('lang.default') : $detected;
             // set the detected code as current code
             $code = $detected;
         } else {
             $code = c::get('lang.default');
         }
         // go to the default homepage
         go(url(false, $code));
     }
     // http://yourdomain.com/error
     // will redirect to http://yourdomain.com/en/error
     if ($code == c::get('404')) {
         go(url('error', c::get('lang.default')));
     }
     // validate the code and switch back to the homepage if it is invalid
     if (!in_array($code, c::get('lang.available'))) {
         go(url());
     }
     // set the current language
     c::set('lang.current', $code);
     // mark if this is a translated version or the default version
     $code != c::get('lang.default') ? c::set('lang.translated', true) : c::set('lang.translated', false);
     // load the additional language files if available
     load::language();
 }
Exemplo n.º 11
0
 function find()
 {
     $args = func_get_args();
     // find multiple files
     if (count($args) > 1) {
         $result = array();
         foreach ($args as $arg) {
             $file = $this->find($arg);
             if ($file) {
                 $result[$file->filename] = $file;
             }
         }
         return empty($result) ? false : new files($result);
     }
     // find a single file
     $key = a::first($args);
     if (!$key) {
         return $this->_;
     }
     return a::get($this->_, $key);
 }
Exemplo n.º 12
0
 /**
  * Modified inventory fetcher
  *
  * @return array
  */
 public function inventory()
 {
     $inventory = parent::inventory();
     $defaultLang = $this->site->defaultLanguage->code;
     $expression = '!(.*?)(\\.(' . implode('|', $this->site->languages->codes()) . ')|)\\.' . $this->kirby->options['content.file.extension'] . '$!i';
     foreach ($inventory['meta'] as $key => $meta) {
         $inventory['meta'][$key] = array($defaultLang => $meta);
     }
     foreach ($inventory['content'] as $key => $content) {
         preg_match($expression, $content, $match);
         $file = $match[1];
         $lang = isset($match[3]) ? $match[3] : null;
         if (in_array($file, $inventory['files'])) {
             $inventory['meta'][$file][$lang] = $content;
         } else {
             if (is_null($lang)) {
                 $lang = f::extension($file);
                 if (empty($lang)) {
                     $lang = $defaultLang;
                 }
             }
             $inventory['content'][$lang] = $content;
         }
         unset($inventory['content'][$key]);
     }
     // try to fill the default language with something else
     if (!isset($inventory['content'][$defaultLang])) {
         $inventory['content'][$defaultLang] = a::first($inventory['content']);
     }
     return $inventory;
 }
Exemplo n.º 13
0
 function first()
 {
     return a::first($this->_);
 }
Exemplo n.º 14
0
 function find()
 {
     $args = func_get_args();
     // find multiple pages
     if (count($args) > 1) {
         $result = array();
         foreach ($args as $arg) {
             $page = $this->find($arg);
             if ($page) {
                 $result[$page->uid] = $page;
             }
         }
         return empty($result) ? false : new pages($result);
     }
     // find a single page
     $path = a::first($args);
     $array = str::split($path, '/');
     $obj = $this;
     $page = false;
     // check if we need to search for translated urls
     $translated = c::get('lang.support');
     foreach ($array as $p) {
         $next = $translated ? $obj->findBy('translatedUID', $p) : $obj->{'_' . $p};
         if (!$next) {
             return $page;
         }
         $page = $next;
         $obj = $next->children();
     }
     return $page;
 }
Exemplo n.º 15
0
" />
      </div>

                         <!-- manually prevented the popup from being shown when the page is of type 'project'.
                        the population of the menu is then limited to "media type" - templates
                        in the end i made sure that the default-template is chosen if the panel is "home"
                        so that we can add categories which are then NOT of type 'project' but use the
                        default template -->


      <div class="field">
        <?php 
if (count($templates) == 1) {
    ?>
          <?php 
    $template = a::first($templates);
    ?>
          <label><?php 
    echo l::get('pages.add.label.template');
    ?>
: <em><?php 
    echo html(data::templateName($template));
    ?>
 (<?php 
    echo html($template);
    ?>
)</em></label>
          <input type="hidden" name="template" value="<?php 
    echo html($template);
    ?>
" />
Exemplo n.º 16
0
 * @var  array
 */
return array('auth' => function () {
    if (!user::current() || !user::current()->isAdmin()) {
        redirect::to('plugin/comments/wizard');
    }
}, 'installed' => function () {
    if ($this->isInstalled()) {
        redirect::home();
    }
}, 'userCanCreate' => function () {
    $route = plugin('comments')->route();
    $hash = a::first($route->arguments());
    $page = site()->index()->findBy('hash', $hash);
    return $page instanceof Page && $page->isVisible();
}, 'userCanRead' => function () {
    $route = plugin('comments')->route();
    $hash = a::first($route->arguments());
    $page = site()->index()->findBy('hash', $hash);
    return $page instanceof Page && $page->isVisible();
}, 'userCanUpdate' => function () {
    $route = plugin('comments')->route();
    $id = a::last($route->arguments());
    $comment = comment::find($id);
    return $comment instanceof Comment && $comment->currentUserCan('update');
}, 'userCanDelete' => function () {
    $route = plugin('comments')->route();
    $id = a::last($route->arguments());
    $comment = comment::find($id);
    return $comment instanceof Comment && $comment->currentUserCan('delete');
});