Skip to content

Commit

Permalink
feature: add json encoding and get method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Waldstein committed Jan 14, 2025
1 parent 24cf2ef commit 72d6c17
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/DonationForms/Listeners/StoreCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,21 @@ protected function handleFileUpload(File $field): ?array
}

/**
* @unreleased replace update_meta with meta repository
* @since 3.0.0
*/
protected function storeAsDonorMeta(int $donorId, string $metaKey, $value): void
{
give()->donor_meta->update_meta($donorId, $metaKey, $value);
give()->donors->meta->upsert($donorId, $metaKey, $value);
}

/**
* @unreleased replace update_meta with meta repository
* @since 3.0.0
*/
protected function storeAsDonationMeta(int $donationId, string $metaKey, $value): void
{
give()->payment_meta->update_meta($donationId, $metaKey, $value);
give()->donations->meta->upsert($donationId, $metaKey, $value);
}

/**
Expand Down
44 changes: 39 additions & 5 deletions src/Donations/Repositories/DonationMetaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,52 @@
namespace Give\Donations\Repositories;

use Give\Framework\Database\DB;
use Give\Framework\QueryBuilder\QueryBuilder;
use Give\Framework\Support\Facades\Str;

/**
* @unreleased
*/
class DonationMetaRepository
{

/**
* @unreleased
* @return mixed|null
*/
public function upsert(int $donationId, string $metaKey, $metaValue): void
public function get(int $donationId, string $metaKey)
{
$queryBuilder = DB::table("give_donationmeta");

$query = $queryBuilder
$query = $this->prepareQuery()
->where("donation_id", $donationId)
->where("meta_key", $metaKey)
->get();

if (!$query) {
return null;
}

$value = $query->meta_value;

if (Str::isJson($value)) {
return json_decode($value, false);
}

return $value;
}

/**
* @unreleased
*/
public function upsert(int $donationId, string $metaKey, $metaValue): void
{
if (is_array($metaValue) || is_object($metaValue)) {
$metaValue = json_encode($metaValue);
}

$exists = $this->get($donationId, $metaKey);

$queryBuilder = $this->prepareQuery();

if (!$exists) {
$queryBuilder->insert([
"donation_id" => $donationId,
"meta_key" => $metaKey,
Expand All @@ -35,4 +61,12 @@ public function upsert(int $donationId, string $metaKey, $metaValue): void
->update(["meta_value" => $metaValue]);
}
}

/**
* @unreleased
*/
public function prepareQuery(): QueryBuilder
{
return DB::table("give_donationmeta");
}
}
45 changes: 40 additions & 5 deletions src/Donors/Repositories/DonorMetaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,53 @@
namespace Give\Donors\Repositories;

use Give\Framework\Database\DB;
use Give\Framework\QueryBuilder\QueryBuilder;
use Give\Framework\Support\Facades\Str;

/**
* @unreleased
*/
class DonorMetaRepository
{

/**
* @unreleased
*
* @return mixed|null
*/
public function upsert(int $donorId, string $metaKey, $metaValue): void
public function get(int $donorId, string $metaKey)
{
$queryBuilder = DB::table("give_donormeta");

$query = $queryBuilder
$query = $this->prepareQuery()
->where("donor_id", $donorId)
->where("meta_key", $metaKey)
->get();

if (!$query) {
return null;
}

$value = $query->meta_value;

if (Str::isJson($value)) {
return json_decode($value, false);
}

return $value;
}

/**
* @unreleased
*/
public function upsert(int $donorId, string $metaKey, $metaValue): void
{
if (is_array($metaValue) || is_object($metaValue)) {
$metaValue = json_encode($metaValue);
}

$exists = $this->get($donorId, $metaKey);

$queryBuilder = $this->prepareQuery();

if (!$exists) {
$queryBuilder->insert([
"donor_id" => $donorId,
"meta_key" => $metaKey,
Expand All @@ -35,4 +62,12 @@ public function upsert(int $donorId, string $metaKey, $metaValue): void
->update(["meta_value" => $metaValue]);
}
}

/**
* @unreleased
*/
public function prepareQuery(): QueryBuilder
{
return DB::table("give_donormeta");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __invoke(DonationReceipt $receipt): DonationReceipt
}

/**
* @unreleased replace get_meta with meta repositories
* @since 3.4.0 updated to check for metaKey first and then fallback to name
* @since 3.3.0 updated conditional to check for scopes and added support for retrieving values programmatically with Fields API
* @since 3.0.0
Expand Down Expand Up @@ -70,17 +71,16 @@ protected function getCustomFields(Donation $donation): array
continue;
}

$value = give()->donor_meta->get_meta(
$value = give()->donors->meta->get(
$donation->donor->id,
$field->getMetaKey() ?? $field->getName(),
true
$field->getMetaKey() ?? $field->getName()
);
} elseif ($field->getScope()->isDonation()) {
if (!metadata_exists('donation', $donation->id, $field->getMetaKey() ?? $field->getName())) {
continue;
}

$value = give()->payment_meta->get_meta($donation->id, $field->getMetaKey() ?? $field->getName(), true);
$value = give()->donations->meta->get($donation->id, $field->getMetaKey() ?? $field->getName());
} else {
$value = null;
}
Expand Down
23 changes: 23 additions & 0 deletions src/Framework/Support/Facades/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Give\Framework\Support\Facades;

use JsonException;

use function sanitize_title;

class Str
Expand Down Expand Up @@ -816,4 +818,25 @@ public static function flushCache()
static::$camelCache = [];
static::$studlyCache = [];
}

/**
* @unreleased
*
* Determine if a given value is valid JSON.
*
* @param mixed $value
* @return bool
*/
public static function isJson($value): bool
{
if (!is_string($value)) {
return false;
}

if (function_exists('json_validate')) {
return json_validate($value);
}

return !is_null(json_decode($value, true));
}
}
57 changes: 53 additions & 4 deletions tests/Unit/Donations/Repositories/TestDonationMetaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,31 @@ public function testUpsertShouldInsertNewMeta(): void
{
$donation = Donation::factory()->create();
$repository = new DonationMetaRepository();
$repository->upsert($donation->id, 'test_key', 'test_value');
$repository->upsert($donation->id, 'test_key_string', 'Test Value');
$repository->upsert($donation->id, 'test_key_array', ['Test Value']);
$repository->upsert($donation->id, 'test_key_int', 1);

$meta = DB::table('give_donationmeta')
$meta1 = DB::table('give_donationmeta')
->where('donation_id', $donation->id)
->where('meta_key', 'test_key')
->where('meta_key', 'test_key_string')
->get()
->meta_value;

$this->assertEquals('test_value', $meta);
$meta2 = DB::table('give_donationmeta')
->where('donation_id', $donation->id)
->where('meta_key', 'test_key_array')
->get()
->meta_value;

$meta3 = DB::table('give_donationmeta')
->where('donation_id', $donation->id)
->where('meta_key', 'test_key_int')
->get()
->meta_value;

$this->assertEquals('Test Value', $meta1);
$this->assertEquals(['Test Value'], json_decode($meta2, false));
$this->assertEquals(1, $meta3);
}

/**
Expand Down Expand Up @@ -84,4 +98,39 @@ public function testUpsertShouldUpdateExistingMeta(): void

$this->assertEquals('Test Value Two', $meta);
}

/**
* @unreleased
* @throws Exception
*/
public function testGetShouldReturnNullIfMetaDoesNotExist(): void
{
$donation = Donation::factory()->create();
$repository = new DonationMetaRepository();

$meta = $repository->get($donation->id, 'test_key');

$this->assertNull($meta);
}

/**
* @unreleased
* @throws Exception
*/
public function testGetShouldReturnMetaValueIfExists(): void
{
$donation = Donation::factory()->create();
$repository = new DonationMetaRepository();

DB::table('give_donationmeta')
->insert([
'donation_id' => $donation->id,
'meta_key' => 'test_key',
'meta_value' => 'Test Value',
]);

$meta = $repository->get($donation->id, 'test_key');

$this->assertEquals('Test Value', $meta);
}
}
51 changes: 49 additions & 2 deletions tests/Unit/Donors/Repositories/TestDonorMetaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,30 @@ public function testUpsertMetaShouldInsertNewMeta(): void
$donor = Donor::factory()->create();
$repository = new DonorMetaRepository();
$repository->upsert($donor->id, 'test_key', 'test_value');
$repository->upsert($donor->id, 'test_key_array', ['Test Value']);
$repository->upsert($donor->id, 'test_key_int', 1);

$meta = DB::table('give_donormeta')
$meta1 = DB::table('give_donormeta')
->where('donor_id', $donor->id)
->where('meta_key', 'test_key')
->get()
->meta_value;

$this->assertEquals('test_value', $meta);
$meta2 = DB::table('give_donormeta')
->where('donor_id', $donor->id)
->where('meta_key', 'test_key_array')
->get()
->meta_value;

$meta3 = DB::table('give_donormeta')
->where('donor_id', $donor->id)
->where('meta_key', 'test_key_int')
->get()
->meta_value;

$this->assertEquals('test_value', $meta1);
$this->assertEquals(['Test Value'], json_decode($meta2, true));
$this->assertEquals(1, $meta3);
}

/**
Expand Down Expand Up @@ -84,4 +100,35 @@ public function testUpsertMetaShouldUpdateExistingMeta(): void
$this->assertEquals('Test Value Two', $meta);
}

/**
* @unreleased
*/
public function testGetShouldReturnNullIfMetaDoesNotExist(): void
{
$donor = Donor::factory()->create();
$repository = new DonorMetaRepository();

$meta = $repository->get($donor->id, 'test_key');

$this->assertNull($meta);
}

/**
* @unreleased
*/
public function testGetShouldReturnMetaValueIfExists(): void
{
$donor = Donor::factory()->create();
$repository = new DonorMetaRepository();

DB::table('give_donormeta')
->insert([
'donor_id' => $donor->id,
'meta_key' => 'test_key',
'meta_value' => 'Test Value',
]);

$meta = $repository->get($donor->id, 'test_key');
$this->assertEquals('Test Value', $meta);
}
}

0 comments on commit 72d6c17

Please sign in to comment.