├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── composer.lock ├── examples.md ├── examples.php ├── phpspec.yml ├── spec ├── .gitkeep └── Fuzzy │ └── FuzzySpec.php └── src ├── .gitkeep └── Fuzzy └── Fuzzy.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Ilya S. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fuzzy 2 | 3 | Fuzzy searching in PHP made easy! 4 | 5 | ## Installation 6 | 7 | `composer require ilya/fuzzy:~1` 8 | 9 | ## Use 10 | 11 | `array search(array $rows, string $query, integer $threshold = 3)` 12 | 13 | 14 | ```php 15 | $rows = ['f', 'fo', 'foo', 'foob', 'fooba', 'foobar']; 16 | 17 | $fuzzy = new \Fuzzy\Fuzzy; 18 | 19 | $query = 'foobar'; 20 | 21 | $fuzzy->search($rows, $query, 0); // ['foobar'] 22 | $fuzzy->search($rows, $query, 1); // ['foobar', 'fooba'] 23 | $fuzzy->search($rows, $query, 2); // ['foobar', 'fooba', 'foob'] 24 | $fuzzy->search($rows, $query, 3); // ['foobar', 'fooba', 'foob', 'foo'] 25 | ``` 26 | 27 | ## License 28 | 29 | This project is licensed under the MIT license. 30 | 31 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ilya/fuzzy", 3 | "description": "Nice PHP library for fuzzy string searching", 4 | "license": "MIT", 5 | "keywords": ["fuzzy", "string", "search"], 6 | 7 | "require-dev": { 8 | "phpspec/phpspec": "~2", 9 | "ilya/exo": "~1" 10 | }, 11 | 12 | "autoload": { 13 | "psr-4": { 14 | "Fuzzy\\": "src/Fuzzy" 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" 5 | ], 6 | "hash": "6880c46ffdd0f017c136bed6524c3c63", 7 | "packages": [ 8 | 9 | ], 10 | "packages-dev": [ 11 | { 12 | "name": "horse/horse", 13 | "version": "1.1.0", 14 | "source": { 15 | "type": "git", 16 | "url": "https://github.com/horse-php/horse.git", 17 | "reference": "e2b72508dde353de6cacfbed852aa3de3fc06eff" 18 | }, 19 | "dist": { 20 | "type": "zip", 21 | "url": "https://api.github.com/repos/horse-php/horse/zipball/e2b72508dde353de6cacfbed852aa3de3fc06eff", 22 | "reference": "e2b72508dde353de6cacfbed852aa3de3fc06eff", 23 | "shasum": "" 24 | }, 25 | "require": { 26 | "ilya/block": "~1", 27 | "symfony/console": "2.4.*" 28 | }, 29 | "require-dev": { 30 | "phpspec/phpspec": "2.0.*" 31 | }, 32 | "type": "library", 33 | "autoload": { 34 | "psr-4": { 35 | "Horse\\": "src/Horse" 36 | }, 37 | "classmap": [ 38 | "tests/DummyCommand.php" 39 | ] 40 | }, 41 | "notification-url": "https://packagist.org/downloads/", 42 | "license": [ 43 | "MIT" 44 | ], 45 | "description": "Powerful symfony/console wrapper", 46 | "keywords": [ 47 | "console", 48 | "symfony", 49 | "wrapper" 50 | ], 51 | "time": "2014-05-02 08:24:56" 52 | }, 53 | { 54 | "name": "ilya/block", 55 | "version": "1.3.0", 56 | "source": { 57 | "type": "git", 58 | "url": "https://github.com/ilya-dev/block.git", 59 | "reference": "52146cd99aa8d54f112cb30058e233e4ebbeb046" 60 | }, 61 | "dist": { 62 | "type": "zip", 63 | "url": "https://api.github.com/repos/ilya-dev/block/zipball/52146cd99aa8d54f112cb30058e233e4ebbeb046", 64 | "reference": "52146cd99aa8d54f112cb30058e233e4ebbeb046", 65 | "shasum": "" 66 | }, 67 | "require": { 68 | "php": ">=5.4.0" 69 | }, 70 | "require-dev": { 71 | "phpspec/phpspec": "~2" 72 | }, 73 | "type": "library", 74 | "autoload": { 75 | "psr-4": { 76 | "Block\\": "src/Block/" 77 | }, 78 | "files": [ 79 | "tests/Dummy.php" 80 | ] 81 | }, 82 | "notification-url": "https://packagist.org/downloads/", 83 | "license": [ 84 | "MIT" 85 | ], 86 | "authors": [ 87 | { 88 | "name": "Ilya S", 89 | "email": "public.ilya.s@gmail.com" 90 | } 91 | ], 92 | "description": "Parsing docblocks made easy", 93 | "keywords": [ 94 | "blocks", 95 | "docblocks", 96 | "docs", 97 | "documentation", 98 | "parsing", 99 | "php", 100 | "reflection" 101 | ], 102 | "time": "2014-05-01 07:33:40" 103 | }, 104 | { 105 | "name": "ilya/exo", 106 | "version": "1.0.1", 107 | "source": { 108 | "type": "git", 109 | "url": "https://github.com/ilya-dev/exo.git", 110 | "reference": "89960a0116513c3172f107041bc9b9fa80ba8e54" 111 | }, 112 | "dist": { 113 | "type": "zip", 114 | "url": "https://api.github.com/repos/ilya-dev/exo/zipball/89960a0116513c3172f107041bc9b9fa80ba8e54", 115 | "reference": "89960a0116513c3172f107041bc9b9fa80ba8e54", 116 | "shasum": "" 117 | }, 118 | "require": { 119 | "horse/horse": "~1" 120 | }, 121 | "require-dev": { 122 | "phpspec/phpspec": "2.0.*" 123 | }, 124 | "bin": [ 125 | "bin/exo" 126 | ], 127 | "type": "library", 128 | "autoload": { 129 | "psr-4": { 130 | "Exo\\": "source/Exo" 131 | } 132 | }, 133 | "notification-url": "https://packagist.org/downloads/", 134 | "license": [ 135 | "MIT" 136 | ], 137 | "description": "Save your time writing documentation.", 138 | "keywords": [ 139 | "documentation", 140 | "examples", 141 | "markdown" 142 | ], 143 | "time": "2014-05-18 16:08:01" 144 | }, 145 | { 146 | "name": "phpspec/php-diff", 147 | "version": "v1.0.2", 148 | "source": { 149 | "type": "git", 150 | "url": "https://github.com/phpspec/php-diff.git", 151 | "reference": "30e103d19519fe678ae64a60d77884ef3d71b28a" 152 | }, 153 | "dist": { 154 | "type": "zip", 155 | "url": "https://api.github.com/repos/phpspec/php-diff/zipball/30e103d19519fe678ae64a60d77884ef3d71b28a", 156 | "reference": "30e103d19519fe678ae64a60d77884ef3d71b28a", 157 | "shasum": "" 158 | }, 159 | "type": "library", 160 | "autoload": { 161 | "psr-0": { 162 | "Diff": "lib/" 163 | } 164 | }, 165 | "notification-url": "https://packagist.org/downloads/", 166 | "license": [ 167 | "BSD-3-Clause" 168 | ], 169 | "authors": [ 170 | { 171 | "name": "Chris Boulton", 172 | "homepage": "http://github.com/chrisboulton" 173 | } 174 | ], 175 | "description": "A comprehensive library for generating differences between two hashable objects (strings or arrays).", 176 | "time": "2013-11-01 13:02:21" 177 | }, 178 | { 179 | "name": "phpspec/phpspec", 180 | "version": "2.0.0", 181 | "source": { 182 | "type": "git", 183 | "url": "https://github.com/phpspec/phpspec.git", 184 | "reference": "1aade5766ddf4f28fdcf0a34d6ed642393d6c43d" 185 | }, 186 | "dist": { 187 | "type": "zip", 188 | "url": "https://api.github.com/repos/phpspec/phpspec/zipball/1aade5766ddf4f28fdcf0a34d6ed642393d6c43d", 189 | "reference": "1aade5766ddf4f28fdcf0a34d6ed642393d6c43d", 190 | "shasum": "" 191 | }, 192 | "require": { 193 | "php": ">=5.3.3", 194 | "phpspec/php-diff": "~1.0.0", 195 | "phpspec/prophecy": "~1.1", 196 | "symfony/console": "~2.1", 197 | "symfony/event-dispatcher": "~2.1", 198 | "symfony/finder": "~2.1", 199 | "symfony/yaml": "~2.1" 200 | }, 201 | "require-dev": { 202 | "behat/behat": "~2.5", 203 | "bossa/phpspec2-expect": "dev-master", 204 | "symfony/filesystem": "~2.1" 205 | }, 206 | "suggest": { 207 | "phpspec/nyan-formatters": "~1.0 – Adds Nyan formatters" 208 | }, 209 | "bin": [ 210 | "bin/phpspec" 211 | ], 212 | "type": "library", 213 | "extra": { 214 | "branch-alias": { 215 | "dev-master": "2.0.x-dev" 216 | } 217 | }, 218 | "autoload": { 219 | "psr-0": { 220 | "PhpSpec": "src/" 221 | } 222 | }, 223 | "notification-url": "https://packagist.org/downloads/", 224 | "license": [ 225 | "MIT" 226 | ], 227 | "authors": [ 228 | { 229 | "name": "Konstantin Kudryashov", 230 | "email": "ever.zet@gmail.com", 231 | "homepage": "http://everzet.com" 232 | }, 233 | { 234 | "name": "Marcello Duarte", 235 | "homepage": "http://marcelloduarte.net/" 236 | } 237 | ], 238 | "description": "Specification-oriented BDD framework for PHP 5.3+", 239 | "homepage": "http://phpspec.net/", 240 | "keywords": [ 241 | "BDD", 242 | "SpecBDD", 243 | "TDD", 244 | "spec", 245 | "specification", 246 | "testing", 247 | "tests" 248 | ], 249 | "time": "2014-03-19 14:23:43" 250 | }, 251 | { 252 | "name": "phpspec/prophecy", 253 | "version": "1.1.2", 254 | "source": { 255 | "type": "git", 256 | "url": "https://github.com/phpspec/prophecy.git", 257 | "reference": "976a65af02a2a0e17ce6c949f7b43437205628bb" 258 | }, 259 | "dist": { 260 | "type": "zip", 261 | "url": "https://api.github.com/repos/phpspec/prophecy/zipball/976a65af02a2a0e17ce6c949f7b43437205628bb", 262 | "reference": "976a65af02a2a0e17ce6c949f7b43437205628bb", 263 | "shasum": "" 264 | }, 265 | "require-dev": { 266 | "phpspec/phpspec": "2.0.*" 267 | }, 268 | "type": "library", 269 | "extra": { 270 | "branch-alias": { 271 | "dev-master": "1.1.x-dev" 272 | } 273 | }, 274 | "autoload": { 275 | "psr-0": { 276 | "Prophecy\\": "src/" 277 | } 278 | }, 279 | "notification-url": "https://packagist.org/downloads/", 280 | "license": [ 281 | "MIT" 282 | ], 283 | "authors": [ 284 | { 285 | "name": "Konstantin Kudryashov", 286 | "email": "ever.zet@gmail.com", 287 | "homepage": "http://everzet.com" 288 | }, 289 | { 290 | "name": "Marcello Duarte", 291 | "email": "marcello.duarte@gmail.com" 292 | } 293 | ], 294 | "description": "Highly opinionated mocking framework for PHP 5.3+", 295 | "homepage": "http://phpspec.org", 296 | "keywords": [ 297 | "Double", 298 | "Dummy", 299 | "fake", 300 | "mock", 301 | "spy", 302 | "stub" 303 | ], 304 | "time": "2014-01-24 11:03:43" 305 | }, 306 | { 307 | "name": "symfony/console", 308 | "version": "v2.4.4", 309 | "target-dir": "Symfony/Component/Console", 310 | "source": { 311 | "type": "git", 312 | "url": "https://github.com/symfony/Console.git", 313 | "reference": "2e452005b1e1d003d23702d227e23614679eb5ca" 314 | }, 315 | "dist": { 316 | "type": "zip", 317 | "url": "https://api.github.com/repos/symfony/Console/zipball/2e452005b1e1d003d23702d227e23614679eb5ca", 318 | "reference": "2e452005b1e1d003d23702d227e23614679eb5ca", 319 | "shasum": "" 320 | }, 321 | "require": { 322 | "php": ">=5.3.3" 323 | }, 324 | "require-dev": { 325 | "symfony/event-dispatcher": "~2.1" 326 | }, 327 | "suggest": { 328 | "symfony/event-dispatcher": "" 329 | }, 330 | "type": "library", 331 | "extra": { 332 | "branch-alias": { 333 | "dev-master": "2.4-dev" 334 | } 335 | }, 336 | "autoload": { 337 | "psr-0": { 338 | "Symfony\\Component\\Console\\": "" 339 | } 340 | }, 341 | "notification-url": "https://packagist.org/downloads/", 342 | "license": [ 343 | "MIT" 344 | ], 345 | "authors": [ 346 | { 347 | "name": "Fabien Potencier", 348 | "email": "fabien@symfony.com", 349 | "homepage": "http://fabien.potencier.org", 350 | "role": "Lead Developer" 351 | }, 352 | { 353 | "name": "Symfony Community", 354 | "homepage": "http://symfony.com/contributors" 355 | } 356 | ], 357 | "description": "Symfony Console Component", 358 | "homepage": "http://symfony.com", 359 | "time": "2014-04-27 13:34:57" 360 | }, 361 | { 362 | "name": "symfony/event-dispatcher", 363 | "version": "v2.4.4", 364 | "target-dir": "Symfony/Component/EventDispatcher", 365 | "source": { 366 | "type": "git", 367 | "url": "https://github.com/symfony/EventDispatcher.git", 368 | "reference": "e539602e5455aa086c0e81e604745af7789e4d8a" 369 | }, 370 | "dist": { 371 | "type": "zip", 372 | "url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/e539602e5455aa086c0e81e604745af7789e4d8a", 373 | "reference": "e539602e5455aa086c0e81e604745af7789e4d8a", 374 | "shasum": "" 375 | }, 376 | "require": { 377 | "php": ">=5.3.3" 378 | }, 379 | "require-dev": { 380 | "symfony/dependency-injection": "~2.0" 381 | }, 382 | "suggest": { 383 | "symfony/dependency-injection": "", 384 | "symfony/http-kernel": "" 385 | }, 386 | "type": "library", 387 | "extra": { 388 | "branch-alias": { 389 | "dev-master": "2.4-dev" 390 | } 391 | }, 392 | "autoload": { 393 | "psr-0": { 394 | "Symfony\\Component\\EventDispatcher\\": "" 395 | } 396 | }, 397 | "notification-url": "https://packagist.org/downloads/", 398 | "license": [ 399 | "MIT" 400 | ], 401 | "authors": [ 402 | { 403 | "name": "Fabien Potencier", 404 | "email": "fabien@symfony.com", 405 | "homepage": "http://fabien.potencier.org", 406 | "role": "Lead Developer" 407 | }, 408 | { 409 | "name": "Symfony Community", 410 | "homepage": "http://symfony.com/contributors" 411 | } 412 | ], 413 | "description": "Symfony EventDispatcher Component", 414 | "homepage": "http://symfony.com", 415 | "time": "2014-04-16 10:34:31" 416 | }, 417 | { 418 | "name": "symfony/finder", 419 | "version": "v2.4.4", 420 | "target-dir": "Symfony/Component/Finder", 421 | "source": { 422 | "type": "git", 423 | "url": "https://github.com/symfony/Finder.git", 424 | "reference": "25e1e7d5e7376f8a92ae3b1d714d956edf33a730" 425 | }, 426 | "dist": { 427 | "type": "zip", 428 | "url": "https://api.github.com/repos/symfony/Finder/zipball/25e1e7d5e7376f8a92ae3b1d714d956edf33a730", 429 | "reference": "25e1e7d5e7376f8a92ae3b1d714d956edf33a730", 430 | "shasum": "" 431 | }, 432 | "require": { 433 | "php": ">=5.3.3" 434 | }, 435 | "type": "library", 436 | "extra": { 437 | "branch-alias": { 438 | "dev-master": "2.4-dev" 439 | } 440 | }, 441 | "autoload": { 442 | "psr-0": { 443 | "Symfony\\Component\\Finder\\": "" 444 | } 445 | }, 446 | "notification-url": "https://packagist.org/downloads/", 447 | "license": [ 448 | "MIT" 449 | ], 450 | "authors": [ 451 | { 452 | "name": "Fabien Potencier", 453 | "email": "fabien@symfony.com", 454 | "homepage": "http://fabien.potencier.org", 455 | "role": "Lead Developer" 456 | }, 457 | { 458 | "name": "Symfony Community", 459 | "homepage": "http://symfony.com/contributors" 460 | } 461 | ], 462 | "description": "Symfony Finder Component", 463 | "homepage": "http://symfony.com", 464 | "time": "2014-04-27 13:34:57" 465 | }, 466 | { 467 | "name": "symfony/yaml", 468 | "version": "v2.4.4", 469 | "target-dir": "Symfony/Component/Yaml", 470 | "source": { 471 | "type": "git", 472 | "url": "https://github.com/symfony/Yaml.git", 473 | "reference": "65539ecde838f9c0d18b006b2101e3deb4b5c9ff" 474 | }, 475 | "dist": { 476 | "type": "zip", 477 | "url": "https://api.github.com/repos/symfony/Yaml/zipball/65539ecde838f9c0d18b006b2101e3deb4b5c9ff", 478 | "reference": "65539ecde838f9c0d18b006b2101e3deb4b5c9ff", 479 | "shasum": "" 480 | }, 481 | "require": { 482 | "php": ">=5.3.3" 483 | }, 484 | "type": "library", 485 | "extra": { 486 | "branch-alias": { 487 | "dev-master": "2.4-dev" 488 | } 489 | }, 490 | "autoload": { 491 | "psr-0": { 492 | "Symfony\\Component\\Yaml\\": "" 493 | } 494 | }, 495 | "notification-url": "https://packagist.org/downloads/", 496 | "license": [ 497 | "MIT" 498 | ], 499 | "authors": [ 500 | { 501 | "name": "Fabien Potencier", 502 | "email": "fabien@symfony.com", 503 | "homepage": "http://fabien.potencier.org", 504 | "role": "Lead Developer" 505 | }, 506 | { 507 | "name": "Symfony Community", 508 | "homepage": "http://symfony.com/contributors" 509 | } 510 | ], 511 | "description": "Symfony Yaml Component", 512 | "homepage": "http://symfony.com", 513 | "time": "2014-04-18 20:37:09" 514 | } 515 | ], 516 | "aliases": [ 517 | 518 | ], 519 | "minimum-stability": "stable", 520 | "stability-flags": [ 521 | 522 | ], 523 | "platform": [ 524 | 525 | ], 526 | "platform-dev": [ 527 | 528 | ] 529 | } 530 | -------------------------------------------------------------------------------- /examples.md: -------------------------------------------------------------------------------- 1 | # Examples 2 | 3 | ## Example #1 4 | 5 | ```php 6 | $rows = ['f', 'fo', 'foo', 'foob', 'fooba', 'foobar']; 7 | $fuzzy = new Fuzzy\Fuzzy; 8 | $query = 'foobar'; 9 | $fuzzy->search($rows, $query, 0); 10 | $fuzzy->search($rows, $query, 1); 11 | $fuzzy->search($rows, $query, 2); 12 | $fuzzy->search($rows, $query, 3); 13 | ``` 14 | ``` 15 | array ( 16 | 0 => 'f', 17 | 1 => 'fo', 18 | 2 => 'foo', 19 | 3 => 'foob', 20 | 4 => 'fooba', 21 | 5 => 'foobar', 22 | ) 23 | [object:Fuzzy\Fuzzy:000000003fe2a3020000000070efdac0] 24 | 'foobar' 25 | array ( 26 | 0 => 'foobar', 27 | ) 28 | array ( 29 | 0 => 'foobar', 30 | 1 => 'fooba', 31 | ) 32 | array ( 33 | 0 => 'foobar', 34 | 1 => 'fooba', 35 | 2 => 'foob', 36 | ) 37 | array ( 38 | 0 => 'foobar', 39 | 1 => 'fooba', 40 | 2 => 'foob', 41 | 3 => 'foo', 42 | ) 43 | ``` 44 | -------------------------------------------------------------------------------- /examples.php: -------------------------------------------------------------------------------- 1 | search($rows, $query, 0); 7 | $fuzzy->search($rows, $query, 1); 8 | $fuzzy->search($rows, $query, 2); 9 | $fuzzy->search($rows, $query, 3); 10 | 11 | -------------------------------------------------------------------------------- /phpspec.yml: -------------------------------------------------------------------------------- 1 | formatter.name: pretty 2 | -------------------------------------------------------------------------------- /spec/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilya-dev/fuzzy/98c85c251fced67015f3cf6356a2b28b58e65ac8/spec/.gitkeep -------------------------------------------------------------------------------- /spec/Fuzzy/FuzzySpec.php: -------------------------------------------------------------------------------- 1 | shouldHaveType('Fuzzy\Fuzzy'); 10 | } 11 | 12 | function it_performs_fuzzy_string_searching() 13 | { 14 | $rows = ['castToArr', 'cast', 'castToArray', 'toArr', 'toArray', 'castTo']; 15 | 16 | $this->search($rows, 'castToArray', 0)->shouldReturn([ 17 | 'castToArray' 18 | ]); 19 | 20 | $this->search($rows, 'castToArray', 3)->shouldReturn([ 21 | 'castToArray', 'castToArr' 22 | ]); 23 | 24 | $this->search($rows, 'castToArray', 5)->shouldReturn([ 25 | 'castToArray', 'castToArr', 'toArray', 'castTo' 26 | ]); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ilya-dev/fuzzy/98c85c251fced67015f3cf6356a2b28b58e65ac8/src/.gitkeep -------------------------------------------------------------------------------- /src/Fuzzy/Fuzzy.php: -------------------------------------------------------------------------------- 1 | calculateDistance($query, $row); 20 | 21 | if ($threshold >= $distance) 22 | { 23 | $matched[] = [$distance, $row]; 24 | } 25 | } 26 | 27 | return $this->transformResult($this->sortMatchedStrings($matched)); 28 | } 29 | 30 | /** 31 | * Calculate the Levenshtein distance between two strings. 32 | * 33 | * @param string $one 34 | * @param string $two 35 | * @return int 36 | */ 37 | protected function calculateDistance($one, $two) 38 | { 39 | return levenshtein($one, $two); 40 | } 41 | 42 | /** 43 | * Sort the matched strings. 44 | * 45 | * @param array $matched 46 | * @return array 47 | */ 48 | protected function sortMatchedStrings(array $matched) 49 | { 50 | usort($matched, function(array $left, array $right) 51 | { 52 | return ($left[0] - $right[0]); 53 | }); 54 | 55 | return $matched; 56 | } 57 | 58 | /** 59 | * Transform a given array of matches. 60 | * 61 | * @param array $matched 62 | * @return array 63 | */ 64 | protected function transformResult(array $matched) 65 | { 66 | $iterator = function(array $element) 67 | { 68 | return $element[1]; 69 | }; 70 | 71 | return array_map($iterator, $matched); 72 | } 73 | 74 | } 75 | --------------------------------------------------------------------------------