/** * Remap properties recursively via regex. * * @param FormatAble $data * @param string $oldKey * @param string $newKey */ protected function remapRecursively(&$data, $oldKey, $newKey) { $data->loopThrough(function ($index, $value) use($data, $oldKey, $newKey) { $matches = $this->checkRegexRecursively($value, $oldKey, $index); foreach ($matches as $match) { $this->remap($data, $match[0], $this->getRegexProperty($match, $newKey)); } }); }
/** * Format the given object or array of objects. * * @param FormatAble $data * * @return FormatAble */ public function format($data) { $data->loopThrough(function ($key, $value) use($data) { if (!in_array($key, $this->fields)) { $data->setProperty($key, null); } }); $data->unsetNull(); return $data; }
/** * Format the meta fields from the given data. * * @param FormatAble $data * * @return FormatAble */ public function format($data) { if (!$data->hasProperty('meta')) { return $data; } // Loop through the meta arrays/objects/whatever foreach ($data->getProperty('meta') as $meta) { $regex = false; // Check if the meta array is valid if (!$this->metaKeyExists($meta) && !($regex = $this->metaKeyIsRegex($meta))) { continue; } // Get the property if ($regex) { $property = $this->getRegexProperty($regex, $regex['value']); } else { $property = $this->metaFields[$meta['meta_key']]; } // Get the value $value = $this->getUnserializedValue($meta); // Set it $data->setProperty($property, $value); } $data->unsetProperty('meta'); return $data; }