Ejemplo n.º 1
0
 public function create($url)
 {
     $urls = $this->storage->getYAML('urls');
     // check to see if it exists already
     $index = count($urls) ? array_search($url, array_column($urls, 'url')) : FALSE;
     // if so, return existing short url
     if ($index !== FALSE) {
         return $urls[$index]['shortcode'];
     }
     // else, generate, store and return
     // add one here because can't encode an ID of 0, otherwise it breaks on first run
     $shortcode = ShortUrl::encode(count($urls) + 1);
     $urls[] = array('shortcode' => $shortcode, 'url' => $url, 'created_date' => time(), 'clicks' => 0);
     $this->storage->putYAML('urls', $urls);
     return $shortcode;
 }