* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class TypeVisitor implements VisitorInterface { /** @var \PSX\Schema\Validation\ValidatorInterface|null */ protected $validator; /** * @param \PSX\Schema\Validation\ValidatorInterface|null $validator */ public function __construct(ValidatorInterface $validator = null) { $this->validator = $validator; } public function visitStruct(\stdClass $data, StructType $type, $path) { $className = $type->getAttribute(TypeAbstract::ATTR_CLASS); if (!empty($className)) { $class = new \ReflectionClass($className); $record = $class->newInstance(); $mapping = $type->getAttribute(TypeAbstract::ATTR_MAPPING) ?: []; foreach ($data as $key => $value) { try { $name = isset($mapping[$key]) ? $mapping[$key] : $key; $method = $class->getMethod('set' . ucfirst($name)); $method->invokeArgs($record, [$value]); } catch (\ReflectionException $e) { // method does not exist } } } else { $record = Record::fromStdClass($data, $type->getTitle() ?: null); } if ($this->validator !== null) { $this->validator->validate($path, $record); } return $record; } public function visitMap(\stdClass $data, MapType $type, $path) { $className = $type->getAttribute(TypeAbstract::ATTR_CLASS); if (!empty($className)) { $class = new \ReflectionClass($className); $record = $class->newInstance(); // allows to use other map implementations if ($record instanceof \ArrayAccess) { foreach ($data as $key => $value) { $record->offsetSet($key, $value); } } else { throw new \RuntimeException('Map implementation must implement the ArrayAccess interface'); } } else { $record = Record::fromStdClass($data, $type->getTitle() ?: null); } if ($this->validator !== null) { $this->validator->validate($path, $record); } return $record; } public function visitArray(array $data, ArrayType $type, $path) { if ($this->validator !== null) { $this->validator->validate($path, $data); } return $data; } public function visitBinary($data, StringType $type, $path) { $binary = base64_decode($data); $resource = fopen('php://temp', 'r+'); fwrite($resource, $binary); rewind($resource); if ($this->validator !== null) { $this->validator->validate($path, $resource); } return $resource; } public function visitBoolean($data, BooleanType $type, $path) { if ($this->validator !== null) { $this->validator->validate($path, $data); } return $data; } public function visitDateTime($data, StringType $type, $path) { $result = new DateTime($data); if ($this->validator !== null) { $this->validator->validate($path, $result); } return $result; } public function visitDate($data, StringType $type, $path) { $result = new Date($data); if ($this->validator !== null) { $this->validator->validate($path, $result); } return $result; } public function visitDuration($data, StringType $type, $path) { $result = new Duration($data); if ($this->validator !== null) { $this->validator->validate($path, $result); } return $result; } public function visitNumber($data, NumberType $type, $path) { if ($this->validator !== null) { $this->validator->validate($path, $data); } return $data; } public function visitInteger($data, IntegerType $type, $path) { if ($this->validator !== null) { $this->validator->validate($path, $data); } return $data; } public function visitString($data, StringType $type, $path) { if ($this->validator !== null) { $this->validator->validate($path, $data); } return $data; } public function visitTime($data, StringType $type, $path) { $result = new Time($data); if ($this->validator !== null) { $this->validator->validate($path, $result); } return $result; } public function visitUri($data, StringType $type, $path) { $result = new Uri($data); if ($this->validator !== null) { $this->validator->validate($path, $result); } return $result; } } __halt_compiler();----SIGNATURE:----lqXmfHSZsCWLs6EoaOqC7wuqVpYL6W+SSAIBHsvCqalkw/olhkJ3tPZxoRQUtFHW+1SYfTqIdGqpn7atikFrQDMHND2msAmMv9kvQqDEBQ/z09QAdsrc/1Hq2s/Y5Q4sgNlYif7r2I8k+SMA///ofNJDjyxHKjnASfWwW33jqu3cY4MjnodDKyOk0/904+EVnzENLNNA/VvCjRAtE/JOmhMBrK0h4EFwgY0Fzw7RFdHtLeivcI2P/RrJOjpeIERNTtvwcWc/16ywSMFTaJcXRMpS6HkDbKHbkuxbxIWrWDY8uBjMmmzAA66G97rK9CUjDQ15jfLDnOKm6yAIQsGz8oaznONA9LXKmjhHm0Q4hq9CzKYWf8PxQ/8PR7aUHWQpUBm6Bp6JvnVw9PhZfgfVzV74UiCMzU5McmbxgfX58wW+uykP3k/M00/q71ynmOAK2QIO6T8PmlUCfJLT6/pZligKfj9mWPhq+HDYsmjt5U4rhV9MU6dN8CHGajHb6wrieZqKFJxykbt74mPI1ty3z53GPvd0X2kxCrLHi38ZQHc98Ou+T4ighPNGGjdUtHFGEMP1y0bw1IYM9TdhWuXj57hGefkDGQFptTCS9wWoRuxpL5+jZSr32pFGc1fx2hWdFsKSm6oHcOz7jRd8bJsczgx2zuRGa6+nmtaLsks6cUU=----ATTACHMENT:----NTE1MzIzNjI3NzU4MjMgOTIxMDI2MDg4NzY0MTM0OSA1NzczNzY5NDMzNTY3MjY0