Skip to content

Commit

Permalink
Add SQL Day Of Week function
Browse files Browse the repository at this point in the history
  • Loading branch information
aydun committed Apr 17, 2023
1 parent 4d56c38 commit 7737fe7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Civi/Api4/Query/SqlFunctionDAYOFWEEK.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

namespace Civi\Api4\Query;

/**
* Sql function
*/
class SqlFunctionDAYOFWEEK extends SqlFunction {

protected static $category = self::CATEGORY_DATE;

protected static $dataType = 'Integer';

protected static function params(): array {
return [
[
'max_expr' => 1,
'optional' => FALSE,
],
];
}

/**
* @return string
*/
public static function getTitle(): string {
return ts('Day of Week');
}

/**
* @return string
*/
public static function getDescription(): string {
return ts('The day of the week of a date.');
}

/**
* @return array
*/
public static function getOptions(): ?array {
return [
1 => ts('Sunday'),
2 => ts('Monday'),
3 => ts('Tuesday'),
4 => ts('Wednesday'),
5 => ts('Thursday'),
6 => ts('Friday'),
7 => ts('Saturday'),
];
}

}
6 changes: 6 additions & 0 deletions tests/phpunit/api/v4/Action/SqlFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ public function testDateFunctions() {
->addSelect('MONTH(birth_date):label AS month_name')
->addSelect('MONTH(birth_date):label')
->addSelect('EXTRACT(YEAR_MONTH FROM birth_date) AS year_month')
->addSelect('DAYOFWEEK(birth_date) AS day_number')
->addSelect('DAYOFWEEK(birth_date):label AS day_name')
->addWhere('last_name', '=', $lastName)
->addOrderBy('id')
->execute();
Expand All @@ -247,6 +249,8 @@ public function testDateFunctions() {
$this->assertEquals('November', $result[0]['month_name']);
$this->assertEquals('November', $result[0]['MONTH:birth_date:label']);
$this->assertEquals('200911', $result[0]['year_month']);
$this->assertEquals(4, $result[0]['day_number']);
$this->assertEquals('Wednesday', $result[0]['day_name']);

$this->assertEquals(0, $result[1]['diff']);
$this->assertEquals(2010, $result[1]['year']);
Expand All @@ -255,6 +259,8 @@ public function testDateFunctions() {
$this->assertEquals('January', $result[1]['month_name']);
$this->assertEquals('January', $result[1]['MONTH:birth_date:label']);
$this->assertEquals('201001', $result[1]['year_month']);
$this->assertEquals(6, $result[1]['day_number']);
$this->assertEquals('Friday', $result[1]['day_name']);
}

public function testIncorrectNumberOfArguments() {
Expand Down

0 comments on commit 7737fe7

Please sign in to comment.