Code Monkey home page Code Monkey logo

dms-filter's Introduction

DMS Filter Component

This library provides a service that can be used to filter object values based on annotations

Latest Stable Version Total Downloads Latest Unstable Version License SensioLabsInsight Build StatusUnit Tests

Install

Use composer to add DMS\Filter to your app

composer require dms/dms-filter

Usage

Annotation way

Your Entity:

<?php

namespace App\Entity;

//Import Annotations
use DMS\Filter\Rules as Filter;

class User
{

    /**
    * @Filter\StripTags()
    * @Filter\Trim()
    * @Filter\StripNewlines()
    *
    * @var string
    */
    public string $name;

    /**
    * @Filter\StripTags()
    * @Filter\Trim()
    * @Filter\StripNewlines()
    *
    * @var string
    */
    public string $email;

}
?>

Filtering:

<?php
    //Get Doctrine Reader
    $reader = new Annotations\AnnotationReader();
    $reader->setEnableParsePhpImports(true);

    //Load AnnotationLoader
    $loader = new Mapping\Loader\AnnotationLoader($reader);
    $this->loader = $loader;

    //Get a MetadataFactory
    $metadataFactory = new Mapping\ClassMetadataFactory($loader);

    //Get a Filter
    $filter = new DMS\Filter\Filter($metadataFactory);


    //Get your Entity
    $user = new App\Entity\User();
    $user->name = "My <b>name</b>";
    $user->email = " [email protected]";

    //Filter you entity
    $filter->filter($user);

    echo $user->name; //"My name"
    echo $user->email; //"[email protected]"
?>

Full example: https://gist.github.com/1098352

Attribute way

Your Entity:

<?php

namespace App\Entity;

//Import Attributes
use DMS\Filter\Rules as Filter;

class User
{
    #[Filter\StripTags]
    #[Filter\Trim]
    #[Filter\StripNewlines]
    public string $name;

    #[Filter\StripTags]
    #[Filter\Trim]
    #[Filter\StripNewlines]
    public string $email;
}
?>

Filtering:

<?php
    //Load AttributeLoader
    $loader = new Mapping\Loader\AttributeLoader();
    $this->loader = $loader;

    //Get a MetadataFactory
    $metadataFactory = new Mapping\ClassMetadataFactory($loader);

    //Get a Filter
    $filter = new DMS\Filter\Filter($metadataFactory);


    //Get your Entity
    $user = new App\Entity\User();
    $user->name = "My <b>name</b>";
    $user->email = " [email protected]";

    //Filter you entity
    $filter->filter($user);

    echo $user->name; //"My name"
    echo $user->email; //"[email protected]"
?>

Dependencies

This package relies on these external libraries:

  • Doctrine Annotations

Contributing

Feel free to send pull requests, just follow these guides:

  • Fork
  • Code
  • Test
    • Just create FilterTestCase and run phpunit
  • Submit PR

Credits

This library is inspired by the Symfony 2 Validator component and is meant to work alongside it.

Symfony 2 Validator: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator

dms-filter's People

Contributors

dependabot[bot] avatar kdadler avatar maxmilash avatar raphaeldealmeida avatar rdohms avatar tobias-trozowski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

dms-filter's Issues

Filter: Callback

Callback Filter should be applied on a property of an object.

Inputs

  • string: a method on the object itself
  • array: a method on a class in the format {'Class', 'method'}
  • closure: a closure

The return value will replace the current value of the property.
Not all possibilities above may be available when using the filter from an annotation.

Structural impact:
This filter must receive the object itself, especially for the string input, as it needs to call a method on that object. This is not currently supported by the library and needs to be implemented.

XML/YML support

The README states this is only configured with annotations, it seems the config could also be loaded from yml/xml as well to make it more inline with other components such as validator or doctrine.

What are the chances of this?

The current loader seems to be similar to how the symfony validation component does it for annotations.

Composer fails to clone this repo

Composer tries to get this repo as a requirement of dms-filter-bundle v1.1.1, but it fails to clone it:

"require": {
    "dms/dms-filter-bundle":               "1.1.*",
}
[RuntimeException]                                                                           
  Failed to clone https://github.com/rdohms/DMS-Filter.git via git, https protocols, aborting. 

  - git://github.com/rdohms/DMS-Filter.git                                                     
    fatal: remote error:                                                                       
      Repository not found.                                                                    

  - https://github.com/rdohms/DMS-Filter.git                                                   
    fatal: Authentication failed 

I am running composer install with the --prefer-source option on.

I noticed that the repo name that is trying to clone is different from the one you can get directly from main page, so probably it's a typo somewhere, the problem is that I don't know where :P

Not Working Read The Readme Docs

Please Move All *.php' To New Folder Filter

And Fixed The Readme Docs $filter->filter($user); to $filter->filterEntity($user)

No licence

Would it be possible to add a licence to this bundle please?

Thanks

Filter: Zend

Implement filter to leverage the use of Zend\Filter classes.

Sample:
@filter\Zend('Zend\Filter[name]', [options])

Filters can be references by either FQN or Shortname

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.