Monthly.php in Commerce Core 8.2
File
modules/number_pattern/src/Plugin/Commerce/NumberPattern/Monthly.phpView source
<?php
namespace Drupal\commerce_number_pattern\Plugin\Commerce\NumberPattern;
use Drupal\commerce_number_pattern\Sequence;
use Drupal\Core\Datetime\DrupalDateTime;
/**
* Provides the monthly number pattern.
*
* @CommerceNumberPattern(
* id = "monthly",
* label = @Translation("Monthly (Reset every month)"),
* )
*/
class Monthly extends SequentialNumberPatternBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'pattern' => '[pattern:year]-[pattern:month]-[pattern:number]',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
protected function shouldReset(Sequence $current_sequence) {
// Reset the sequence if the current one is from a previous month.
$generated_time = DrupalDateTime::createFromTimestamp($current_sequence
->getGeneratedTime());
$current_time = DrupalDateTime::createFromTimestamp($this->time
->getCurrentTime());
return $generated_time
->format('Y-m') != $current_time
->format('Y-m');
}
}