Esempio n. 1
0
 function urlize($text)
 {
     return trim(Inflector::underscore(Inflector::unaccent($text)), '_');
 }
Esempio n. 2
0
 public function searchBano($sellzone_id, $q, $limit = 30, $offset = 0, $lat = 0, $lng = 0)
 {
     return call_user_func_array([$this, 'suggestAddressBySellzone'], func_get_args());
     if (fnmatch('* *', $q)) {
         $tab = explode(' ', $q);
         $first = Arrays::first($tab);
         if (is_numeric($first)) {
             $number = true;
             array_shift($tab);
             $q = implode(' ', $tab);
         }
     }
     $keyCache = 'l.cachersBanos.' . sha1(serialize(func_get_args()));
     $collection = redis()->get($keyCache);
     $cachedZip = redis()->get('zips.covered.' . $sellzone_id);
     if (!$cachedZip) {
         $coll = [];
         $zips = Model::Coveredcity()->where(['sellzone_id', '=', (int) $sellzone_id])->cursor();
         foreach ($zips as $zip) {
             $coll[] = $zip['zip'];
         }
         redis()->set('zips.covered.' . $sellzone_id, serialize($coll));
         $cachedZip = $coll;
     } else {
         $cachedZip = unserialize($cachedZip);
     }
     $sz = Model::Sellzone()->refresh()->find((int) $sellzone_id);
     $lat = 0 < $lat ? $lat : (double) $sz->latitude;
     $lng = 0 < $lng ? $lng : (double) $sz->longitude;
     $calcDistance = 0 < $lat && 0 < $lng;
     if (!$collection) {
         $collection = [];
         if ($sz) {
             $q = str_replace(' ', '%', Inflector::lower(Inflector::unaccent($q)));
             $cursor = Model::GeoBano()->where(['sellzone_id', '=', $sz->id])->where(['street', 'LIKE', "%{$q}%"])->cursor();
             $c = 0;
             while ($row = $cursor->fetch()) {
                 unset($row['id']);
                 unset($row['sellzone_id']);
                 unset($row['created_at']);
                 unset($row['updated_at']);
                 $address = $row['street'];
                 ksort($row);
                 $distances = distanceKmMiles(floatval($row['lng']), floatval($row['lat']), floatval($lng), floatval($lat));
                 if ((double) $distances['km'] > 5) {
                     continue;
                 }
                 $old = $row;
                 if (isset($number)) {
                     $row = $this->getCoords($first . ' ' . $row['street'] . ' ' . $row['zip'] . ' ' . $row['city']);
                 } else {
                     $row = $this->getCoords($row['street'] . ' ' . $row['zip'] . ' ' . $row['city']);
                 }
                 if (isset($row['zip'])) {
                     if (!in_array($row['zip'], $cachedZip)) {
                         continue;
                     }
                 } else {
                     continue;
                 }
                 if (isset($row['city'])) {
                     if (is_array($row['city'])) {
                         $row['city_id'] = $row['city']['code'];
                         $row['city'] = $old['city'];
                     }
                 }
                 $row['address'] = $address;
                 if ($calcDistance) {
                     if (isset($row['lng']) && isset($row['lat']) && isset($lat) && isset($lng)) {
                         $distances = distanceKmMiles(floatval($row['lng']), floatval($row['lat']), floatval($lng), floatval($lat));
                         $km = floatval($distances['km']);
                         $row['distance'] = $km;
                     } else {
                         $row['distance'] = 0;
                     }
                 }
                 foreach ($row as $k => $v) {
                     if (fnmatch('*_id', $k)) {
                         $row[$k] = (int) $v;
                     } else {
                         if (is_numeric($v) && (fnmatch('*.*', $v) || fnmatch('*.*', $v))) {
                             $row[$k] = (double) $v;
                         }
                     }
                 }
                 if (isset($row['address_label'])) {
                     $row['name'] = $row['address_label'];
                     unset($row['address_label']);
                 }
                 if (fnmatch($sz->department . '*', $row['zip'])) {
                     // if (count($collection) >= $limit) {
                     //     return $this->sortBy($collection, 'distance');
                     // }
                     $collection[] = $row;
                     $c++;
                 }
             }
         }
         redis()->set($keyCache, serialize($collection));
     } else {
         $collection = unserialize($collection);
     }
     if (empty($collection)) {
         $sugColl = [];
         $suggest = $this->suggestAddressBySellzone($sellzone_id, $q);
         foreach ($suggest as $sug) {
             if (isset($sug['lng']) && isset($sug['lat']) && isset($lat) && isset($lng)) {
                 $distances = distanceKmMiles(floatval($sug['lng']), floatval($sug['lat']), floatval($lng), floatval($lat));
                 $km = floatval($distances['km']);
                 $sug['distance'] = $km;
                 $sugColl[] = $sug;
             }
         }
         return $this->sortBy($sugColl, 'distance');
     }
     if (isset($calcDistance)) {
         if ($calcDistance) {
             $collection = $this->sortBy($collection, 'distance');
         }
     }
     return array_slice($collection, $offset, $limit);
 }
Esempio n. 3
0
 private static function _urlize($text)
 {
     return trim(Inflector::underscore(Inflector::unaccent($text)), '_');
 }
Esempio n. 4
0
 public function where($key, $operator = null, $value = null)
 {
     // Here we will make some assumptions about the operator. If only 2 values are
     // passed to the method, we will assume that the operator is an equals sign
     // and keep going.
     if (func_num_args() == 2) {
         list($value, $operator) = [$operator, '='];
     }
     return $this->filter(function ($item) use($key, $operator, $value) {
         $actual = $item->{$key};
         $actual = Inflector::lower(Inflector::unaccent($actual));
         $value = Inflector::lower(Inflector::unaccent($value));
         switch ($operator) {
             case '<>':
             case '!=':
                 return sha1($actual) != sha1($value);
                 break;
             case '>':
                 return $actual > $value;
                 break;
             case '<':
                 return $actual < $value;
                 break;
             case '>=':
                 return $actual >= $value;
                 break;
             case '<=':
                 return $actual <= $value;
                 break;
             case 'between':
                 return $actual >= $value[0] && $actual <= $value[1];
                 break;
             case 'not between':
                 return $actual < $value[0] || $actual > $value[1];
                 break;
             case 'in':
                 return in_array($actual, $value);
                 break;
             case 'not in':
                 return !in_array($actual, $value);
                 break;
             case 'like':
                 $value = str_replace("'", '', $value);
                 $value = str_replace('%', '*', $value);
                 return fnmatch($value, $actual);
                 break;
             case 'not like':
                 $value = str_replace("'", '', $value);
                 $value = str_replace('%', '*', $value);
                 $check = fnmatch($value, $actual);
                 return !$check;
                 break;
             case '=':
             default:
                 return sha1($actual) == sha1($value);
                 break;
         }
     });
 }
Esempio n. 5
0
 /**
  * [orderSearch description]
  * @param  [type] $collection [description]
  * @param  [type] $pattern    [description]
  * @return [type]             [description]
  */
 private function orderSearch($collection, $pattern)
 {
     if (empty($collection)) {
         return $collection;
     }
     $newCollection = $lengths = $byItem = [];
     foreach ($collection as $item) {
         if (!isset($lengths[strlen($item['name'])])) {
             $lengths[strlen($item['name'])] = [];
         }
         $lengths[strlen($item['name'])][] = $item;
     }
     asort($lengths);
     foreach ($lengths as $length => $subColl) {
         foreach ($subColl as $k => $segment) {
             $comp = Inflector::lower(Inflector::unaccent($pattern));
             $value = Inflector::lower(Inflector::unaccent($segment['name']));
             $check = fnmatch("{$comp}*", $value);
             if ($check) {
                 $newCollection[] = $segment;
                 unset($lengths[$length][$k]);
             }
         }
     }
     foreach ($lengths as $length => $subColl) {
         foreach ($subColl as $k => $segment) {
             $newCollection[] = $segment;
         }
     }
     return $newCollection;
 }