Moved validation to entities
This commit is contained in:
parent
097deb52bd
commit
05a3cf927b
12 changed files with 47 additions and 16 deletions
|
@ -22,14 +22,6 @@ class AddPostJob extends AbstractJob
|
|||
$arguments[EditPostJob::POST_ENTITY] = $post;
|
||||
Api::run((new EditPostJob)->skipSaving(), $arguments);
|
||||
|
||||
// basically means that user didn't specify file nor url
|
||||
//todo:
|
||||
//- move this to PostEntity::isValid()
|
||||
//- create IValidatable interface
|
||||
//- enforce entity validity upon calling save() in models
|
||||
if (empty($post->getType()))
|
||||
throw new SimpleException('No post type detected; upload faled');
|
||||
|
||||
//save to db
|
||||
PostModel::save($post);
|
||||
|
||||
|
|
|
@ -18,6 +18,8 @@ class CommentModel extends AbstractCrudModel
|
|||
|
||||
public static function save($comment)
|
||||
{
|
||||
$comment->validate();
|
||||
|
||||
Database::transaction(function() use ($comment)
|
||||
{
|
||||
self::forgeId($comment);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
class AbstractEntity
|
||||
abstract class AbstractEntity implements IValidatable
|
||||
{
|
||||
public $id;
|
||||
protected $__cache;
|
||||
|
|
|
@ -1,11 +1,16 @@
|
|||
<?php
|
||||
class CommentEntity extends AbstractEntity
|
||||
class CommentEntity extends AbstractEntity implements IValidatable
|
||||
{
|
||||
public $text;
|
||||
public $postId;
|
||||
public $commentDate;
|
||||
public $commenterId;
|
||||
|
||||
public function validate()
|
||||
{
|
||||
//todo
|
||||
}
|
||||
|
||||
public function getText()
|
||||
{
|
||||
return TextHelper::parseMarkdown($this->text);
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
use \Chibi\Sql as Sql;
|
||||
use \Chibi\Database as Database;
|
||||
|
||||
class PostEntity extends AbstractEntity
|
||||
class PostEntity extends AbstractEntity implements IValidatable
|
||||
{
|
||||
protected $type;
|
||||
protected $name;
|
||||
|
@ -21,6 +21,13 @@ class PostEntity extends AbstractEntity
|
|||
public $favCount = 0;
|
||||
public $score = 0;
|
||||
|
||||
public function validate()
|
||||
{
|
||||
//todo
|
||||
if (empty($this->getType()))
|
||||
throw new SimpleException('No post type detected');
|
||||
}
|
||||
|
||||
public function getUploader()
|
||||
{
|
||||
if ($this->hasCache('uploader'))
|
||||
|
|
|
@ -2,10 +2,15 @@
|
|||
use \Chibi\Sql as Sql;
|
||||
use \Chibi\Database as Database;
|
||||
|
||||
class TagEntity extends AbstractEntity
|
||||
class TagEntity extends AbstractEntity implements IValidatable
|
||||
{
|
||||
protected $name;
|
||||
|
||||
public function validate()
|
||||
{
|
||||
//todo
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
|
|
@ -1,11 +1,16 @@
|
|||
<?php
|
||||
class TokenEntity extends AbstractEntity
|
||||
class TokenEntity extends AbstractEntity implements IValidatable
|
||||
{
|
||||
public $userId;
|
||||
public $token;
|
||||
public $used;
|
||||
public $expires;
|
||||
|
||||
public function validate()
|
||||
{
|
||||
//todo
|
||||
}
|
||||
|
||||
public function getUser()
|
||||
{
|
||||
return UserModel::findById($this->userId);
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
use \Chibi\Sql as Sql;
|
||||
use \Chibi\Database as Database;
|
||||
|
||||
class UserEntity extends AbstractEntity
|
||||
class UserEntity extends AbstractEntity implements IValidatable
|
||||
{
|
||||
protected $name;
|
||||
public $passSalt;
|
||||
|
@ -16,6 +16,16 @@ class UserEntity extends AbstractEntity
|
|||
public $settings;
|
||||
protected $banned = false;
|
||||
|
||||
public function validate()
|
||||
{
|
||||
//todo: add more validation
|
||||
if (empty($this->getAccessRank()))
|
||||
throw new SimpleException('No access rank detected');
|
||||
|
||||
if ($this->getAccessRank()->toInteger() == AccessRank::Anonymous)
|
||||
throw new Exception('Trying to save anonymous user into database');
|
||||
}
|
||||
|
||||
public function isBanned()
|
||||
{
|
||||
return $this->banned;
|
||||
|
|
|
@ -45,6 +45,8 @@ class PostModel extends AbstractCrudModel
|
|||
|
||||
public static function save($post)
|
||||
{
|
||||
$post->validate();
|
||||
|
||||
Database::transaction(function() use ($post)
|
||||
{
|
||||
self::forgeId($post);
|
||||
|
|
|
@ -19,6 +19,8 @@ class TagModel extends AbstractCrudModel
|
|||
|
||||
public static function save($tag)
|
||||
{
|
||||
$tag->validate();
|
||||
|
||||
Database::transaction(function() use ($tag)
|
||||
{
|
||||
self::forgeId($tag, 'tag');
|
||||
|
|
|
@ -11,6 +11,8 @@ class TokenModel extends AbstractCrudModel
|
|||
|
||||
public static function save($token)
|
||||
{
|
||||
$token->validate();
|
||||
|
||||
Database::transaction(function() use ($token)
|
||||
{
|
||||
self::forgeId($token);
|
||||
|
|
|
@ -34,8 +34,7 @@ class UserModel extends AbstractCrudModel
|
|||
|
||||
public static function save($user)
|
||||
{
|
||||
if ($user->getAccessRank()->toInteger() == AccessRank::Anonymous)
|
||||
throw new Exception('Trying to save anonymous user into database');
|
||||
$user->validate();
|
||||
|
||||
Database::transaction(function() use ($user)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue