-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
PriceCondtion
and PriceTypeConditionRule
- Loading branch information
1 parent
9be49ca
commit d61ca50
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
75
src/elements/conditions/products/PriceTypeConditionRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |