/** * Makes this mapping to an ignored field. * * @param Mapping $mapping The mapping object * @param mixed $value The value * * @return Mapping */ public function ignored(Mapping $mapping, $value) { $emitter = $mapping->getEmitter(); $emitter->on(Events::APPLIED, function (Data $data) use($value) { $data->setErrors([]); $data->setResult($value); }); return $mapping; }
/** * Adds prototype objects depending on given data. * * @param Mapping $mapping The mapping object * @param Mapping $proto The prototype mapping object * @param mixed $input The input * * @throw InvalidArgumentException If given data is not an array */ private function resize(Mapping $mapping, Mapping $proto, $input) { if (!is_array($input)) { throw new \InvalidArgumentException('The input must be an array.'); } $children = []; foreach ($input as $index => $value) { $children[$index] = $proto; } $mapping->setChildren($children); }
/** * Enables object validation. * * @param Mapping $mapping The mapping object * * @return Mapping */ public function enableObjectValidation(Mapping $mapping) { $emitter = $mapping->getEmitter(); $emitter->on(Events::APPLIED, function (Data $data) { $vios = $this->validator->validate($data->getResult()); foreach ($vios as $vio) { $propertyPath = new PropertyPath($vio->getPropertyPath()); $data->addError(new Error($vio->getMessage(), $propertyPath->getElements())); } }); return $mapping; }
/** * Adds a transformer. * * @param Mapping $mapping The mapping object * @param Transformer $transformer The transformer * * @return Mapping */ public function transform(Mapping $mapping, Transformer $transformer) { $emitter = $mapping->getEmitter(); $emitter->on(Events::APPLIED, function (Data $data) use($transformer) { if (count($data->getErrors()) > 0) { return; } $data->setResult($transformer->transform($data->getResult())); }); $emitter->on(Events::UNAPPLY, function (Data $data) use($transformer) { $data->setInput($transformer->reverseTransform($data->getInput())); }); return $mapping; }
/** * Makes this mapping optional. * * @param Mapping $mapping The mapping object * * @return Mapping */ public function optional(Mapping $mapping) { $emitter = $mapping->getEmitter(); $emitter->on(Events::APPLIED, function (Data $data) { if (0 === count($data->getErrors())) { return; } foreach ($data->getErrors() as $error) { if ('error.required' === $error->getMessage()) { $data->removeError($error); } } }); return $mapping; }
/** * Returns a form. * * @param Mapping $mapping The mapping object * * @return Form */ public function form(Mapping $mapping, $path = []) { $children = []; foreach ($mapping->getChildren() as $name => $child) { $children[$name] = $child->form(array_merge($path, [$name])); } $form = new Form($mapping, $children, $path); $emitter = $mapping->getEmitter(); $emitter->on(Events::APPLIED, function (Data $data) use($form) { $form->setData($data->getResult()); $form->setValue($data->getInput()); $form->setErrors($data->getErrors()); }); $emitter->on(Events::UNAPPLIED, function (Data $data) use($form) { $form->setValue($data->getResult()); }); return $form; }
/** * Transforms the data to given class via setter and getter methods. * Important: If you pass the `object` param as string, then the class must * be initializable without constructor arguments. * * @param Mapping $mapping The mapping object * @param string|object $object The class name or an object * * @return Mapping */ public function transformTo(Mapping $mapping, $object) { $refl = new \ReflectionClass($object); if (is_string($object)) { $constr = $refl->getConstructor(); if (!$constr || 0 === $constr->getNumberOfRequiredParameters()) { $object = $refl->newInstance(); } else { $object = $refl->newInstanceWithoutConstructor(); } } $mapping->transform(new CallbackTransformer(function ($data) use($refl, $object, $mapping) { if ($object instanceof \stdClass) { return json_decode(json_encode($data)); } foreach ($mapping->getChildren() as $name => $child) { if (array_key_exists($name, $data) && null !== $data[$name]) { $this->setValue($refl, $object, $name, $data[$name]); } } return $object; }, function ($data) use($refl, $object, $mapping) { if (!$data instanceof $object) { return; } if ($object instanceof \stdClass) { return json_decode(json_encode($data), true); } $result = []; foreach ($mapping->getChildren() as $name => $child) { $result[$name] = $this->getValue($refl, $data, $name); } return $result; }, false)); return $mapping; }
public function testUndefinedExtensionMethod() { $this->setExpectedException('BadMethodCallException'); $mapping = new Mapping(new Emitter()); $mapping->foo(); }
/** * Checks if the value is not empty. * * @param Mapping $mapping The mapping object * @param string $message The error message * * @return Mapping */ public function notEmpty(Mapping $mapping, $message = 'error.not_empty') { return $mapping->validate(new \Mapped\Constraint\NotEmpty($message)); }