├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json └── src ├── ServiceProvider.php └── Validator.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-validator-color` will be documented in this file. 4 | 5 | ### 3.0.2 6 | - Fixed broken use statement 7 | 8 | ### 3.0.1 9 | - Fixed test file 10 | 11 | ### 3.0.0 12 | - Changed Namespaces 13 | 14 | ### 2.0.0 15 | - Changing repository ownership to 'tylercd100' 16 | 17 | ### 1.0.0 18 | - Initial release and connected with packagist 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Tylercd100 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Validate colors with Laravel 5 2 | [![Latest Version](https://img.shields.io/github/release/tylercd100/laravel-validator-color.svg?style=flat-square)](https://github.com/tylercd100/laravel-validator-color/releases) 3 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 4 | [![Build Status](https://travis-ci.org/tylercd100/laravel-validator-color.svg?branch=master)](https://travis-ci.org/tylercd100/laravel-validator-color) 5 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/tylercd100/laravel-validator-color/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/tylercd100/laravel-validator-color/?branch=master) 6 | [![Code Coverage](https://scrutinizer-ci.com/g/tylercd100/laravel-validator-color/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/tylercd100/laravel-validator-color/?branch=master) 7 | [![Dependency Status](https://www.versioneye.com/user/projects/56f3252c35630e0029db0187/badge.svg?style=flat)](https://www.versioneye.com/user/projects/56f3252c35630e0029db0187) 8 | [![Total Downloads](https://img.shields.io/packagist/dt/tylercd100/laravel-validator-color.svg?style=flat-square)](https://packagist.org/packages/tylercd100/laravel-validator-color) 9 | 10 | This package will let you validate that a certain value is a valid CSS color string. 11 | 12 | ## Installation 13 | 14 | Install via [composer](https://getcomposer.org/) - In the terminal: 15 | ```bash 16 | composer require tylercd100/laravel-validator-color 17 | ``` 18 | 19 | Now add the following to the `providers` array in your `config/app.php` 20 | ```php 21 | Tylercd100\Validator\Color\ServiceProvider::class 22 | ``` 23 | 24 | ## Usage 25 | 26 | ```php 27 | // Test any color type 28 | Validator::make(['test' => '#454ACF'], ['test' => 'color']); 29 | 30 | // Test for rgb 31 | Validator::make(['test' => 'rgb(0, 200, 150)'], ['test' => 'color_rgb']); 32 | 33 | // Test for rgba 34 | Validator::make(['test' => 'rgba(0, 200, 150, 0.52)'], ['test' => 'color_rgba']); 35 | 36 | // Test for hex 37 | Validator::make(['test' => '#333'], ['test' => 'color_hex']); 38 | 39 | // Test for css color keyword 40 | Validator::make(['test' => 'gold'], ['test' => 'color_keyword']); -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tylercd100/laravel-validator-color", 3 | "description": "Validate that a certain value is a valid CSS color string in Laravel 5", 4 | "keywords": [ 5 | "laravel", 6 | "validator", 7 | "color", 8 | "tylercd100" 9 | ], 10 | "homepage": "https://github.com/tylercd100/laravel-validator-color", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Tyler Arbon", 15 | "email": "tylercd100@gmail.com" 16 | } 17 | ], 18 | "autoload":{ 19 | "psr-4":{ 20 | "Tylercd100\\Validator\\Color\\":"src/" 21 | } 22 | }, 23 | "autoload-dev": { 24 | "psr-4": { 25 | "Tylercd100\\Validator\\Color\\Tests\\": "tests/" 26 | } 27 | }, 28 | "minimum-stability": "stable", 29 | "require": { 30 | "illuminate/validation": "^5.0|^6.0|^7.0", 31 | "illuminate/support": "^5.0|^6.0|^7.0", 32 | "php": "^5.5.9|^7.0" 33 | }, 34 | "require-dev": { 35 | "phpunit/phpunit": "^4.8|^5.0|^6.0|^7.0|^8.0", 36 | "orchestra/testbench": "^3.2" 37 | }, 38 | "suggest": { 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->resolving('validator', function ($factory, $app) { 28 | 29 | $colorValidator = new ColorValidator(); 30 | 31 | $factory->extend('color', function ($attribute, $value, $parameters, $validator) use ($colorValidator) { 32 | return $colorValidator->isColor($value); 33 | }); 34 | 35 | $factory->extend('color_hex', function ($attribute, $value, $parameters, $validator) use ($colorValidator) { 36 | return $colorValidator->isColorAsHex($value); 37 | }); 38 | 39 | $factory->extend('color_rgb', function ($attribute, $value, $parameters, $validator) use ($colorValidator) { 40 | return $colorValidator->isColorAsRGB($value); 41 | }); 42 | 43 | $factory->extend('color_rgba', function ($attribute, $value, $parameters, $validator) use ($colorValidator) { 44 | return $colorValidator->isColorAsRGBA($value); 45 | }); 46 | 47 | $factory->extend('color_keyword', function ($attribute, $value, $parameters, $validator) use ($colorValidator) { 48 | return $colorValidator->isColorAsKeyword($value); 49 | }); 50 | }); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Validator.php: -------------------------------------------------------------------------------- 1 | isColorAsKeyword($color) 23 | || $this->isColorAsHex($color) 24 | || $this->isColorAsRGB($color) 25 | || $this->isColorAsRGBA($color); 26 | } 27 | 28 | /** 29 | * Check if color is in hexadecimal format. 30 | * 31 | * @param string $color 32 | * @return bool 33 | */ 34 | public function isColorAsHex($color) 35 | { 36 | return $this->isColorAsLongHex($color) || $this->isColorAsShortHex($color); 37 | } 38 | 39 | /** 40 | * Check if color is in rgb format. 41 | * 42 | * @param string $color 43 | * @return bool 44 | */ 45 | public function isColorAsRGB($color) 46 | { 47 | preg_match('/^(rgb)\(([01]?\d\d?|2[0-4]\d|25[0-5])(\W+)([01]?\d\d?|2[0-4]\d|25[0-5])\W+(([01]?\d\d?|2[0-4]\d|25[0-5])\))$/i',$color,$m); 48 | return count($m) > 0; 49 | } 50 | 51 | /** 52 | * Check if color is in rgba format. 53 | * 54 | * @param string $color 55 | * @return bool 56 | */ 57 | public function isColorAsRGBA($color) 58 | { 59 | preg_match('/^(rgba)\(([01]?\d\d?|2[0-4]\d|25[0-5])\W+([01]?\d\d?|2[0-4]\d|25[0-5])\W+([01]?\d\d?|2[0-4]\d|25[0-5])\)?\W+([01](\.\d+)?)\)$/i',$color,$m); 60 | return count($m) > 0; 61 | } 62 | 63 | /** 64 | * Check if color is in short hexadecimal format. 65 | * 66 | * @param string $color 67 | * @return bool 68 | */ 69 | public function isColorAsShortHex($color) 70 | { 71 | preg_match('/^#(\d|a|b|c|d|e|f){3}$/i', $color, $m); 72 | return count($m) > 0; 73 | } 74 | 75 | /** 76 | * Check if color is in long hexadecimal format. 77 | * 78 | * @param string $color 79 | * @return bool 80 | */ 81 | public function isColorAsLongHex($color) 82 | { 83 | preg_match('/^#(\d|a|b|c|d|e|f){6}$/i', $color, $m); 84 | return count($m) > 0; 85 | } 86 | 87 | /** 88 | * Check if color is in list of supported colors. 89 | * 90 | * @param string $color 91 | * @return bool 92 | */ 93 | public function isColorAsKeyword($color) 94 | { 95 | return in_array($color, $this->colors); 96 | } 97 | } 98 | --------------------------------------------------------------------------------