Minor code cleanup
This commit is contained in:
parent
f51e306cda
commit
aff231bcb6
@ -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
|
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
|
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.
|
||||||
|
19
bin/app.php
19
bin/app.php
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use Domain\Model\ASRockMemoryQVL;
|
use Domain\Model\ASRockMemoryQVL;
|
||||||
use Domain\Model\MemoryConfiguration;
|
use Domain\Model\MemoryConfiguration;
|
||||||
use Domain\Normalizer\Decorator\PricewatchDecorator;
|
use Domain\Normalizer\Decorator\TweakersPricewatchUrl;
|
||||||
use Domain\Normalizer\MemoryConfigurationNormalizer;
|
use Domain\Normalizer\MemoryConfigurationNormalizer;
|
||||||
use Encoder\EncoderFactory;
|
use Encoder\EncoderFactory;
|
||||||
use IO\Downloader;
|
use IO\Downloader;
|
||||||
@ -10,22 +10,25 @@ use IO\Downloader;
|
|||||||
require_once __DIR__ . '/../vendor/autoload.php';
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
// setup
|
// setup
|
||||||
$list = new ASRockMemoryQVL('AMD', 'X570 Pro4', cpuFamily: 'MS');
|
$qualifiedVendorList = new ASRockMemoryQVL('AMD', 'X570 Pro4', cpuFamily: 'MS');
|
||||||
$downloader = new Downloader();
|
$downloader = new Downloader();
|
||||||
$scraper = new MemoryQVLScraper();
|
$scraper = new MemoryQVLScraper();
|
||||||
|
|
||||||
// scrape and filter
|
// scrape and filter
|
||||||
$page = $downloader->download($list);
|
$page = $downloader->download($qualifiedVendorList);
|
||||||
$configurations = $scraper->scrape($page);
|
$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->totalSize >= 16
|
||||||
&& $memory->speed >= 3600
|
&& $memory->speed >= 3600
|
||||||
&& $memory->overclockingVerified
|
&& $memory->overclockingVerified;
|
||||||
);
|
});
|
||||||
|
|
||||||
// data presentation
|
// data presentation
|
||||||
$normalizer = new MemoryConfigurationNormalizer();
|
$normalizer = new MemoryConfigurationNormalizer();
|
||||||
$normalizer = PricewatchDecorator::decorate($normalizer);
|
$normalizer = TweakersPricewatchUrl::decorate($normalizer);
|
||||||
$encoderFactory = new EncoderFactory($normalizer);
|
$encoderFactory = new EncoderFactory($normalizer);
|
||||||
$encoder = $encoderFactory->getEncoder($argv[1] ?? 'csv');
|
|
||||||
|
$encoding = $argv[1] ?? 'csv';
|
||||||
|
$encoder = $encoderFactory->getEncoder($encoding);
|
||||||
print($encoder->encode($selection));
|
print($encoder->encode($selection));
|
||||||
|
@ -4,6 +4,9 @@ namespace Domain\Model;
|
|||||||
|
|
||||||
class MemoryConfiguration
|
class MemoryConfiguration
|
||||||
{
|
{
|
||||||
|
private const MAX_MOTHERBOARD_SLOTS = 8;
|
||||||
|
private const OVERCLOCKING_VERIFIED = ['v', '2'];
|
||||||
|
|
||||||
public int $moduleSize;
|
public int $moduleSize;
|
||||||
public int $totalSize;
|
public int $totalSize;
|
||||||
public string $totalSizeHr;
|
public string $totalSizeHr;
|
||||||
@ -26,7 +29,7 @@ class MemoryConfiguration
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
$this->moduleSize = filter_var($this->moduleSizeHr, FILTER_SANITIZE_NUMBER_INT);
|
$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->dualChannelOverclockingVerified = $this->overclockingSupport === '2';
|
||||||
|
|
||||||
$this->deriveAdditionalFields();
|
$this->deriveAdditionalFields();
|
||||||
@ -41,7 +44,7 @@ class MemoryConfiguration
|
|||||||
private function findTotalSize(): void
|
private function findTotalSize(): void
|
||||||
{
|
{
|
||||||
// Initially 8 times the module size
|
// 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...
|
// Work our way down to the module size...
|
||||||
while (!str_contains($this->module, $this->totalSize) && $this->totalSize > $this->moduleSize) {
|
while (!str_contains($this->module, $this->totalSize) && $this->totalSize > $this->moduleSize) {
|
||||||
|
@ -4,8 +4,12 @@ namespace Domain\Normalizer\Decorator;
|
|||||||
|
|
||||||
use Domain\Normalizer\Normalizer;
|
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 function __construct(
|
||||||
private Normalizer $parent
|
private Normalizer $parent
|
||||||
)
|
)
|
||||||
@ -17,10 +21,6 @@ abstract class NormalizerDecorator implements Normalizer
|
|||||||
return new static($normalizer);
|
return new static($normalizer);
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract protected function getAdditionalHeaders(): array;
|
|
||||||
|
|
||||||
abstract public function makePass(array $normalized): array;
|
|
||||||
|
|
||||||
final public function getHeaders(): array
|
final public function getHeaders(): array
|
||||||
{
|
{
|
||||||
return array_merge($this->parent->getHeaders(), $this->getAdditionalHeaders());
|
return array_merge($this->parent->getHeaders(), $this->getAdditionalHeaders());
|
@ -2,19 +2,19 @@
|
|||||||
|
|
||||||
namespace Domain\Normalizer\Decorator;
|
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);
|
$module = $this->selectSignificantTerms($module);
|
||||||
|
|
||||||
$normalized['pricewatch_url'] = sprintf(
|
$data['pricewatch_url'] = sprintf(
|
||||||
'https://tweakers.net/pricewatch/zoeken/?keyword=%s',
|
'https://tweakers.net/pricewatch/zoeken/?keyword=%s',
|
||||||
rawurlencode($module)
|
rawurlencode($module)
|
||||||
);
|
);
|
||||||
|
|
||||||
return $normalized;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getAdditionalHeaders(): array
|
protected function getAdditionalHeaders(): array
|
@ -2,12 +2,9 @@
|
|||||||
|
|
||||||
namespace Domain\Normalizer;
|
namespace Domain\Normalizer;
|
||||||
|
|
||||||
use Domain\Normalizer\Decorator\NormalizerDecorator;
|
|
||||||
|
|
||||||
interface Normalizer
|
interface Normalizer
|
||||||
{
|
{
|
||||||
public function getHeaders(): array;
|
public function getHeaders(): array;
|
||||||
|
|
||||||
public function normalize(object $object): array;
|
public function normalize(object $object): array;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,15 +17,14 @@ class CsvEncoder implements StringEncoder
|
|||||||
|
|
||||||
public function encode(Collection $items): string
|
public function encode(Collection $items): string
|
||||||
{
|
{
|
||||||
$items = $items->values();
|
|
||||||
$buffer = implode($this->columnSeparator, $this->normalizer->getHeaders());
|
$buffer = implode($this->columnSeparator, $this->normalizer->getHeaders());
|
||||||
$buffer .= $this->rowSeparator;
|
$buffer .= $this->rowSeparator;
|
||||||
|
|
||||||
$items->each(function (object $object) use (&$buffer) {
|
$items->values()->each(function (object $object) use (&$buffer) {
|
||||||
$normalized = $this->normalizer->normalize($object);
|
$normalized = $this->normalizer->normalize($object);
|
||||||
$buffer .= collect($normalized)
|
$buffer .= collect($normalized)
|
||||||
->map([CsvEncoder::class, 'formatBoolean'])
|
->map([static::class, 'formatBoolean'])
|
||||||
->map([CsvEncoder::class, 'formatEmpty'])
|
->map([static::class, 'formatEmpty'])
|
||||||
->implode($this->columnSeparator);
|
->implode($this->columnSeparator);
|
||||||
$buffer .= $this->rowSeparator;
|
$buffer .= $this->rowSeparator;
|
||||||
});
|
});
|
||||||
|
@ -7,6 +7,8 @@ use Illuminate\Support\Collection;
|
|||||||
|
|
||||||
class JsonEncoder implements StringEncoder
|
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(
|
public function __construct(
|
||||||
private Normalizer $normalizer
|
private Normalizer $normalizer
|
||||||
)
|
)
|
||||||
@ -15,16 +17,9 @@ class JsonEncoder implements StringEncoder
|
|||||||
|
|
||||||
public function encode(Collection $items): string
|
public function encode(Collection $items): string
|
||||||
{
|
{
|
||||||
return json_encode(
|
return json_encode([
|
||||||
[
|
|
||||||
'count' => $items->count(),
|
'count' => $items->count(),
|
||||||
'items' => $items->map([$this->normalizer, 'normalize'])->values()->toArray(),
|
'items' => $items->map([$this->normalizer, 'normalize'])->values()->toArray(),
|
||||||
],
|
], self::FLAGS);
|
||||||
JSON_UNESCAPED_SLASHES
|
|
||||||
| JSON_UNESCAPED_UNICODE
|
|
||||||
| JSON_UNESCAPED_LINE_TERMINATORS
|
|
||||||
| JSON_BIGINT_AS_STRING
|
|
||||||
| JSON_THROW_ON_ERROR
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user