-
-
Notifications
You must be signed in to change notification settings - Fork 333
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
4 changed files
with
141 additions
and
30 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
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,67 @@ | ||
<?php | ||
|
||
/** | ||
* League.Csv (https://csv.thephpleague.com) | ||
* | ||
* (c) Ignace Nyamagana Butera <[email protected]> | ||
* | ||
* 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\Serializer; | ||
|
||
use Attribute; | ||
use ReflectionClass; | ||
use ReflectionException; | ||
use ReflectionMethod; | ||
use ValueError; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS)] | ||
final class MapRecord | ||
{ | ||
public function __construct( | ||
/** @var array<string> $afterMapping */ | ||
public readonly array $afterMapping, | ||
public readonly bool $allowEmptyStringAsNull = false, | ||
) { | ||
foreach ($this->afterMapping as $method) { | ||
if (!is_string($method)) { | ||
throw new ValueError('The method names must be strings.'); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @return array<ReflectionMethod> | ||
*/ | ||
public function afterMappingMethods(ReflectionClass $class): array | ||
{ | ||
$methods = []; | ||
foreach ($this->afterMapping as $method) { | ||
try { | ||
$accessor = $class->getMethod($method); | ||
} catch (ReflectionException $exception) { | ||
throw new MappingFailed('The method `'.$method.'` is not defined on the `'.$class->getName().'` class.', 0, $exception); | ||
} | ||
|
||
if (0 !== $accessor->getNumberOfRequiredParameters()) { | ||
throw new MappingFailed('The method `'.$class->getName().'::'.$accessor->getName().'` has too many required parameters.'); | ||
} | ||
|
||
$methods[] = $accessor; | ||
} | ||
|
||
return $methods; | ||
} | ||
|
||
public function resolveOptions(?MapCell $mapCell = null): array | ||
{ | ||
return [ | ||
...['allowEmptyStringAsNull' => $this->allowEmptyStringAsNull], | ||
...$mapCell?->options ?? [], | ||
]; | ||
} | ||
} |