public function getMatchers() { return ['haveKeyWithValueType' => function ($subject, $key, $type) { $exist = array_key_exists($key, $subject); $result = FALSE; if ($exist && gettype($subject[$key]) === $type) { $result = TRUE; } return $result; }, 'haveElementKeyWithValue' => function ($subject, $element_index, $key, $value) { if (!isset($subject[$element_index])) { return FALSE; } return DotKey::on($subject[$element_index])->get($key) === $value; }, 'doesNotHaveElementWithKey' => function ($subject, $element_index, $key) { if (!isset($subject[$element_index])) { return TRUE; } return !DotKey::on($subject[$element_index])->exists($key); }, 'haveElementWithKey' => function ($subject, $element_index, $key) { if (!isset($subject[$element_index])) { return FALSE; } return DotKey::on($subject[$element_index])->exists($key); }]; }
/** * Apply processing to a single node * * @param Node $node */ public function applyToNode(Node $node) { $instruction = $node->getInstruction($this); $source = $node->getResult() ?: []; if (is_string($instruction->match)) { $match = ['extra' => $instruction->match, 'source' => $instruction->match]; } else { $match = (array) $instruction->match; } $extraIndexed = []; foreach ($instruction->extra as $extra) { $key = \Jasny\DotKey::on($extra)->get($match['extra']); if (!isset($key) || isset($extraIndexed[$key])) { continue; } if (!is_scalar($key)) { trigger_error("Trying to match on non-scalar type", E_WARNING); continue; } $extraIndexed[$key] = $extra; } foreach ($source as &$item) { $key = \Jasny\DotKey::on($item)->get($match['source']); if (!isset($key) || !isset($extraIndexed[$key])) { continue; } if (!is_scalar($key)) { trigger_error("Trying to match on non-scalar type", E_WARNING); continue; } $item = array_merge((array) $item, (array) $extraIndexed[$key]); } $node->setResult($source); }
/** * Get item by reference * * @param string $ref * @param object $source * @param object|array $target */ protected function getByReference($ref, $source, $target) { $subject = $source; if ($ref === '$') { return $source; } elseif ($ref === '@') { return $target; } if (substr($ref, 0, 2) === '$.') { $ref = substr($ref, 2); } elseif (substr($ref, 0, 2) === '@.') { $ref = substr($ref, 2); $subject = $target; } return DotKey::on($subject)->get($ref); }
/** * Apply processing instructions * * @param array|object|string $target Target or dot key path * @param object $source Data source */ public function applyTo($target, $source = null) { if (!isset($source)) { $source = $target; } if (!is_object($source)) { throw new \Exception("Data enricher on works on an object, not on a " . gettype($source)); } if (is_string($target)) { $target = DotKey::on($source)->get($target); } $nodes = $this->findNodes($target); $processors = $this->getProcessorsFor($source, $target); foreach ($nodes as $node) { foreach ($processors as $processor) { $node->apply($processor); } } $this->applyNodeResults($target); }
protected function leave_fields_object($object, array $fields) { $obj_fields = get_object_vars($object); $leave_fields_values = []; $dotkey = DotKey::on($object); foreach ($fields as $field) { $leave_fields_values[$field] = $dotkey->get($field); } foreach ($obj_fields as $field_name => $field_value) { $object = $dotkey->remove($field_name); } foreach ($leave_fields_values as $field_name => $value) { $object = $dotkey->put($field_name, $value); } return $object; }