我正在使用Laravel 5的Command Bus,我不清楚如何实现验证器类

who*_*boy 9 php validation laravel laravel-5 laravel-validation

我正在使用Laravel 5的Command Bus,我不清楚如何实现验证器类.

我想创建一个ResizeImageCommandValidator类,在尝试调整图像大小之前检查图像实际上是图像.

我想提取的代码来自ResizeImageCommandHandler resize方法.

if (!($image instanceof Image))
{
    throw new ImageInvalidException('ResizeImageCommandHandler');
}
Run Code Online (Sandbox Code Playgroud)

这个想法来自Laracasts 命令和域事件,但Jeffrey没有使用Laravel 5架构.

这是代码.

ResizeImageCommandHandler.php

<?php namespace App\Handlers\Commands;

use App\Commands\ResizeImageCommand;

use App\Exceptions\ImageInvalidException;
use Illuminate\Queue\InteractsWithQueue;
use Intervention\Image\Image;

class ResizeImageCommandHandler {

    /**
     * Create the command handler.
     */
    public function __construct()
    {
    }
    /**
     * Handle the command.
     *
     * @param  ResizeImageCommand  $command
     * @return void
     */
    public function handle($command)
    {
        $this->resizeImage($command->image, $command->dimension);
    }
    /**
     * Resize the image by width, designed for square image only
     * @param Image $image Image to resize
     * @param $dimension
     * @throws ImageInvalidException
     */
    private function resizeImage(&$image, $dimension)
    {
        if (!($image instanceof Image))
        {
            throw new ImageInvalidException('ResizeImageCommandHandler');
        }
        $image->resize($dimension, null, $this->constrainAspectRatio());
    }
    /**
     * @return callable
     */
    private function constrainAspectRatio()
    {
        return function ($constraint) {
            $constraint->aspectRatio();
        };
    }


}      
Run Code Online (Sandbox Code Playgroud)

ResizeImageCommand.php

<?php namespace App\Commands;

use App\Commands\Command;

use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
use Image;

class ResizeImageCommand extends Command {
    use InteractsWithQueue, SerializesModels;

    public $image;
    public $savePath;
    public $dimension;

    /**
     * Create a new command instance.
     * @param Image $image
     * @param string $savePath
     * @param int $dimension
     * @param int $pose_id
     * @param int $state_id
     */
    public function __construct(&$image, $savePath, $dimension)
    {
        $this->image = $image;
        $this->savePath = $savePath;
        $this->dimension = $dimension;
    }

}
Run Code Online (Sandbox Code Playgroud)

Jer*_*son 4

为了回答您的问题,我建议不要太关注其中的命令部分。在 Laravel 5.1 中,该文件夹被重命名为“Jobs” - 请参阅;

https://laravel-news.com/2015/04/laravel-5-1/

这正是因为泰勒觉得人们太拘泥于“命令”这个词了。

另请参阅http://www.laravelpodcast.com/episodes/6823-episode-21-commands-pipelines-and-packageshttps://laracasts.com/lessons/laravel-5-commands

Illuminate 包中的验证器类非常棒,http://laravel.com/api/5.0/Illuminate/Validation/Validator.html - 我猜我不确定问题是什么。

我想说,除非您有令人信服的理由为此使用 Command 类,否则不要这样做。另请参阅:http://www.laravelpodcast.com/episodes/9313-episode-23-new-beginnings-envoyer-laravel-5-1

我谦虚地建议您可能问了错误的问题,也许您不需要使用命令来处理这个问题。

这可能是您正在寻找的答案:https ://mattstauffer.co/blog/laravel-5.0-validateswhenresolved

use Illuminate\Contracts\Validation\ValidatesWhenResolved;

如果这不起作用,请注册 Larachat,http://larachat.co/ - 一个专门针对此类事情的 Slack 频道。Laravel 帮助的最佳场所。(当然,Stack Overflow 除外)

这是我用来检查图像格式的一个小类,我想你可能会发现它很有用。

<?php
Class FileUploadFormat
{
public function is_image($image_path)
  {
      if (!$f = fopen($image_path, 'rb'))
      {
          return false;
      }

      $data = fread($f, 8);
      fclose($f);

      // signature checking
      $unpacked = unpack("H12", $data);
      if (array_pop($unpacked) == '474946383961' || array_pop($unpacked) == '474946383761') return "gif";
      $unpacked = unpack("H4", $data);
      if (array_pop($unpacked) == 'ffd8') return "jpg";
      $unpacked = unpack("H16", $data);
      if (array_pop($unpacked) == '89504e470d0a1a0a') return "png";
      return false;
  }
}
Run Code Online (Sandbox Code Playgroud)