-
Notifications
You must be signed in to change notification settings - Fork 0
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
10 changed files
with
319 additions
and
23 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 |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
*/ | ||
class Factory | ||
{ | ||
private $extensions = []; | ||
protected $extensions = []; | ||
|
||
/** | ||
* Constructor. | ||
|
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,89 @@ | ||
<?php | ||
|
||
namespace Mapped; | ||
|
||
/** | ||
* ReflectionFactory (Experimental). | ||
*/ | ||
class ReflectionFactory extends Factory | ||
{ | ||
private $mappings = []; | ||
|
||
/** | ||
* @see Factory::__construct() | ||
*/ | ||
public function __construct(array $extensions = []) | ||
{ | ||
parent::__construct($extensions); | ||
$this->mappings = [ | ||
'string' => $this->string(), | ||
'int' => $this->int(), | ||
'float' => $this->float(), | ||
'bool' => $this->bool(), | ||
'array' => $this->mapping()->multiple(), | ||
]; | ||
} | ||
|
||
/** | ||
* Creates a mapping from given type. | ||
* | ||
* @param string $type The type | ||
* | ||
* @return Mapping | ||
*/ | ||
public function of($type) | ||
{ | ||
$l2c = substr($type, -2); | ||
if ('[]' === $l2c) { | ||
$type = substr($type, 0, -2); | ||
} | ||
|
||
if (array_key_exists($type, $this->mappings)) { | ||
$mapping = $this->mappings[$type]; | ||
} else { | ||
$refl = new \ReflectionClass($type); | ||
$mapping = new Mapping(new Emitter, $this->extensions); | ||
|
||
foreach ($refl->getProperties() as $prop) { | ||
if ($child = $this->mappingFromProp($prop)) { | ||
$mapping->addChild($prop->getName(), $child); | ||
} | ||
} | ||
|
||
$mapping->transformTo($type); | ||
} | ||
|
||
if ('[]' === $l2c) { | ||
$mapping = $mapping->multiple(); | ||
} | ||
|
||
return $mapping; | ||
} | ||
|
||
/** | ||
* Creates a mapping from `ReflectionProperty`. | ||
* | ||
* @param ReflectionProperty $prop The reflection property | ||
* | ||
* @return Mapping | ||
*/ | ||
private function mappingFromProp(\ReflectionProperty $prop) | ||
{ | ||
$comment = $prop->getDocComment(); | ||
preg_match('/@var\s+(.*)[\s|\n]/', $comment, $matches); | ||
|
||
if (!isset($matches[1])) { | ||
return; | ||
} | ||
|
||
$annotation = $matches[1]; | ||
$type = $annotation; | ||
|
||
if (ctype_upper($type[0]) && !class_exists($type)) { | ||
$ns = $prop->getDeclaringClass()->getNamespaceName(); | ||
$type = $ns . '\\' . $type; | ||
} | ||
|
||
return $this->of($type)->optional(); | ||
} | ||
} |
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
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
Oops, something went wrong.