-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from cergey-obr/attribute-support
Added support for php attributes, new loader
- Loading branch information
Showing
30 changed files
with
315 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ Use composer to add DMS\Filter to your app | |
|
||
## Usage | ||
|
||
### Annotation way | ||
|
||
Your Entity: | ||
|
||
```php | ||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class Alnum extends RegExp | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class Alpha extends RegExp | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class BooleanScalar extends Rule | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class Digits extends RegExp | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class FloatScalar extends Rule | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class HtmlEntities extends Rule | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class IntScalar extends Rule | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class Laminas extends Rule | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class PregReplace extends Rule | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class RegExp extends Rule | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class StripNewlines extends Rule | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class StripTags extends Rule | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class ToLower extends Rule | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class ToUpper extends Rule | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class Trim extends Rule | ||
{ | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
* | ||
* @Annotation | ||
*/ | ||
#[\Attribute] | ||
class Zend extends Rule | ||
{ | ||
/** | ||
|
Oops, something went wrong.