Skip to content

Commit

Permalink
Improve layout/structure for validators.
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Aug 27, 2017
1 parent 488d1a2 commit 18ca83f
Show file tree
Hide file tree
Showing 28 changed files with 300 additions and 311 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/BoardsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use App\Json\Api\BoardsApi as Api;
use App\Json\Schemes\BoardScheme as Scheme;
use App\Json\Validators\BoardCreate;
use App\Json\Validators\BoardUpdate;
use App\Json\Validators\Board\BoardCreate;
use App\Json\Validators\Board\BoardUpdate;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/CommentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use App\Json\Api\CommentsApi as Api;
use App\Json\Schemes\CommentScheme as Scheme;
use App\Json\Validators\CommentCreate;
use App\Json\Validators\CommentUpdate;
use App\Json\Validators\Comment\CommentCreate;
use App\Json\Validators\Comment\CommentUpdate;

/**
* @package App
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use App\Json\Api\PostsApi as Api;
use App\Json\Schemes\PostScheme as Scheme;
use App\Json\Validators\PostCreate;
use App\Json\Validators\PostUpdate;
use App\Json\Validators\Post\PostCreate;
use App\Json\Validators\Post\PostUpdate;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/RolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use App\Json\Api\RolesApi as Api;
use App\Json\Schemes\RoleScheme as Scheme;
use App\Json\Validators\RoleCreate;
use App\Json\Validators\RoleUpdate;
use App\Json\Validators\Role\RoleCreate;
use App\Json\Validators\Role\RoleUpdate;

/**
* @package App
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use App\Json\Api\UsersApi as Api;
use App\Json\Schemes\UserScheme as Scheme;
use App\Json\Validators\UserCreate;
use App\Json\Validators\UserUpdate;
use App\Json\Validators\User\UserCreate;
use App\Json\Validators\User\UserUpdate;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php namespace App\Json\Validators\Rules;
<?php namespace App\Json\Validators;

use Limoncello\Flute\Contracts\Validation\ErrorCodes;

Expand Down
114 changes: 114 additions & 0 deletions app/Json/Validators/BaseRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php namespace App\Json\Validators;

use App\Data\Models\Board;
use App\Data\Models\Comment;
use App\Data\Models\Post;
use App\Data\Models\Role;
use App\Data\Models\User;
use App\Json\Schemes\BoardScheme;
use App\Json\Schemes\CommentScheme;
use App\Json\Schemes\PostScheme;
use App\Json\Schemes\RoleScheme;
use App\Json\Schemes\UserScheme;
use Limoncello\Flute\Validation\Rules\ExistInDatabaseTrait;
use Limoncello\Flute\Validation\Rules\RelationshipsTrait;
use Limoncello\Validation\Contracts\Rules\RuleInterface;
use Limoncello\Validation\Rules;

/**
* @package App
*/
class BaseRules extends Rules
{
use RelationshipsTrait, ExistInDatabaseTrait;

/**
* @return RuleInterface
*/
public static function boardId(): RuleInterface
{
return self::stringToInt(self::exists(Board::TABLE_NAME, Board::FIELD_ID));
}

/**
* @return RuleInterface
*/
public static function boardRelationship(): RuleInterface
{
return self::toOneRelationship(BoardScheme::TYPE, static::boardId());
}

/**
* @return RuleInterface
*/
public static function commentId(): RuleInterface
{
return self::stringToInt(self::exists(Comment::TABLE_NAME, Comment::FIELD_ID));
}

/**
* @return RuleInterface
*/
public static function commentRelationship(): RuleInterface
{
return self::toOneRelationship(CommentScheme::TYPE, static::commentId());
}

/**
* @return RuleInterface
*/
public static function postId(): RuleInterface
{
return self::stringToInt(self::exists(Post::TABLE_NAME, Post::FIELD_ID));
}

/**
* @return RuleInterface
*/
public static function postRelationship(): RuleInterface
{
return self::toOneRelationship(PostScheme::TYPE, static::postId());
}

/**
* @return RuleInterface
*/
public static function roleId(): RuleInterface
{
return self::isString(self::exists(Role::TABLE_NAME, Role::FIELD_ID));
}

/**
* @return RuleInterface
*/
public static function roleRelationship(): RuleInterface
{
return self::toOneRelationship(RoleScheme::TYPE, static::roleId());
}

/**
* @return RuleInterface
*/
public static function userId(): RuleInterface
{
return self::stringToInt(self::exists(User::TABLE_NAME, User::FIELD_ID));
}

/**
* @return RuleInterface
*/
public static function userRelationship(): RuleInterface
{
return self::toOneRelationship(UserScheme::TYPE, static::userId());
}

/**
* @return RuleInterface
*/
public static function usersRelationship(): RuleInterface
{
$readableAll = static::stringArrayToIntArray(static::existAll(User::TABLE_NAME, User::FIELD_ID));

return self::toManyRelationship(UserScheme::TYPE, $readableAll);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace App\Json\Validators;
<?php namespace App\Json\Validators\Board;

use App\Json\Schemes\BoardScheme as Scheme;
use App\Json\Validators\Rules\BoardRules as v;
use App\Json\Validators\Board\BoardRules as r;
use Limoncello\Flute\Contracts\Validation\JsonApiRuleSetInterface;
use Limoncello\Validation\Contracts\Rules\RuleInterface;

Expand All @@ -17,15 +17,15 @@ final class BoardCreate implements JsonApiRuleSetInterface
*/
public static function getTypeRule(): RuleInterface
{
return v::isBoardType();
return r::boardType();
}

/**
* @inheritdoc
*/
public static function getIdRule(): RuleInterface
{
return v::equals(null);
return r::equals(null);
}

/**
Expand All @@ -34,7 +34,7 @@ public static function getIdRule(): RuleInterface
public static function getAttributeRules(): array
{
return [
Scheme::ATTR_TITLE => v::required(v::title()),
Scheme::ATTR_TITLE => r::required(r::title()),
];
}

Expand Down
30 changes: 30 additions & 0 deletions app/Json/Validators/Board/BoardRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php namespace App\Json\Validators\Board;

use App\Data\Models\Board as Model;
use App\Json\Schemes\BoardScheme as Scheme;
use App\Json\Validators\BaseRules;
use Limoncello\Validation\Contracts\Rules\RuleInterface;

/**
* @package App
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
final class BoardRules extends BaseRules
{
/**
* @return RuleInterface
*/
public static function boardType(): RuleInterface
{
return self::equals(Scheme::TYPE);
}

/**
* @return RuleInterface
*/
public static function title(): RuleInterface
{
return self::isString(self::stringLengthMax(Model::getAttributeLengths()[Model::FIELD_TITLE]));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace App\Json\Validators;
<?php namespace App\Json\Validators\Board;

use App\Json\Schemes\BoardScheme as Scheme;
use App\Json\Validators\Rules\BoardRules as v;
use App\Json\Validators\Board\BoardRules as r;
use Limoncello\Flute\Contracts\Validation\JsonApiRuleSetInterface;
use Limoncello\Validation\Contracts\Rules\RuleInterface;

Expand All @@ -17,15 +17,15 @@ final class BoardUpdate implements JsonApiRuleSetInterface
*/
public static function getTypeRule(): RuleInterface
{
return v::isBoardType();
return r::boardType();
}

/**
* @inheritdoc
*/
public static function getIdRule(): RuleInterface
{
return v::isBoardId();
return r::boardId();
}

/**
Expand All @@ -34,7 +34,7 @@ public static function getIdRule(): RuleInterface
public static function getAttributeRules(): array
{
return [
Scheme::ATTR_TITLE => v::required(v::title()),
Scheme::ATTR_TITLE => r::required(r::title()),
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace App\Json\Validators;
<?php namespace App\Json\Validators\Comment;

use App\Json\Schemes\CommentScheme as Scheme;
use App\Json\Validators\Rules\CommentRules as v;
use App\Json\Validators\Comment\CommentRules as r;
use Limoncello\Flute\Contracts\Validation\JsonApiRuleSetInterface;
use Limoncello\Validation\Contracts\Rules\RuleInterface;

Expand All @@ -17,15 +17,15 @@ final class CommentCreate implements JsonApiRuleSetInterface
*/
public static function getTypeRule(): RuleInterface
{
return v::isCommentType();
return r::commentType();
}

/**
* @inheritdoc
*/
public static function getIdRule(): RuleInterface
{
return v::equals(null);
return r::equals(null);
}

/**
Expand All @@ -34,7 +34,7 @@ public static function getIdRule(): RuleInterface
public static function getAttributeRules(): array
{
return [
Scheme::ATTR_TEXT => v::required(v::text()),
Scheme::ATTR_TEXT => r::required(r::text()),
];
}

Expand All @@ -44,7 +44,7 @@ public static function getAttributeRules(): array
public static function getToOneRelationshipRules(): array
{
return [
Scheme::REL_POST => v::required(v::isPostRelationship()),
Scheme::REL_POST => r::required(r::postRelationship()),
];
}

Expand Down
29 changes: 29 additions & 0 deletions app/Json/Validators/Comment/CommentRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php namespace App\Json\Validators\Comment;

use App\Json\Schemes\CommentScheme as Scheme;
use App\Json\Validators\BaseRules;
use Limoncello\Validation\Contracts\Rules\RuleInterface;

/**
* @package App
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
final class CommentRules extends BaseRules
{
/**
* @return RuleInterface
*/
public static function commentType(): RuleInterface
{
return self::equals(Scheme::TYPE);
}

/**
* @return RuleInterface
*/
public static function text(): RuleInterface
{
return self::isString();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace App\Json\Validators;
<?php namespace App\Json\Validators\Comment;

use App\Json\Schemes\CommentScheme as Scheme;
use App\Json\Validators\Rules\CommentRules as v;
use App\Json\Validators\Comment\CommentRules as r;
use Limoncello\Flute\Contracts\Validation\JsonApiRuleSetInterface;
use Limoncello\Validation\Contracts\Rules\RuleInterface;

Expand All @@ -17,15 +17,15 @@ final class CommentUpdate implements JsonApiRuleSetInterface
*/
public static function getTypeRule(): RuleInterface
{
return v::isCommentType();
return r::commentType();
}

/**
* @inheritdoc
*/
public static function getIdRule(): RuleInterface
{
return v::isCommentId();
return r::commentId();
}

/**
Expand All @@ -34,7 +34,7 @@ public static function getIdRule(): RuleInterface
public static function getAttributeRules(): array
{
return [
Scheme::ATTR_TEXT => v::required(v::text()),
Scheme::ATTR_TEXT => r::required(r::text()),
];
}

Expand Down
Loading

0 comments on commit 18ca83f

Please sign in to comment.