-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,070 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/** | ||
* League.Csv (https://csv.thephpleague.com) | ||
* | ||
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace League\Csv\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_PROPERTY)] | ||
final class Column | ||
{ | ||
/** | ||
* @param ?class-string $cast | ||
*/ | ||
public function __construct( | ||
public readonly string|int $offset, | ||
public readonly ?string $cast = null, | ||
public readonly array $castArguments = [] | ||
) { | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
/** | ||
* League.Csv (https://csv.thephpleague.com) | ||
* | ||
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace League\Csv; | ||
|
||
use League\Csv\TypeCasting\TypeCasting; | ||
use ReflectionMethod; | ||
use ReflectionProperty; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class CellMapper | ||
{ | ||
public function __construct( | ||
public readonly int $offset, | ||
private readonly ReflectionMethod|ReflectionProperty $accessor, | ||
private readonly TypeCasting $cast, | ||
) { | ||
} | ||
|
||
public function __invoke(object $object, ?string $value): void | ||
{ | ||
$type = (string) match (true) { | ||
$this->accessor instanceof ReflectionMethod => $this->accessor->getParameters()[0]->getType(), | ||
$this->accessor instanceof ReflectionProperty => $this->accessor->getType(), | ||
}; | ||
|
||
$value = $this->cast->toVariable($value, $type); | ||
|
||
match (true) { | ||
$this->accessor instanceof ReflectionMethod => $this->accessor->invoke($object, $value), | ||
$this->accessor instanceof ReflectionProperty => $this->accessor->setValue($object, $value), | ||
}; | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
/** | ||
* League.Csv (https://csv.thephpleague.com) | ||
* | ||
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace League\Csv; | ||
|
||
use ArrayIterator; | ||
use Iterator; | ||
use ReflectionException; | ||
use RuntimeException; | ||
|
||
class Mapper | ||
{ | ||
/** | ||
* @param class-string $className | ||
*/ | ||
public function __construct(private readonly string $className) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws RuntimeException | ||
* @throws ReflectionException | ||
*/ | ||
public function map(TabularDataReader $tabularDataReader): Iterator | ||
{ | ||
return $this($tabularDataReader, $tabularDataReader->getHeader()); | ||
} | ||
|
||
/** | ||
* @throws RuntimeException | ||
* @throws ReflectionException | ||
*/ | ||
public function __invoke(iterable $records, array $header): Iterator | ||
{ | ||
$mapper = new RecordMapper($this->className, $header); | ||
|
||
return match (true) { | ||
is_array($records) => new MapIterator(new ArrayIterator($records), $mapper(...)), | ||
default => new MapIterator($records, $mapper(...)), | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.