123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- /**
- * Created by PhpStorm.
- * User: robin
- * Date: 7/1/16
- * Time: 10:17 PM
- */
-
- namespace Luticate\Utils\Business;
-
-
- use Luticate\Utils\Dbo\LuDbo;
- use Luticate\Utils\Dbo\LuDboConstraintException;
- use Luticate\Utils\Dbo\LuDboDeserializeException;
-
- class LuInjector
- {
- public static function inject($obj, string $methodName, array $parameters)
- {
- if (is_string($obj)) {
- $className = $obj;
- $objInstance = new $className();
- }
- else {
- $className = get_class($obj);
- $objInstance = $obj;
- }
- $reflect = new \ReflectionMethod($className, $methodName);
- $params = $reflect->getParameters();
- $docs = new LuMethodDocParser($reflect->getDocComment());
- $docs->parse();
-
- $args = [];
- foreach ($params as $param) {
-
- try {
- $value = null;
- if (array_key_exists($param->getName(), $parameters)) {
- $value = static::getParam($param, $parameters[$param->getName()]);
- }
- else if ($param->isOptional()) {
- $value = $param->getDefaultValue();
- }
- else {
- throw new LuDboDeserializeException("Missing parameter '" . $param->getName() . "'", 400);
- }
-
- if (array_key_exists($param->getName(), $docs->getParams())) {
- $doc = $docs->getParams()[$param->getName()];
- if (is_null($value) && $doc->isNotNull()) {
- throw new LuDboConstraintException("Parameter '" . $param->getName() . "' can not be null", 400);
- }
-
- if (!is_null($value)) {
- $value->checkConstraints($doc->getConstraints());
- }
- }
- $args[$param->getName()] = $value;
- }
- catch (LuDboConstraintException $e)
- {
- $paramName = $param->getName();
- throw new LuBusinessException("Invalid value for '${paramName}': " . $e->getMessage(), 400, $e);
- }
- catch (LuDboDeserializeException $e)
- {
- $paramName = $param->getName();
- throw new LuBusinessException("Failed to deserialize '${paramName}': " . $e->getMessage(), 400, $e);
- }
- }
-
- $result = call_user_func_array(array($objInstance, $methodName), $args);
- return $result;
- }
-
- public static function getClassName(\ReflectionParameter $param) {
- preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
- return isset($matches[1]) ? $matches[1] : null;
- }
-
- public static function getParam(\ReflectionParameter $param, $value)
- {
- $typedValue = null;
- $className = static::getClassName($param);
- if (is_null($className)) {
- $typedValue = $value;
- }
- else {
- $class = $param->getClass();
- $className = is_null($class) ? "string" : $class->getName();
- try
- {
- $json = json_decode($value, true);
- $typedValue = LuDbo::deserializeValue($json, $className);
- }
- catch (\Exception $e)
- {
- if ($e instanceof LuDboDeserializeException || $e instanceof LuDboConstraintException) {
- throw $e;
- }
- throw new LuBusinessException("Unable to parse JSON value for '" . $param->getName() . "'", 400, $e);
- }
- }
- return $typedValue;
- }
- }
|