You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuInjector.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: robin
  5. * Date: 7/1/16
  6. * Time: 10:17 PM
  7. */
  8. namespace Luticate\Utils\Business;
  9. use Luticate\Utils\Dbo\LuDbo;
  10. use Luticate\Utils\Dbo\LuDboConstraintException;
  11. use Luticate\Utils\Dbo\LuDboDeserializeException;
  12. class LuInjector
  13. {
  14. public static function inject($obj, string $methodName, array $parameters)
  15. {
  16. if (is_string($obj)) {
  17. $className = $obj;
  18. $objInstance = new $className();
  19. }
  20. else {
  21. $className = get_class($obj);
  22. $objInstance = $obj;
  23. }
  24. $reflect = new \ReflectionMethod($className, $methodName);
  25. $params = $reflect->getParameters();
  26. $docs = new LuMethodDocParser($reflect->getDocComment());
  27. $docs->parse();
  28. $args = [];
  29. foreach ($params as $param) {
  30. try {
  31. $value = null;
  32. if (array_key_exists($param->getName(), $parameters)) {
  33. $value = static::getParam($param, $parameters[$param->getName()]);
  34. }
  35. else if ($param->isOptional()) {
  36. $value = $param->getDefaultValue();
  37. }
  38. else {
  39. throw new LuDboDeserializeException("Missing parameter '" . $param->getName() . "'", 400);
  40. }
  41. if (array_key_exists($param->getName(), $docs->getParams())) {
  42. $doc = $docs->getParams()[$param->getName()];
  43. if (is_null($value) && $doc->isNotNull()) {
  44. throw new LuDboConstraintException("Parameter '" . $param->getName() . "' can not be null", 400);
  45. }
  46. if (!is_null($value)) {
  47. $value->checkConstraints($doc->getConstraints());
  48. }
  49. }
  50. $args[$param->getName()] = $value;
  51. }
  52. catch (LuDboConstraintException $e)
  53. {
  54. $paramName = $param->getName();
  55. throw new LuBusinessException("Invalid value for '${paramName}': " . $e->getMessage(), 400, $e);
  56. }
  57. catch (LuDboDeserializeException $e)
  58. {
  59. $paramName = $param->getName();
  60. throw new LuBusinessException("Failed to deserialize '${paramName}': " . $e->getMessage(), 400, $e);
  61. }
  62. }
  63. $result = call_user_func_array(array($objInstance, $methodName), $args);
  64. return $result;
  65. }
  66. public static function getClassName(\ReflectionParameter $param) {
  67. preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
  68. return isset($matches[1]) ? $matches[1] : null;
  69. }
  70. public static function getParam(\ReflectionParameter $param, $value)
  71. {
  72. $typedValue = null;
  73. $className = static::getClassName($param);
  74. if (is_null($className)) {
  75. $typedValue = $value;
  76. }
  77. else {
  78. $class = $param->getClass();
  79. $className = is_null($class) ? "string" : $class->getName();
  80. try
  81. {
  82. $json = json_decode($value, true);
  83. $typedValue = LuDbo::deserializeValue($json, $className);
  84. }
  85. catch (\Exception $e)
  86. {
  87. if ($e instanceof LuDboDeserializeException || $e instanceof LuDboConstraintException) {
  88. throw $e;
  89. }
  90. throw new LuBusinessException("Unable to parse JSON value for '" . $param->getName() . "'", 400, $e);
  91. }
  92. }
  93. return $typedValue;
  94. }
  95. }