Skip to content

Commit

Permalink
Add PriceCondtion and PriceTypeConditionRule
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Apr 25, 2024
1 parent 9be49ca commit d61ca50
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/elements/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
use craft\base\NestedElementTrait;
use craft\db\Query;
use craft\db\Table as CraftTable;
use craft\elements\conditions\ElementConditionInterface;
use craft\elements\User;
use craft\helpers\Db;
use craft\helpers\Html;
use craft\helpers\Json;
use craft\helpers\StringHelper;
use craft\models\FieldLayout;
use craft\stripe\db\Table;
use craft\stripe\elements\conditions\products\PriceCondition;
use craft\stripe\elements\db\PriceQuery;
use craft\stripe\helpers\Price as PriceHelper;
use craft\stripe\Plugin;
Expand Down Expand Up @@ -227,6 +229,14 @@ public static function find(): PriceQuery
return Craft::createObject(PriceQuery::class, [static::class]);
}

/**
* @inerhitdoc
*/
public static function createCondition(): PriceCondition
{
return Craft::createObject(PriceCondition::class, [static::class]);
}

/**
* @inheritdoc
*/
Expand Down
29 changes: 29 additions & 0 deletions src/elements/conditions/products/PriceCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\stripe\elements\conditions\products;

use craft\elements\conditions\ElementCondition;
use craft\errors\InvalidTypeException;

/**
* Price condition
*
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class PriceCondition extends ElementCondition
{
/**
* @throws InvalidTypeException
*/
protected function selectableConditionRules(): array
{
return array_merge(parent::selectableConditionRules(), [
PriceTypeConditionRule::class,
]);
}
}
75 changes: 75 additions & 0 deletions src/elements/conditions/products/PriceTypeConditionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\stripe\elements\conditions\products;

use craft\base\conditions\BaseMultiSelectConditionRule;
use craft\base\ElementInterface;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\ElementQueryInterface;
use craft\helpers\StringHelper;
use craft\stripe\elements\db\PriceQuery;
use craft\stripe\elements\Price;
use craft\stripe\enums\PriceType;

/**
* Class PriceTypeConditionRule
*
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class PriceTypeConditionRule extends BaseMultiSelectConditionRule implements ElementConditionRuleInterface
{
/**
* @inheritDoc
*/
public function getLabel(): string
{
return \Craft::t('stripe', 'Type');
}

/**
* @inheritdoc
*/
protected function options(): array
{
return array_map(function($option) {
return [
'value' => $option,
'label' => StringHelper::humanize($option),
];
}, [
PriceType::OneTime->value,
PriceType::Recurring->value,
]);
}

/**
* @inheritdoc
*/
public function getExclusiveQueryParams(): array
{
return ['type'];
}

/**
* @inheritdoc
*/
public function matchElement(ElementInterface $element): bool
{
/** @var Price $element */
return $this->matchValue($element->priceType);
}

/**
* @inheritdoc
*/
public function modifyQuery(ElementQueryInterface $query): void
{
/** @var PriceQuery $query */
$query->priceType($this->paramValue());
}
}

0 comments on commit d61ca50

Please sign in to comment.