"Fossies" - the Fresh Open Source Software Archive

Member "neos-development-collection-7.0.1/Neos.Media/Classes/Validator/ImageOrientationValidator.php" (23 Feb 2021, 3910 Bytes) of package /linux/www/neos-development-collection-7.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ImageOrientationValidator.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 namespace Neos\Media\Validator;
    3 
    4 /*
    5  * This file is part of the Neos.Media package.
    6  *
    7  * (c) Contributors of the Neos Project - www.neos.io
    8  *
    9  * This package is Open Source Software. For the full copyright and license
   10  * information, please view the LICENSE file which was distributed with this
   11  * source code.
   12  */
   13 
   14 use Neos\Flow\Annotations as Flow;
   15 use Neos\Flow\Validation\Exception\InvalidValidationOptionsException;
   16 use Neos\Media\Domain\Model\ImageInterface;
   17 
   18 /**
   19  * Validator that checks the orientation (square, portrait, landscape) of a given image.
   20  *
   21  * Supported validator options are (array)allowedOrientations with one or two out of 'square', 'landcape' or 'portrait'.
   22  *
   23  * *Example*::
   24  *
   25  *   [at]Flow\Validate("$image", type="\Neos\Media\Validator\ImageOrientationValidator",
   26  *         options={ "allowedOrientations"={"square", "landscape"} })
   27  *
   28  * this would refuse an image that is in portrait orientation, but allow landscape and square ones.
   29  */
   30 class ImageOrientationValidator extends \Neos\Flow\Validation\Validator\AbstractValidator
   31 {
   32     /**
   33      * @var array
   34      */
   35     protected $supportedOptions = [
   36         'allowedOrientations' => [[], 'Array of image orientations, one or two out of \'square\', \'landcape\' or \'portrait\'', 'array', true]
   37     ];
   38 
   39     /**
   40      * The given $value is valid if it is an \Neos\Media\Domain\Model\ImageInterface of the
   41      * configured orientation (square, portrait and/or landscape)
   42      * Note: a value of NULL or empty string ('') is considered valid
   43      *
   44      * @param ImageInterface $image The image that should be validated
   45      * @return void
   46      * @api
   47      */
   48     protected function isValid($image)
   49     {
   50         $this->validateOptions();
   51         if (!$image instanceof ImageInterface) {
   52             $this->addError('The given value was not an Image instance.', 1328028604);
   53             return;
   54         }
   55         if (!in_array($image->getOrientation(), $this->options['allowedOrientations'])) {
   56             if (count($this->options['allowedOrientations']) === 1) {
   57                 reset($this->options['allowedOrientations']);
   58                 $allowedOrientation = current($this->options['allowedOrientations']);
   59                 $this->addError('The image orientation must be "%s".', 1328029406, [$allowedOrientation]);
   60             } else {
   61                 $this->addError('The image orientation "%s" is not allowed.', 1328029362, [$image->getOrientation()]);
   62             }
   63         }
   64     }
   65 
   66     /**
   67      * @return void
   68      * @throws InvalidValidationOptionsException if the configured validation options are incorrect
   69      */
   70     protected function validateOptions()
   71     {
   72         if (!isset($this->options['allowedOrientations'])) {
   73             throw new InvalidValidationOptionsException('The option "allowedOrientations" was not specified.', 1328028795);
   74         } elseif (!is_array($this->options['allowedOrientations']) || $this->options['allowedOrientations'] === []) {
   75             throw new InvalidValidationOptionsException('The option "allowedOrientations" must be an array with at least one element of "square", "portrait" or "landscape".', 1328028798);
   76         }
   77         foreach ($this->options['allowedOrientations'] as $orientation) {
   78             if ($orientation !== ImageInterface::ORIENTATION_LANDSCAPE
   79                 && $orientation !== ImageInterface::ORIENTATION_PORTRAIT
   80                 && $orientation !== ImageInterface::ORIENTATION_SQUARE) {
   81                 throw new InvalidValidationOptionsException(sprintf('The option "allowedOrientations" contains an invalid orientation "%s".', $orientation), 1328029114);
   82             }
   83         }
   84         if (count($this->options['allowedOrientations']) === 3) {
   85             throw new InvalidValidationOptionsException('The option "allowedOrientations" must contain at most two elements of "square", "portrait" or "landscape".', 1328029781);
   86         }
   87     }
   88 }