Skip to content

Commit

Permalink
Add attribute support
Browse files Browse the repository at this point in the history
  • Loading branch information
MisatoTremor committed Nov 23, 2021
1 parent 87ede47 commit 5a3d4d4
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Annotation/ImportExclude.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

namespace Avro\CsvBundle\Annotation;

use Attribute;

/**
* @Annotation
*/
#[Attribute(Attribute::TARGET_PROPERTY)]
class ImportExclude
{
}
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,25 @@ class Client
* @ImportExclude
*/
protected $password;
// ...
}
```

Since PHP 8 you can also use it as an attribute like this
```php
namespace Avro\CrmBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Avro\CsvBundle\Annotation\ImportExclude;

#[ORM\Entity]
class Client
{
#[ORM\Column(type: 'string', length: 100, nullable: true)]
#[ImportExclude]
protected string $password;
// ...
}
```

Importing
Expand Down
27 changes: 27 additions & 0 deletions Tests/AnnotationTestEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Avro\CsvBundle\Tests;

use Avro\CsvBundle\Annotation\ImportExclude;

class AnnotationTestEntity
{
protected $id;

/**
* @ImportExclude
*/
protected $field1;

#[ImportExclude]
protected $field2;

protected $assoc;

protected $custom;
}
86 changes: 86 additions & 0 deletions Tests/Util/FieldRetrieverAnnotationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Avro\CsvBundle\Tests\Util;

use Avro\CaseBundle\Util\CaseConverter;
use Avro\CsvBundle\Tests\AnnotationTestEntity;
use Avro\CsvBundle\Util\FieldRetriever;
use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use const PHP_VERSION_ID;

class FieldRetrieverAnnotationTest extends TestCase
{
/**
* @var FieldRetriever
*/
protected $fieldRetriever;
/**
* @var string
*/
protected $class;

public function setUp(): void
{
$annotationReader = new AnnotationReader();
$caseConverter = $this->createMock(CaseConverter::class);
$caseConverter
->method('convert')
->willReturnMap(
[
['', 'title', ''],
['id', 'title', 'Id'],
['field1', 'title', 'Field1'],
['field2', 'title', 'Field2'],
['assoc', 'title', 'Assoc'],
['custom', 'title', 'Custom'],
['', 'camel', ''],
['id', 'camel', 'id'],
['field1', 'camel', 'field1'],
['field2', 'camel', 'field2'],
['assoc', 'camel', 'assoc'],
['custom', 'camel', 'custom'],
]
);
$this->fieldRetriever = new FieldRetriever($annotationReader, $caseConverter);
$this->class = AnnotationTestEntity::class;
}

public function testGetAnnotationFields(): void
{
if (PHP_VERSION_ID >= 80000) {
$this->markTestSkipped('This test requires PHP < 8.0');
}
$this->assertEquals(
[
'0' => '',
'1' => 'Id',
'2' => 'Field2',
'3' => 'Assoc',
'4' => 'Custom',
],
$this->fieldRetriever->getFields($this->class)
);
}

public function testGetAttributeFields(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('This test requires PHP >= 8.0');
}
$this->assertEquals(
[
'0' => '',
'1' => 'id',
'2' => 'assoc',
'3' => 'custom',
],
$this->fieldRetriever->getFields($this->class, 'camel')
);
}
}
8 changes: 8 additions & 0 deletions Util/FieldRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public function getFields($class, $format = 'title', $copyToKey = false)
$addField = false;
}
}
if (PHP_VERSION_ID >= 80000) {
foreach ($property->getAttributes() as $attribute) {
$attributeName = $attribute->getName();
if (ImportExclude::class === $attributeName) {
$addField = false;
}
}
}

if ($addField) {
$fields[] = $this->caseConverter->convert($property->getName(), $format);
Expand Down

0 comments on commit 5a3d4d4

Please sign in to comment.