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;
|
$arguments[EditPostJob::POST_ENTITY] = $post;
|
||||||
Api::run((new EditPostJob)->skipSaving(), $arguments);
|
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
|
//save to db
|
||||||
PostModel::save($post);
|
PostModel::save($post);
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,8 @@ class CommentModel extends AbstractCrudModel
|
||||||
|
|
||||||
public static function save($comment)
|
public static function save($comment)
|
||||||
{
|
{
|
||||||
|
$comment->validate();
|
||||||
|
|
||||||
Database::transaction(function() use ($comment)
|
Database::transaction(function() use ($comment)
|
||||||
{
|
{
|
||||||
self::forgeId($comment);
|
self::forgeId($comment);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
class AbstractEntity
|
abstract class AbstractEntity implements IValidatable
|
||||||
{
|
{
|
||||||
public $id;
|
public $id;
|
||||||
protected $__cache;
|
protected $__cache;
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
<?php
|
<?php
|
||||||
class CommentEntity extends AbstractEntity
|
class CommentEntity extends AbstractEntity implements IValidatable
|
||||||
{
|
{
|
||||||
public $text;
|
public $text;
|
||||||
public $postId;
|
public $postId;
|
||||||
public $commentDate;
|
public $commentDate;
|
||||||
public $commenterId;
|
public $commenterId;
|
||||||
|
|
||||||
|
public function validate()
|
||||||
|
{
|
||||||
|
//todo
|
||||||
|
}
|
||||||
|
|
||||||
public function getText()
|
public function getText()
|
||||||
{
|
{
|
||||||
return TextHelper::parseMarkdown($this->text);
|
return TextHelper::parseMarkdown($this->text);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
use \Chibi\Sql as Sql;
|
use \Chibi\Sql as Sql;
|
||||||
use \Chibi\Database as Database;
|
use \Chibi\Database as Database;
|
||||||
|
|
||||||
class PostEntity extends AbstractEntity
|
class PostEntity extends AbstractEntity implements IValidatable
|
||||||
{
|
{
|
||||||
protected $type;
|
protected $type;
|
||||||
protected $name;
|
protected $name;
|
||||||
|
@ -21,6 +21,13 @@ class PostEntity extends AbstractEntity
|
||||||
public $favCount = 0;
|
public $favCount = 0;
|
||||||
public $score = 0;
|
public $score = 0;
|
||||||
|
|
||||||
|
public function validate()
|
||||||
|
{
|
||||||
|
//todo
|
||||||
|
if (empty($this->getType()))
|
||||||
|
throw new SimpleException('No post type detected');
|
||||||
|
}
|
||||||
|
|
||||||
public function getUploader()
|
public function getUploader()
|
||||||
{
|
{
|
||||||
if ($this->hasCache('uploader'))
|
if ($this->hasCache('uploader'))
|
||||||
|
|
|
@ -2,10 +2,15 @@
|
||||||
use \Chibi\Sql as Sql;
|
use \Chibi\Sql as Sql;
|
||||||
use \Chibi\Database as Database;
|
use \Chibi\Database as Database;
|
||||||
|
|
||||||
class TagEntity extends AbstractEntity
|
class TagEntity extends AbstractEntity implements IValidatable
|
||||||
{
|
{
|
||||||
protected $name;
|
protected $name;
|
||||||
|
|
||||||
|
public function validate()
|
||||||
|
{
|
||||||
|
//todo
|
||||||
|
}
|
||||||
|
|
||||||
public function setName($name)
|
public function setName($name)
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
<?php
|
<?php
|
||||||
class TokenEntity extends AbstractEntity
|
class TokenEntity extends AbstractEntity implements IValidatable
|
||||||
{
|
{
|
||||||
public $userId;
|
public $userId;
|
||||||
public $token;
|
public $token;
|
||||||
public $used;
|
public $used;
|
||||||
public $expires;
|
public $expires;
|
||||||
|
|
||||||
|
public function validate()
|
||||||
|
{
|
||||||
|
//todo
|
||||||
|
}
|
||||||
|
|
||||||
public function getUser()
|
public function getUser()
|
||||||
{
|
{
|
||||||
return UserModel::findById($this->userId);
|
return UserModel::findById($this->userId);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
use \Chibi\Sql as Sql;
|
use \Chibi\Sql as Sql;
|
||||||
use \Chibi\Database as Database;
|
use \Chibi\Database as Database;
|
||||||
|
|
||||||
class UserEntity extends AbstractEntity
|
class UserEntity extends AbstractEntity implements IValidatable
|
||||||
{
|
{
|
||||||
protected $name;
|
protected $name;
|
||||||
public $passSalt;
|
public $passSalt;
|
||||||
|
@ -16,6 +16,16 @@ class UserEntity extends AbstractEntity
|
||||||
public $settings;
|
public $settings;
|
||||||
protected $banned = false;
|
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()
|
public function isBanned()
|
||||||
{
|
{
|
||||||
return $this->banned;
|
return $this->banned;
|
||||||
|
|
|
@ -45,6 +45,8 @@ class PostModel extends AbstractCrudModel
|
||||||
|
|
||||||
public static function save($post)
|
public static function save($post)
|
||||||
{
|
{
|
||||||
|
$post->validate();
|
||||||
|
|
||||||
Database::transaction(function() use ($post)
|
Database::transaction(function() use ($post)
|
||||||
{
|
{
|
||||||
self::forgeId($post);
|
self::forgeId($post);
|
||||||
|
|
|
@ -19,6 +19,8 @@ class TagModel extends AbstractCrudModel
|
||||||
|
|
||||||
public static function save($tag)
|
public static function save($tag)
|
||||||
{
|
{
|
||||||
|
$tag->validate();
|
||||||
|
|
||||||
Database::transaction(function() use ($tag)
|
Database::transaction(function() use ($tag)
|
||||||
{
|
{
|
||||||
self::forgeId($tag, 'tag');
|
self::forgeId($tag, 'tag');
|
||||||
|
|
|
@ -11,6 +11,8 @@ class TokenModel extends AbstractCrudModel
|
||||||
|
|
||||||
public static function save($token)
|
public static function save($token)
|
||||||
{
|
{
|
||||||
|
$token->validate();
|
||||||
|
|
||||||
Database::transaction(function() use ($token)
|
Database::transaction(function() use ($token)
|
||||||
{
|
{
|
||||||
self::forgeId($token);
|
self::forgeId($token);
|
||||||
|
|
|
@ -34,8 +34,7 @@ class UserModel extends AbstractCrudModel
|
||||||
|
|
||||||
public static function save($user)
|
public static function save($user)
|
||||||
{
|
{
|
||||||
if ($user->getAccessRank()->toInteger() == AccessRank::Anonymous)
|
$user->validate();
|
||||||
throw new Exception('Trying to save anonymous user into database');
|
|
||||||
|
|
||||||
Database::transaction(function() use ($user)
|
Database::transaction(function() use ($user)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue