metadata = $metadata; } protected function getMetadata() { return $this->metadata; } public function read($name, $scope) { return $this->getMetadata()->get($this->metadataType.'.'.$scope.'.fields.'.$name); } public function create($name, $fieldDef, $scope) { $existingField = $this->read($name, $scope); if (isset($existingField)) { throw new Error('Field ['.$name.'] exists in '.$scope); } return $this->update($name, $fieldDef, $scope); } public function update($name, $fieldDef, $scope) { /*Add option to metadata to identify the custom field*/ if (!$this->isCore($name, $scope)) { $fieldDef[$this->customOptionName] = true; } return $this->setEntityDefs($name, $fieldDef, $scope); } public function delete($name, $scope) { if ($this->isCore($name, $scope)) { throw new Error('Cannot delete core field ['.$name.'] in '.$scope); } $unsets = 'fields.'.$name; return $this->getMetadata()->unsets($unsets, $this->metadataType, $scope); } protected function setEntityDefs($name, $fieldDef, $scope) { $fieldDef = $this->normalizeDefs($name, $fieldDef); $data = Json::encode($fieldDef); $result = $this->getMetadata()->set($data, $this->metadataType, $scope); return $result; } /** * Add all needed block for a field defenition * * @param string $fieldName * @param array $fieldDef * @return array */ protected function normalizeDefs($fieldName, array $fieldDef) { if (isset($fieldDef['name'])) { unset($fieldDef['name']); } foreach ($fieldDef as $defName => $defValue) { if (!isset($defValue)) { unset($fieldDef[$defName]); } } return array( 'fields' => array( $fieldName => $fieldDef, ), ); } protected function isCore($name, $scope) { $existingField = $this->read($name, $scope); if (isset($existingField) && (!isset($existingField[$this->customOptionName]) || !$existingField[$this->customOptionName])) { return true; } return false; } }