Compare commits

..

No commits in common. "master" and "v1.0.2" have entirely different histories.

8 changed files with 38 additions and 170 deletions

View file

@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.3', '7.4', '8.0', '8.1', '8.2']
php-version: ['7.3', '7.4']
steps:
- name: Check out code into the workspace
uses: actions/checkout@v2
@ -35,4 +35,4 @@ jobs:
- name: Run tests
run: composer run-script phpunit-ci
- name: Coverage
run: bash <(curl -s https://codecov.io/bash)
run: bash <(curl -s https://codecov.io/bash)

View file

@ -11,9 +11,6 @@ jobs:
phpcs:
name: "PHP CodeSniffer"
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.3', '7.4', '8.0', '8.1', '8.2']
steps:
- name: Check out code into the workspace
uses: actions/checkout@v2
@ -22,9 +19,6 @@ jobs:
phpmd:
name: "PHP MessDetector"
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.3', '7.4', '8.0', '8.1', '8.2']
steps:
- name: Check out code into the workspace
uses: actions/checkout@v2
@ -34,19 +28,15 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
level: 'warning'
reporter: github-pr-check
standard: './phpmd.xml.dist'
standard: './phpmd.xml'
target_directory: 'src'
phpstan:
name: PHPStan
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.3', '7.4', '8.0', '8.1', '8.2']
runs-on: ubuntu-18.04
steps:
- name: Check out code into the workspace
uses: actions/checkout@v2
- name: Run PHPStan
uses: docker://oskarstark/phpstan-ga:1.8.0
uses: docker://oskarstark/phpstan-ga:0.12.88
with:
args: analyse src -c phpstan.neon --memory-limit=1G --no-progress
args: analyse src -c phpstan.neon --memory-limit=1G --no-progress

122
README.md
View file

@ -1,121 +1 @@
[![Build Status](https://github.com/retailcrm/url-validator/workflows/CI/badge.svg)](https://github.com/retailcrm/url-validator/actions)
[![Coverage](https://img.shields.io/codecov/c/gh/retailcrm/url-validator/master.svg?logo=codecov&logoColor=white)](https://codecov.io/gh/retailcrm/url-validator)
[![Latest stable](https://img.shields.io/packagist/v/retailcrm/url-validator.svg)](https://packagist.org/packages/retailcrm/url-validator)
[![PHP from Packagist](https://img.shields.io/packagist/php-v/retailcrm/url-validator.svg?logo=php&logoColor=white)](https://packagist.org/packages/retailcrm/url-validator)
# RetailCRM URL Validator
This validator will help you validate system URLs in your project using [`symfony/validator`](https://packagist.org/packages/symfony/validator).
# Table of contents
* [Requirements](#requirements)
* [Installation](#installation)
* [Usage](#usage)
## Requirements
* PHP 7.3 and above
* PHP's JSON support
* `symfony/validator`
## Installation
Follow those steps to install the library:
1. Download and install [Composer](https://getcomposer.org/download/) package manager.
2. Install the library from the Packagist by executing this command:
```bash
composer require retailcrm/url-validator:"^1"
```
3. Include the autoloader if it's not included, or you didn't use Composer before.
```php
require 'path/to/vendor/autoload.php';
```
Replace `path/to/vendor/autoload.php` with the correct path to Composer's `autoload.php`.
## Usage
You have to use Symfony Validator to work with this library.
Please refer to the [official documentation for the `symfony/validator`](https://symfony.com/doc/current/components/validator.html) to learn how to use it.
If you want to use `symfony/validator` with Symfony framework - you should use [this documentation](https://symfony.com/doc/current/validation.html).
After ensuring that you're using `symfony/validator` you can just append the `@CrmUrl()` annotation to the DTO entity field that contains system URL.
After that validator's `validate` call on this DTO will generate the proper violation messages for incorrect URLs.
Here's an example of the DTO (*note:* `@Assert\Url()` is optional):
```php
<?php
namespace App\Dto;
use Symfony\Component\Validator\Constraints as Assert;
use RetailCrm\Validator\CrmUrl;
class Connection
{
/**
* @var string
*
* @Assert\NotBlank()
* @Assert\Url()
* @CrmUrl()
*/
public $apiUrl;
}
```
And below you can find a complete example of usage (*note:* it requires `doctrine/annotations` and `symfony/cache` to work properly).
**Connection.php**
```php
<?php
namespace App;
use Symfony\Component\Validator\Constraints as Assert;
use RetailCrm\Validator\CrmUrl;
class Connection
{
/**
* @var string
*
* @Assert\NotBlank()
* @Assert\Url()
* @CrmUrl()
*/
public $apiUrl;
public function __construct(string $apiUrl)
{
$this->apiUrl = $apiUrl;
}
}
```
**app.php**
```php
namespace App;
// We assume that `app.php` is stored within a directory that is being autoloaded by Composer.
require __DIR__ . '/../vendor/autoload.php';
use Symfony\Component\Validator\Validation;
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader()
->getValidator();
$violations = $validator->validate(new Connection('https://test.retailcrm.pro'));
if (0 !== count($violations)) {
foreach ($violations as $violation) {
echo $violation->getMessage();
}
}
```
Validator for RetailCRM projects on Symfony

View file

@ -14,12 +14,12 @@
"require": {
"php": ">=7.3",
"ext-json": "*",
"symfony/validator": "^3 || ^4 || ^5 || ^6"
"symfony/validator": "^3 || ^4 || ^5"
},
"require-dev": {
"phpunit/phpunit": "^9.5.7",
"squizlabs/php_codesniffer": "3.*",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan": "^0.12.92",
"phpmd/phpmd": "^2.10"
},
"support": {
@ -40,9 +40,9 @@
"scripts": {
"phpunit": "./vendor/bin/phpunit -c phpunit.xml.dist --coverage-text",
"phpunit-ci": "@php -dpcov.enabled=1 -dpcov.directory=. -dpcov.exclude=\"~vendor~\" ./vendor/bin/phpunit --teamcity -c phpunit.xml.dist",
"phpmd": "./vendor/bin/phpmd src text ./phpmd.xml.dist",
"phpcs": "./vendor/bin/phpcs -p --standard=phpcs.xml.dist --runtime-set testVersion 7.3-8.2 --warning-severity=0",
"phpstan": "./vendor/bin/phpstan analyse -c phpstan.neon",
"phpmd": "./vendor/bin/phpmd src text ./phpmd.xml",
"phpcs": "./vendor/bin/phpcs -p src --runtime-set testVersion 7.3-8.0 && ./vendor/bin/phpcs -p tests --runtime-set testVersion 7.3-8.0 --warning-severity=0",
"phpstan": "./vendor/bin/phpstan analyse -c phpstan.neon src --memory-limit=-1",
"verify": [
"@phpcs",
"@phpmd",

View file

@ -5,43 +5,43 @@
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>Ruleset</description>
<rule ref="rulesets/controversial.xml"/>
<rule ref="rulesets/design.xml"/>
<rule ref="rulesets/naming.xml">
<exclude name="ShortVariable" />
<exclude name="LongVariable" />
<exclude name="LongClassName" />
<exclude name="ShortMethodName" />
<rule ref="rulesets/controversial.xml" />
<rule ref="rulesets/unusedcode.xml" />
<rule ref="rulesets/design.xml">
<exclude name="CouplingBetweenObjects" />
</rule>
<rule ref="rulesets/cleancode.xml">
<exclude name="StaticAccess" />
<exclude name="ElseExpression" />
</rule>
<rule ref="rulesets/unusedcode.xml">
<exclude name="UnusedFormalParameter" />
</rule>
<rule ref="rulesets/codesize.xml">
<exclude name="TooManyPublicMethods" />
<exclude name="TooManyFields" />
</rule>
<rule ref="rulesets/naming.xml">
<exclude name="ShortVariable" />
</rule>
<rule ref="rulesets/naming.xml/ShortVariable">
<properties>
<property name="minimum" value="2" />
</properties>
</rule>
<rule ref="rulesets/naming.xml/LongVariable">
<rule ref="rulesets/codesize.xml/TooManyPublicMethods">
<properties>
<property name="maximum" value="30" />
<property name="maxmethods" value="20" />
</properties>
</rule>
<rule ref="rulesets/naming.xml/LongClassName">
<rule ref="rulesets/codesize.xml/TooManyFields">
<properties>
<property name="maximum" value="80" />
<property name="maxfields" value="30" />
</properties>
</rule>
<rule ref="rulesets/naming.xml/ShortMethodName">
<rule ref="rulesets/design.xml/CouplingBetweenObjects">
<properties>
<property name="minimum" value="2" />
<property name="maximum" value="15" />
</properties>
</rule>
<exclude-pattern>tests/*</exclude-pattern>
</ruleset>

View file

@ -6,5 +6,3 @@ parameters:
paths:
- src
- tests
bootstrapFiles:
- ./vendor/autoload.php

View file

@ -19,6 +19,7 @@
<coverage>
<include>
<directory>src</directory>
<directory>dev</directory>
</include>
<report>
<clover outputFile="coverage.xml"/>

View file

@ -21,20 +21,19 @@ class CrmUrlValidatorTest extends TestCase
public function testValidateSuccess(): void
{
$validCrms = [
'https://retailcrm.tvoydom.ru',
'https://asd.retailcrm.ru',
'https://test.retailcrm.pro',
'https://raisa.retailcrm.es',
'https://blabla.simla.com',
'https://blabla.simlachat.com',
'https://blabla.simlachat.ru',
'https://blabla.ecomlogic.com',
'https://retailcrm.inventive.ru',
'https://crm.baucenter.ru',
'https://crm.holodilnik.ru',
'https://crm.eco.lanit.ru',
'https://ecom.inventive.ru',
'https://test.retailcrm.ru',
'https://test.retailcrm.pro',
'https://test.retailcrm.es',
'https://test.simla.com',
'https://test.simlachat.com',
'https://test.ecomlogic.com',
'https://test.retailcrm.io',
'https://test.simla.io'
'https://retailcrm.tvoydom.ru',
];
$translator = new class () implements TranslatorInterface, LocaleAwareInterface {