vendor/univapay/php-sdk/src/Univapay/Utility/Json/JsonSchema.php line 105

Open in your IDE?
  1. <?php
  2. namespace Univapay\Utility\Json;
  3. use Univapay\Utility\FunctionalUtils;
  4. use ReflectionClass;
  5. use Univapay\Utility\StringUtils;
  6. class JsonSchema
  7. {
  8.     public $components;
  9.     public $prefix;
  10.     public $targetClass;
  11.     public function __construct($targetClass$prefix null)
  12.     {
  13.         $this->components = [];
  14.         $this->targetClass $targetClass;
  15.         $this->prefix $prefix;
  16.     }
  17.     
  18.     /**
  19.      * @param Callable $formatter - Take in the following variables:
  20.      *      $value - The value at the path
  21.      *      $contextRoot - The root at the same level with $value
  22.      *      $root - The original object that is being parsed
  23.      *      $additionalArgs - An array that usually contains the context,
  24.      *          but may be injected with additional args for object construction
  25.      */
  26.     public function with($path$required false$formatter 'Univapay\Utility\FunctionalUtils::identity')
  27.     {
  28.         array_push($this->components, new SchemaComponent($this->prefix '/' .$path$required$formatter));
  29.         return $this;
  30.     }
  31.     public function withNested(JsonSchema $schema)
  32.     {
  33.         foreach ($schema->components as $element) {
  34.             array_push($this->components$element);
  35.         }
  36.         return $this;
  37.     }
  38.     public function upsert($path$required false$formatter 'Univapay\Utility\FunctionalUtils::identity')
  39.     {
  40.         $index FunctionalUtils::arrayFindIndex($this->components, function ($value) use ($path) {
  41.             return $value->path === $path;
  42.         });
  43.         if ($index !== null) {
  44.             $this->components array_replace(
  45.                 $this->components,
  46.                 [$index => new SchemaComponent($this->prefix '/' .$path$required$formatter)]
  47.             );
  48.             return $this;
  49.         } else {
  50.             return $this->with($path$required $required$formatter $formatter);
  51.         }
  52.     }
  53.     private function getValues($json, array $parent null, array $additionalArgs = [])
  54.     {
  55.         return array_map(function ($component) use ($json$parent$additionalArgs) {
  56.             $path_parts explode('/'$component->path);
  57.             $value JsonSchema::getField($json$component->required$path_parts);
  58.             if ($value === null) {
  59.                 if ($component->required) {
  60.                     throw new RequiredValueNotFoundException($component->path);
  61.                 } else {
  62.                     return null;
  63.                 }
  64.             }
  65.             return call_user_func($component->formatter$value$json$parent$additionalArgs);
  66.         }, $this->components);
  67.     }
  68.     public function parse($json, array $additionalArgs = [], array $parent null)
  69.     {
  70.         $targetClass = new ReflectionClass($this->targetClass);
  71.         $arguments array_merge($this->getValues($json$parent$additionalArgs), $additionalArgs);
  72.         return $targetClass->newInstanceArgs($arguments);
  73.     }
  74.     public function getParser(array $context = [])
  75.     {
  76.         return function ($json$contextRoot null, array $root null, array $additionalArgs = []) use ($context) {
  77.             $additionalArgs = empty($context) ? $additionalArgs $context;
  78.             return $this->parse($json$additionalArgs, isset($root) ? $root $contextRoot);
  79.         };
  80.     }
  81.     public static function fromClass($targetClass$snakeCase true$includeParentVars true)
  82.     {
  83.         $classVars FunctionalUtils::getClassVarsAssoc($targetClass$includeParentVars);
  84.         $newSchema = new JsonSchema($targetClass);
  85.         return array_reduce(
  86.             $classVars,
  87.             function ($schema$path) use ($snakeCase) {
  88.                 $realPath $snakeCase StringUtils::toSnakeCase($path) : $path;
  89.                 return $schema->with($realPath);
  90.             },
  91.             $newSchema
  92.         );
  93.     }
  94.     public static function getField($json$required, array $paths)
  95.     {
  96.         if ($json === null && !$required) {
  97.             return $json;
  98.         }
  99.         if (sizeof($paths) === 0) {
  100.             throw new NoSuchPathException(null);
  101.         }
  102.         $nextKey $paths[0];
  103.         if (!array_key_exists($nextKey$json)) {
  104.             if ($required) {
  105.                 throw new NoSuchPathException($nextKey);
  106.             } else {
  107.                 return null;
  108.             }
  109.         }
  110.         $nextJson $json[$nextKey];
  111.         if (sizeof($paths) === 1) {
  112.             return $nextJson;
  113.         } else {
  114.             try {
  115.                 return JsonSchema::getField($nextJson$requiredarray_slice($paths1));
  116.             } catch (NoSuchPathException $except) {
  117.                 throw new NoSuchPathException($nextKey '/' $except->path);
  118.             }
  119.         }
  120.     }
  121. }