From 3cafb318dcb99fa190c622104a693804ad4d087e Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 11 Jun 2024 21:03:27 -0400 Subject: [PATCH] Use auto IDs; drop to PHP 8.2 --- src/app-config.php | 3 ++- src/composer.json | 1 + src/composer.lock | 11 ++++++----- src/lib/Feed.php | 15 ++++++--------- src/lib/Item.php | 21 +++++++++------------ src/lib/ItemWithFeed.php | 4 ++-- src/lib/Key.php | 6 +++--- src/lib/ParsedFeed.php | 8 ++++---- src/lib/Security.php | 16 +++++++--------- src/lib/Table.php | 6 +++--- src/lib/User.php | 7 ++----- 11 files changed, 45 insertions(+), 53 deletions(-) diff --git a/src/app-config.php b/src/app-config.php index 21fdabb..6f7e106 100644 --- a/src/app-config.php +++ b/src/app-config.php @@ -2,7 +2,7 @@ /** The current Feed Reader Central version */ -use BitBadger\PDODocument\Configuration; +use BitBadger\PDODocument\{AutoId, Configuration}; use FeedReaderCentral\Data; const FRC_VERSION = '1.0.0-beta1'; @@ -27,6 +27,7 @@ require __DIR__ . '/vendor/autoload.php'; require 'user-config.php'; Configuration::$pdoDSN = 'sqlite:' . implode(DIRECTORY_SEPARATOR, [__DIR__, 'data', DATABASE_NAME]); +Configuration::$autoId = AutoId::Number; Data::ensureDb(); /** @var string The date the world wide web was created */ diff --git a/src/composer.json b/src/composer.json index 6eab05b..298c72f 100644 --- a/src/composer.json +++ b/src/composer.json @@ -2,6 +2,7 @@ "name": "bit-badger/feed-reader-central", "minimum-stability": "dev", "require": { + "php": ">=8.2", "bit-badger/pdo-document": "^1", "ext-curl": "*", "ext-dom": "*", diff --git a/src/composer.lock b/src/composer.lock index 32c046f..a51a4d6 100644 --- a/src/composer.lock +++ b/src/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f074a197e429ac24507becc14e0d99c3", + "content-hash": "4f0add59e384feb6787acf3685c9e031", "packages": [ { "name": "bit-badger/pdo-document", - "version": "v1.0.0-alpha1", + "version": "v1.0.0-alpha2", "source": { "type": "git", "url": "https://git.bitbadger.solutions/bit-badger/pdo-document", - "reference": "f784f3e52cc1e4691fa347eefc82a2e4587c7f38" + "reference": "330e27218756df8b93081a17dead8aaec789b071" }, "require": { "ext-pdo": "*", "netresearch/jsonmapper": "^4", - "php": ">=8.3" + "php": ">=8.2" }, "require-dev": { "phpunit/phpunit": "^11" @@ -54,7 +54,7 @@ "rss": "https://git.bitbadger.solutions/bit-badger/pdo-document.rss", "source": "https://git.bitbadger.solutions/bit-badger/pdo-document" }, - "time": "2024-06-08T23:58:45+00:00" + "time": "2024-06-11T11:07:56+00:00" }, { "name": "netresearch/jsonmapper", @@ -115,6 +115,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { + "php": ">=8.2", "ext-curl": "*", "ext-dom": "*", "ext-pdo": "*", diff --git a/src/lib/Feed.php b/src/lib/Feed.php index d6445d7..f24ed87 100644 --- a/src/lib/Feed.php +++ b/src/lib/Feed.php @@ -3,7 +3,7 @@ namespace FeedReaderCentral; use BitBadger\PDODocument\{ - Configuration, Custom, DocumentException, DocumentList, Exists, Field, Find, Parameters, Patch, Query + Configuration, Custom, Document, DocumentException, DocumentList, Exists, Field, Find, Parameters, Patch, Query }; use DateTimeInterface; @@ -15,16 +15,16 @@ class Feed // ***** CONSTANTS ***** /** @var int Do not purge items */ - public const int PURGE_NONE = 0; + public const PURGE_NONE = 0; /** @var int Purge all read items (will not purge unread items) */ - public const int PURGE_READ = 1; + public const PURGE_READ = 1; /** @var int Purge items older than the specified number of days */ - public const int PURGE_BY_DAYS = 2; + public const PURGE_BY_DAYS = 2; /** @var int Purge items in number greater than the specified number of items to keep */ - public const int PURGE_BY_COUNT = 3; + public const PURGE_BY_COUNT = 3; /** * Constructor @@ -187,10 +187,7 @@ class Feed return ['error' => "Already subscribed to feed $feed->url"]; } - Custom::nonQuery(<<<'SQL' - INSERT INTO feed (data) - VALUES (json_set(:data, '$.id', (SELECT coalesce(max(data->>'id'), 0) + 1 FROM feed))) - SQL, Parameters::json(':data', self::fromParsed($feed))); + Document::insert(Table::FEED, self::fromParsed($feed)); $doc = Find::firstByFields(Table::FEED, $fields, static::class); if (!$doc) return ['error' => 'Could not retrieve inserted feed']; diff --git a/src/lib/Item.php b/src/lib/Item.php index ecbd7e2..757d1cb 100644 --- a/src/lib/Item.php +++ b/src/lib/Item.php @@ -2,7 +2,7 @@ namespace FeedReaderCentral; -use BitBadger\PDODocument\{Custom, DocumentException, Parameters, Patch}; +use BitBadger\PDODocument\{Document, DocumentException, Patch}; /** * An item from a feed @@ -57,17 +57,14 @@ class Item */ public static function add(int $feedId, ParsedItem $parsed): void { - Custom::nonQuery(<<<'SQL' - INSERT INTO item (data) - VALUES (json_set(:data, '$.id', (SELECT coalesce(max(data->>'id'), 0) + 1 FROM item))) - SQL, Parameters::json(':data', new static( - feed_id: $feedId, - title: $parsed->title, - item_guid: $parsed->guid, - item_link: $parsed->link, - published_on: $parsed->publishedOn, - updated_on: $parsed->updatedOn, - content: $parsed->content))); + Document::insert(Table::ITEM, new static( + feed_id: $feedId, + title: $parsed->title, + item_guid: $parsed->guid, + item_link: $parsed->link, + published_on: $parsed->publishedOn, + updated_on: $parsed->updatedOn, + content: $parsed->content)); } /** diff --git a/src/lib/ItemWithFeed.php b/src/lib/ItemWithFeed.php index c7f7ec5..602626b 100644 --- a/src/lib/ItemWithFeed.php +++ b/src/lib/ItemWithFeed.php @@ -11,11 +11,11 @@ use BitBadger\PDODocument\Mapper\{DocumentMapper, ExistsMapper}; class ItemWithFeed extends Item { /** @var string The body of the `FROM` clause to join item and feed */ - public const string FROM_WITH_JOIN = Table::ITEM . ' INNER JOIN ' . Table::FEED + public const FROM_WITH_JOIN = Table::ITEM . ' INNER JOIN ' . Table::FEED . ' ON ' . Table::ITEM . ".data->>'feed_id' = " . Table::FEED . ".data->>'id'"; /** @var string The `SELECT` clause to add the feed as a property to the item's document */ - public const string SELECT_WITH_FEED = + public const SELECT_WITH_FEED = 'SELECT json_set(' . Table::ITEM . ".data, '$.feed', json(" . Table::FEED . '.data)) AS data FROM ' . self::FROM_WITH_JOIN; diff --git a/src/lib/Key.php b/src/lib/Key.php index 137e848..d68ba38 100644 --- a/src/lib/Key.php +++ b/src/lib/Key.php @@ -8,11 +8,11 @@ namespace FeedReaderCentral; class Key { /** @var string The $_SESSION key for the current user's e-mail address */ - public const string USER_EMAIL = 'FRC_USER_EMAIL'; + public const USER_EMAIL = 'FRC_USER_EMAIL'; /** @var string The $_SESSION key for the current user's ID */ - public const string USER_ID = 'FRC_USER_ID'; + public const USER_ID = 'FRC_USER_ID'; /** @var string The $_REQUEST key for the array of user messages to display */ - public const string USER_MSG = 'FRC_USER_MSG'; + public const USER_MSG = 'FRC_USER_MSG'; } diff --git a/src/lib/ParsedFeed.php b/src/lib/ParsedFeed.php index a263bfb..947c389 100644 --- a/src/lib/ParsedFeed.php +++ b/src/lib/ParsedFeed.php @@ -22,16 +22,16 @@ class ParsedFeed public array $items = []; /** @var string The XML namespace for Atom feeds */ - public const string ATOM_NS = 'http://www.w3.org/2005/Atom'; + public const ATOM_NS = 'http://www.w3.org/2005/Atom'; /** @var string The XML namespace for the `` tag that allows HTML content in a feed */ - public const string CONTENT_NS = 'http://purl.org/rss/1.0/modules/content/'; + public const CONTENT_NS = 'http://purl.org/rss/1.0/modules/content/'; /** @var string The XML namespace for XHTML */ - public const string XHTML_NS = 'http://www.w3.org/1999/xhtml'; + public const XHTML_NS = 'http://www.w3.org/1999/xhtml'; /** @var string The user agent for Feed Reader Central's refresh requests */ - private const string USER_AGENT = + private const USER_AGENT = 'FeedReaderCentral/' . FRC_VERSION . ' +https://bitbadger.solutions/open-source/feed-reader-central'; /** diff --git a/src/lib/Security.php b/src/lib/Security.php index 68d9fce..8e74926 100644 --- a/src/lib/Security.php +++ b/src/lib/Security.php @@ -2,9 +2,7 @@ namespace FeedReaderCentral; -use BitBadger\PDODocument\DocumentException; -use BitBadger\PDODocument\Field; -use BitBadger\PDODocument\Patch; +use BitBadger\PDODocument\{DocumentException, Field, Patch}; /** * Security functions @@ -12,22 +10,22 @@ use BitBadger\PDODocument\Patch; class Security { /** @var int Run as a single user requiring no password */ - public const int SINGLE_USER = 0; + public const SINGLE_USER = 0; /** @var int Run as a single user requiring a password */ - public const int SINGLE_USER_WITH_PASSWORD = 1; + public const SINGLE_USER_WITH_PASSWORD = 1; /** @var int Require users to provide e-mail address and password */ - public const int MULTI_USER = 2; + public const MULTI_USER = 2; /** @var string The e-mail address for the single user */ - public const string SINGLE_USER_EMAIL = 'solouser@example.com'; + public const SINGLE_USER_EMAIL = 'solouser@example.com'; /** @var string The password for the single user with no password */ - public const string SINGLE_USER_PASSWORD = 'no-password-required'; + public const SINGLE_USER_PASSWORD = 'no-password-required'; /** @var string The password algorithm to use for our passwords */ - public const string PW_ALGORITHM = PASSWORD_DEFAULT; + public const PW_ALGORITHM = PASSWORD_DEFAULT; /** * Verify a user's password diff --git a/src/lib/Table.php b/src/lib/Table.php index e0eeb0d..b4f62b0 100644 --- a/src/lib/Table.php +++ b/src/lib/Table.php @@ -8,11 +8,11 @@ namespace FeedReaderCentral; class Table { /** @var string The user table */ - public const string USER = 'frc_user'; + public const USER = 'frc_user'; /** @var string The feed table */ - public const string FEED = 'feed'; + public const FEED = 'feed'; /** @var string The item table */ - public const string ITEM = 'item'; + public const ITEM = 'item'; } diff --git a/src/lib/User.php b/src/lib/User.php index 69d1823..4549ce2 100644 --- a/src/lib/User.php +++ b/src/lib/User.php @@ -2,7 +2,7 @@ namespace FeedReaderCentral; -use BitBadger\PDODocument\{Custom, DocumentException, Field, Find, Parameters, Query}; +use BitBadger\PDODocument\{Custom, Document, DocumentException, Field, Find, Parameters, Query}; use BitBadger\PDODocument\Mapper\ExistsMapper; /** @@ -40,10 +40,7 @@ class User */ public static function add(string $email, string $password): void { - Custom::nonQuery(<<<'SQL' - INSERT INTO user (data) - VALUES (json_set(:data, '$.id', (SELECT coalesce(max(data->>'id'), 0) + 1 FROM user))) - SQL, Parameters::json(':data', new User(email: $email, password: $password))); + Document::insert(Table::USER, new User(email: $email, password: $password)); } /**