-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ENHANCEMENT: Upped phpstan * MINOR: Upped phpstan * MINOR: Whitespace * FIX: PHPStan now passes * WIP: Refactoring, but sleepy. Park until tomorrow * FIX: Coding standards and tests pass locally
- Loading branch information
1 parent
76df748
commit 7115c30
Showing
19 changed files
with
223 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* Created by PhpStorm. | ||
* User: gordon | ||
* Date: 25/3/2561 | ||
* Time: 17:01 น. | ||
*/ | ||
|
||
namespace Suilven\FreeTextSearch\Helper; | ||
|
||
use SilverStripe\ORM\DataObject; | ||
|
||
class SearchHelper | ||
{ | ||
/** | ||
* @param \SilverStripe\ORM\DataObject $dataObject the dataobject to extra text from | ||
* @return array<string,array<string,string>> | ||
*/ | ||
public function getTextFieldPayload(DataObject $dataObject): array | ||
{ | ||
$helper = new IndexingHelper(); | ||
$fullPayload = $helper->getFieldsToIndex($dataObject); | ||
|
||
$textPayload = []; | ||
|
||
$keys = \array_keys($fullPayload); | ||
$specsHelper = new SpecsHelper(); | ||
|
||
foreach ($keys as $key) { | ||
if ($fullPayload[$key] === []) { | ||
continue; | ||
} | ||
|
||
$textPayload[$key] = []; | ||
$specs = $specsHelper->getFieldSpecs($key); | ||
|
||
foreach (\array_keys($specs) as $field) { | ||
// skip link field | ||
if ($field === 'Link') { | ||
continue; | ||
} | ||
$type = $specs[$field]; | ||
if (!\in_array($type, ['Varchar', 'HTMLText'], true)) { | ||
continue; | ||
} | ||
|
||
$textPayload[$key][$field] = (string) $fullPayload[$key][$field]; | ||
} | ||
} | ||
|
||
return $textPayload; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* Created by PhpStorm. | ||
* User: gordon | ||
* Date: 25/3/2561 | ||
* Time: 17:01 น. | ||
*/ | ||
|
||
namespace Suilven\FreeTextSearch\Helper; | ||
|
||
use SilverStripe\ORM\DataObjectSchema; | ||
use Suilven\FreeTextSearch\Indexes; | ||
|
||
class SpecsHelper | ||
{ | ||
/** | ||
* Helper method to get get field specs for a DataObject relevant to it's index definition | ||
* | ||
* @param string $indexName the name of the index | ||
* @return array<string,string> | ||
*/ | ||
public function getFieldSpecs(string $indexName): array | ||
{ | ||
$indexes = new Indexes(); | ||
$index = $indexes->getIndex($indexName); | ||
$singleton = \singleton((string)($index->getClass())); | ||
|
||
$helper = new IndexingHelper(); | ||
$fields = $helper->getFields($indexName); | ||
|
||
/** @var \SilverStripe\ORM\DataObjectSchema $schema */ | ||
$schema = $singleton->getSchema(); | ||
$specs = $schema->fieldSpecs((string) $index->getClass(), DataObjectSchema::INCLUDE_CLASS); | ||
|
||
/** @var array<string,string> $filteredSpecs the DB specs for fields related to the index */ | ||
$filteredSpecs = []; | ||
|
||
foreach ($fields as $field) { | ||
if ($field === 'Link') { | ||
continue; | ||
} | ||
$fieldType = $specs[$field]; | ||
|
||
// fix likes of varchar(255) | ||
$fieldType = \explode('(', $fieldType)[0]; | ||
|
||
// remove the class name | ||
$fieldType = \explode('.', $fieldType)[1]; | ||
|
||
$filteredSpecs[$field] = $fieldType; | ||
} | ||
|
||
// if Link undefined in the original index specs, add it if the method exists on the singleton dataobject | ||
if (!isset($filteredSpecs['Link'])) { | ||
if (\method_exists($singleton, 'Link')) { | ||
$filteredSpecs['Link'] = 'Varchar'; | ||
} | ||
} | ||
|
||
return $filteredSpecs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.