Minor code cleanup

This commit is contained in:
Joop Schilder 2021-04-28 20:17:37 +02:00
parent f51e306cda
commit aff231bcb6
8 changed files with 37 additions and 40 deletions

View File

@ -2,6 +2,6 @@
Utility written in PHP 8.0 that scrapes a Memory QVL from the ASRock webpage and makes it searchable. It performs some
nifty tricks such as inferring the amount of RAM modules in a kit based on module size and module number. There's a
decorator enabled in [bin/app.php] which adds a hyperlink to the Tweakers.net pricewatch to the output.
decorator enabled in [bin/app.php](bin/app.php) which adds a hyperlink to the Tweakers.net pricewatch to the output.
There are two output modes available; csv and json. See [bin/app.php] for an example.
There are two output modes available; csv and json. See [bin/app.php](bin/app.php) for an example.

View File

@ -2,7 +2,7 @@
use Domain\Model\ASRockMemoryQVL;
use Domain\Model\MemoryConfiguration;
use Domain\Normalizer\Decorator\PricewatchDecorator;
use Domain\Normalizer\Decorator\TweakersPricewatchUrl;
use Domain\Normalizer\MemoryConfigurationNormalizer;
use Encoder\EncoderFactory;
use IO\Downloader;
@ -10,22 +10,25 @@ use IO\Downloader;
require_once __DIR__ . '/../vendor/autoload.php';
// setup
$list = new ASRockMemoryQVL('AMD', 'X570 Pro4', cpuFamily: 'MS');
$qualifiedVendorList = new ASRockMemoryQVL('AMD', 'X570 Pro4', cpuFamily: 'MS');
$downloader = new Downloader();
$scraper = new MemoryQVLScraper();
// scrape and filter
$page = $downloader->download($list);
$page = $downloader->download($qualifiedVendorList);
$configurations = $scraper->scrape($page);
$selection = $configurations->filter(fn(MemoryConfiguration $memory) => in_array($memory->numberOfModules, [2, 4])
$selection = $configurations->filter(static function (MemoryConfiguration $memory) {
in_array($memory->numberOfModules, [2, 4])
&& $memory->totalSize >= 16
&& $memory->speed >= 3600
&& $memory->overclockingVerified
);
&& $memory->overclockingVerified;
});
// data presentation
$normalizer = new MemoryConfigurationNormalizer();
$normalizer = PricewatchDecorator::decorate($normalizer);
$normalizer = TweakersPricewatchUrl::decorate($normalizer);
$encoderFactory = new EncoderFactory($normalizer);
$encoder = $encoderFactory->getEncoder($argv[1] ?? 'csv');
$encoding = $argv[1] ?? 'csv';
$encoder = $encoderFactory->getEncoder($encoding);
print($encoder->encode($selection));

View File

@ -4,6 +4,9 @@ namespace Domain\Model;
class MemoryConfiguration
{
private const MAX_MOTHERBOARD_SLOTS = 8;
private const OVERCLOCKING_VERIFIED = ['v', '2'];
public int $moduleSize;
public int $totalSize;
public string $totalSizeHr;
@ -26,7 +29,7 @@ class MemoryConfiguration
)
{
$this->moduleSize = filter_var($this->moduleSizeHr, FILTER_SANITIZE_NUMBER_INT);
$this->overclockingVerified = in_array($this->overclockingSupport, ['v', '2'], true);
$this->overclockingVerified = in_array($this->overclockingSupport, self::OVERCLOCKING_VERIFIED, true);
$this->dualChannelOverclockingVerified = $this->overclockingSupport === '2';
$this->deriveAdditionalFields();
@ -41,7 +44,7 @@ class MemoryConfiguration
private function findTotalSize(): void
{
// Initially 8 times the module size
$this->totalSize = 8 * $this->moduleSize;
$this->totalSize = self::MAX_MOTHERBOARD_SLOTS * $this->moduleSize;
// Work our way down to the module size...
while (!str_contains($this->module, $this->totalSize) && $this->totalSize > $this->moduleSize) {

View File

@ -4,8 +4,12 @@ namespace Domain\Normalizer\Decorator;
use Domain\Normalizer\Normalizer;
abstract class NormalizerDecorator implements Normalizer
abstract class Decorator implements Normalizer
{
abstract protected function getAdditionalHeaders(): array;
abstract public function makePass(array $data): array;
private function __construct(
private Normalizer $parent
)
@ -17,10 +21,6 @@ abstract class NormalizerDecorator implements Normalizer
return new static($normalizer);
}
abstract protected function getAdditionalHeaders(): array;
abstract public function makePass(array $normalized): array;
final public function getHeaders(): array
{
return array_merge($this->parent->getHeaders(), $this->getAdditionalHeaders());

View File

@ -2,19 +2,19 @@
namespace Domain\Normalizer\Decorator;
class PricewatchDecorator extends NormalizerDecorator
class TweakersPricewatchUrl extends Decorator
{
public function makePass(array $normalized): array
public function makePass(array $data): array
{
$module = $normalized['module'] ?? '-';
$module = $data['module'] ?? '-';
$module = $this->selectSignificantTerms($module);
$normalized['pricewatch_url'] = sprintf(
$data['pricewatch_url'] = sprintf(
'https://tweakers.net/pricewatch/zoeken/?keyword=%s',
rawurlencode($module)
);
return $normalized;
return $data;
}
protected function getAdditionalHeaders(): array

View File

@ -2,12 +2,9 @@
namespace Domain\Normalizer;
use Domain\Normalizer\Decorator\NormalizerDecorator;
interface Normalizer
{
public function getHeaders(): array;
public function normalize(object $object): array;
}

View File

@ -17,15 +17,14 @@ class CsvEncoder implements StringEncoder
public function encode(Collection $items): string
{
$items = $items->values();
$buffer = implode($this->columnSeparator, $this->normalizer->getHeaders());
$buffer .= $this->rowSeparator;
$items->each(function (object $object) use (&$buffer) {
$items->values()->each(function (object $object) use (&$buffer) {
$normalized = $this->normalizer->normalize($object);
$buffer .= collect($normalized)
->map([CsvEncoder::class, 'formatBoolean'])
->map([CsvEncoder::class, 'formatEmpty'])
->map([static::class, 'formatBoolean'])
->map([static::class, 'formatEmpty'])
->implode($this->columnSeparator);
$buffer .= $this->rowSeparator;
});

View File

@ -7,6 +7,8 @@ use Illuminate\Support\Collection;
class JsonEncoder implements StringEncoder
{
private const FLAGS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS | JSON_BIGINT_AS_STRING | JSON_THROW_ON_ERROR;
public function __construct(
private Normalizer $normalizer
)
@ -15,16 +17,9 @@ class JsonEncoder implements StringEncoder
public function encode(Collection $items): string
{
return json_encode(
[
return json_encode([
'count' => $items->count(),
'items' => $items->map([$this->normalizer, 'normalize'])->values()->toArray(),
],
JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE
| JSON_UNESCAPED_LINE_TERMINATORS
| JSON_BIGINT_AS_STRING
| JSON_THROW_ON_ERROR
);
], self::FLAGS);
}
}