/** * Creates a mapping object. * * @param Mapping[] $children An array of mapping objects * @param callable $apply The apply function * @param callable $unapply The unapply function * * @return Mapping */ public function mapping(array $children = [], callable $apply = null, callable $unapply = null) { $mapping = new Mapping(new Emitter(), $this->extensions); foreach ($children as $name => $child) { $mapping->addChild($name, $child); } $mapping->transform(new Transformer\CallbackTransformer($apply, $unapply)); return $mapping; }
/** * Creates a mapping from given type. * * @param string $type The type * * @return Mapping */ public function of($type) { $isArray = '[]' === substr($type, -2); if ($isArray) { $type = substr($type, 0, -2); } if (array_key_exists($type, $this->mappings)) { $mapping = clone $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); $this->mappings[$type] = clone $mapping; } if ($isArray) { $mapping = $mapping->multiple(); } return $mapping; }