Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for php attributes, new loader #90

Merged
merged 6 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

strategy:
matrix:
php: [7.4, 8.0, 8.1]
php: [8.0, 8.1]
dependency-version: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.dependency-version }}
Expand Down
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Use composer to add DMS\Filter to your app

## Usage

### Annotation way

Your Entity:

```php
Expand Down Expand Up @@ -81,6 +83,60 @@ Filtering:

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

### Attribute way

Your Entity:

```php
<?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
<?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:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],

"require": {
"php": "^7.4 || ~8.0 || ~8.1",
"php": "~8.0 || ~8.1",
"doctrine/annotations": "^1.13",
"laminas/laminas-zendframework-bridge": "^1.0"
},
Expand Down
32 changes: 32 additions & 0 deletions src/DMS/Filter/Mapping/Loader/AttributeLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
cergey-obr marked this conversation as resolved.
Show resolved Hide resolved
declare(strict_types=1);

namespace DMS\Filter\Mapping\Loader;

use DMS\Filter\Mapping\ClassMetadataInterface;
use DMS\Filter\Rules\Rule;
use ReflectionAttribute;
use ReflectionProperty;

class AttributeLoader implements LoaderInterface
{
public function loadClassMetadata(ClassMetadataInterface $metadata): bool
{
foreach ($metadata->getReflectionClass()->getProperties() as $property) {
$this->readProperty($property, $metadata);
}

return true;
}

private function readProperty(ReflectionProperty $property, ClassMetadataInterface $metadata): void
{
if ($property->getDeclaringClass()->getName() !== $metadata->getClassName()) {
return;
}

foreach ($property->getAttributes(Rule::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
$metadata->addPropertyRule($property->getName(), $attribute->newInstance());
}
}
}
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/Alnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* @Annotation
*/
#[\Attribute]
class Alnum extends RegExp
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/Alpha.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* @Annotation
*/
#[\Attribute]
class Alpha extends RegExp
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/BooleanScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* @Annotation
*/
#[\Attribute]
class BooleanScalar extends Rule
{
}
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*
* @Annotation
*/
#[\Attribute]
class Callback extends Rule
{
public const SELF_METHOD_TYPE = 'self_method';
Expand Down
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/Digits.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* @Annotation
*/
#[\Attribute]
class Digits extends RegExp
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/FloatScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* @Annotation
*/
#[\Attribute]
class FloatScalar extends Rule
{
}
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/HtmlEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*
* @Annotation
*/
#[\Attribute]
class HtmlEntities extends Rule
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/IntScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* @Annotation
*/
#[\Attribute]
class IntScalar extends Rule
{
}
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/Laminas.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*
* @Annotation
*/
#[\Attribute]
class Laminas extends Rule
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/PregReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*
* @Annotation
*/
#[\Attribute]
class PregReplace extends Rule
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/RegExp.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*
* @Annotation
*/
#[\Attribute]
class RegExp extends Rule
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/StripNewlines.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* @Annotation
*/
#[\Attribute]
class StripNewlines extends Rule
{
}
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/StripTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* @Annotation
*/
#[\Attribute]
class StripTags extends Rule
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/ToLower.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* @Annotation
*/
#[\Attribute]
class ToLower extends Rule
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/ToUpper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* @Annotation
*/
#[\Attribute]
class ToUpper extends Rule
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/Trim.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* @Annotation
*/
#[\Attribute]
class Trim extends Rule
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/DMS/Filter/Rules/Zend.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*
* @Annotation
*/
#[\Attribute]
class Zend extends Rule
{
/**
Expand Down
Loading
Loading