Pimp entities and models
* Strict * Extend proper class * Typehint * Return types Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
parent
017bfa2994
commit
45e5e1f3b5
|
@ -1,4 +1,6 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2019 Inigo Jiron <ijiron@terpmail.umd.edu>
|
||||
*
|
||||
|
@ -35,10 +37,18 @@ use OCP\AppFramework\Db\Entity;
|
|||
* @method integer getTimestamp()
|
||||
* @method void setTimestamp(integer $value)
|
||||
*/
|
||||
class Answer extends Model {
|
||||
class Answer extends Entity {
|
||||
|
||||
/** @var int */
|
||||
protected $formId;
|
||||
|
||||
/** @var int */
|
||||
protected $questionId;
|
||||
|
||||
/** @var string */
|
||||
protected $text;
|
||||
|
||||
/** @var int */
|
||||
protected $timestamp;
|
||||
|
||||
/**
|
||||
|
@ -51,7 +61,7 @@ class Answer extends Model {
|
|||
$this->addType('timestamp', 'integer');
|
||||
}
|
||||
|
||||
public function read() {
|
||||
public function read(): array {
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'formId' => $this->getFormId(),
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2019 Inigo Jiron <ijiron@terpmail.umd.edu>
|
||||
*
|
||||
|
@ -35,7 +37,7 @@ class AnswerMapper extends QBMapper {
|
|||
* @param IDBConnection $db
|
||||
*/
|
||||
public function __construct(IDBConnection $db) {
|
||||
parent::__construct($db, 'forms_answers', '\OCA\Forms\Db\Answer');
|
||||
parent::__construct($db, 'forms_answers', Answer::class);
|
||||
}
|
||||
|
||||
// TODO: Change below functions to search by form and question id
|
||||
|
@ -47,7 +49,7 @@ class AnswerMapper extends QBMapper {
|
|||
* @return Answer[]
|
||||
*/
|
||||
|
||||
public function findByForm($formId, $questionId) {
|
||||
public function findByForm(int $formId, int $questionId): array {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
|
@ -66,7 +68,7 @@ class AnswerMapper extends QBMapper {
|
|||
* @param int $formId
|
||||
* @param int $questionId
|
||||
*/
|
||||
public function deleteByFormAndQuestion($formId, $questionId) {
|
||||
public function deleteByFormAndQuestion(int $formId, int $questionId): void {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->delete($this->getTableName())
|
||||
|
@ -83,7 +85,7 @@ class AnswerMapper extends QBMapper {
|
|||
/**
|
||||
* @param int $formId
|
||||
*/
|
||||
public function deleteByForm($formId) {
|
||||
public function deleteByForm(int $formId): void {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->delete($this->getTableName())
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
|
||||
*
|
||||
|
@ -45,7 +46,7 @@ use OCP\AppFramework\Db\Entity;
|
|||
* @method integer getIsAnonymous()
|
||||
* @method void setIsAnonymous(integer $value)
|
||||
*/
|
||||
class Event extends Model {
|
||||
class Event extends Entity {
|
||||
protected $title;
|
||||
protected $description;
|
||||
protected $owner;
|
||||
|
|
|
@ -35,7 +35,7 @@ class EventMapper extends QBMapper {
|
|||
* @param IDBConnection $db
|
||||
*/
|
||||
public function __construct(IDBConnection $db) {
|
||||
parent::__construct($db, 'forms_events', '\OCA\Forms\Db\Event');
|
||||
parent::__construct($db, 'forms_events', Event::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,7 +44,7 @@ class EventMapper extends QBMapper {
|
|||
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
||||
* @return Event
|
||||
*/
|
||||
public function find($id) {
|
||||
public function find(int $id): Event {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
|
@ -62,7 +62,7 @@ class EventMapper extends QBMapper {
|
|||
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
||||
* @return Event
|
||||
*/
|
||||
public function findByHash($hash) {
|
||||
public function findByHash(string $hash): Event {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
|
@ -78,7 +78,7 @@ class EventMapper extends QBMapper {
|
|||
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
||||
* @return Event[]
|
||||
*/
|
||||
public function findAll() {
|
||||
public function findAll(): array {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2017 Kai Schröer <git@schroeer.co>
|
||||
*
|
||||
* @author Kai Schröer <git@schroeer.co>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Forms\Db;
|
||||
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
|
||||
abstract class Model extends Entity {
|
||||
/**
|
||||
* FactoryMuffin checks for the existence of setters with method_exists($obj, $attr) but that returns false.
|
||||
* By overwriting the __set() magic method we can trigger the changed flag on $obj->attr assignment.
|
||||
*
|
||||
* @param $name
|
||||
* @param $value
|
||||
*/
|
||||
public function __set($name, $value) {
|
||||
$this->setter($name, [$value]);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
|
||||
*
|
||||
|
@ -32,7 +33,7 @@ use OCP\AppFramework\Db\Entity;
|
|||
* @method integer getFormId()
|
||||
* @method void setFormId(integer $value)
|
||||
*/
|
||||
class Notification extends Model {
|
||||
class Notification extends Entity {
|
||||
protected $userId;
|
||||
protected $formId;
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
|
||||
*
|
||||
|
@ -31,7 +32,7 @@ use OCP\AppFramework\Db\QBMapper;
|
|||
class NotificationMapper extends QBMapper {
|
||||
|
||||
public function __construct(IDBConnection $db) {
|
||||
parent::__construct($db, 'forms_notif', '\OCA\Forms\Db\Notification');
|
||||
parent::__construct($db, 'forms_notif', Notification::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -41,7 +42,7 @@ class NotificationMapper extends QBMapper {
|
|||
* @return Notification[]
|
||||
*/
|
||||
|
||||
public function findAllByForm($formId) {
|
||||
public function findAllByForm(int $formId): array {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
|
@ -59,9 +60,9 @@ class NotificationMapper extends QBMapper {
|
|||
* @param string $userId
|
||||
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
||||
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
||||
* @return Notification
|
||||
* @return Notification[]
|
||||
*/
|
||||
public function findByUserAndForm($formId, $userId) {
|
||||
public function findByUserAndForm(int $formId, string $userId): array {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
|
@ -79,7 +80,7 @@ class NotificationMapper extends QBMapper {
|
|||
/**
|
||||
* @param int $formId
|
||||
*/
|
||||
public function deleteByForm($formId) {
|
||||
public function deleteByForm(int $formId): void {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->delete($this->getTableName())
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2019 Inigo Jiron <ijiron@terpmail.umd.edu>
|
||||
*
|
||||
|
@ -35,7 +36,7 @@ use OCP\AppFramework\Db\Entity;
|
|||
* @method integer getTimestamp()
|
||||
* @method void setTimestamp(integer $value)
|
||||
*/
|
||||
class Question extends Model {
|
||||
class Question extends Entity {
|
||||
protected $formId;
|
||||
protected $formQuestionType;
|
||||
protected $formQuestionText;
|
||||
|
@ -49,7 +50,7 @@ class Question extends Model {
|
|||
$this->addType('timestamp', 'integer');
|
||||
}
|
||||
|
||||
public function read() {
|
||||
public function read(): array {
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'formId' => $this->getFormId(),
|
||||
|
|
|
@ -35,7 +35,7 @@ class QuestionMapper extends QBMapper {
|
|||
* @param IDBConnection $db
|
||||
*/
|
||||
public function __construct(IDBConnection $db) {
|
||||
parent::__construct($db, 'forms_questions', '\OCA\Forms\Db\Question');
|
||||
parent::__construct($db, 'forms_questions', Question::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,7 +44,7 @@ class QuestionMapper extends QBMapper {
|
|||
* @return Option[]
|
||||
*/
|
||||
|
||||
public function findByForm($formId) {
|
||||
public function findByForm(int $formId): array {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
|
@ -59,7 +59,7 @@ class QuestionMapper extends QBMapper {
|
|||
/**
|
||||
* @param int $formId
|
||||
*/
|
||||
public function deleteByForm($formId) {
|
||||
public function deleteByForm(int $formId): void {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->delete($this->getTableName())
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
|
||||
*
|
||||
|
@ -41,7 +43,7 @@ use OCP\AppFramework\Db\Entity;
|
|||
* @method string getVoteOptionType()
|
||||
* @method void setVoteOptionType(string $value)
|
||||
*/
|
||||
class Vote extends Model {
|
||||
class Vote extends Entity {
|
||||
protected $formId;
|
||||
protected $userId;
|
||||
protected $voteOptionId;
|
||||
|
@ -57,7 +59,7 @@ class Vote extends Model {
|
|||
$this->addType('voteOptionId', 'integer');
|
||||
}
|
||||
|
||||
public function read() {
|
||||
public function read(): array {
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'userId' => $this->getUserId(),
|
||||
|
|
|
@ -35,7 +35,7 @@ class VoteMapper extends QBMapper {
|
|||
* @param IDBConnection $db
|
||||
*/
|
||||
public function __construct(IDBConnection $db) {
|
||||
parent::__construct($db, 'forms_votes', '\OCA\Forms\Db\Vote');
|
||||
parent::__construct($db, 'forms_votes', VoteMapper::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,7 +44,7 @@ class VoteMapper extends QBMapper {
|
|||
* @return Comment[]
|
||||
*/
|
||||
|
||||
public function findByForm($formId) {
|
||||
public function findByForm(int $formId): array {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
|
@ -59,9 +59,9 @@ class VoteMapper extends QBMapper {
|
|||
/**
|
||||
* @param int $formId
|
||||
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
||||
* @return Array
|
||||
* @return array
|
||||
*/
|
||||
public function findParticipantsByForm($formId, $limit = null, $offset = null) {
|
||||
public function findParticipantsByForm(int $formId, $limit = null, $offset = null): array {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->select('user_id')
|
||||
|
@ -77,7 +77,7 @@ class VoteMapper extends QBMapper {
|
|||
* @param int $formId
|
||||
* @param string $userId
|
||||
*/
|
||||
public function deleteByFormAndUser($formId, $userId) {
|
||||
public function deleteByFormAndUser(int $formId, string $userId): void {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->delete($this->getTableName())
|
||||
|
@ -94,7 +94,7 @@ class VoteMapper extends QBMapper {
|
|||
/**
|
||||
* @param int $formId
|
||||
*/
|
||||
public function deleteByForm($formId) {
|
||||
public function deleteByForm(int $formId): void {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->delete($this->getTableName())
|
||||
|
|
Loading…
Reference in a new issue