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) { $existingField = $this->read($name, $scope); if (isset($existingField) && (!isset($existingField['isCustom']) || !$existingField['isCustom'])) { throw new Error('Core field ['.$name.'] cannot be changed in '.$scope); } /*Add option to metadata that identify the custom field*/ if (!isset($fieldDef[$this->customOptionName]) || !$fieldDef[$this->customOptionName]) { $fieldDef[$this->customOptionName] = true; } $defs = $this->normalizeDefs($name, $fieldDef); return $this->setEntityDefs($defs, $scope); } public function delete($name, $scope) { $unsets = 'fields.'.$name; return $this->getMetadata()->unsets($unsets, $this->metadataType, $scope); } /** * Add all needed block for a field defenition * * @param string $fieldName * @param array $fieldDef * @return array */ protected function normalizeDefs($fieldName, array $fieldDef) { return array( 'fields' => array( $fieldName => $fieldDef, ), ); } protected function setEntityDefs($defs, $scope) { $data = Json::encode($defs); $result = $this->getMetadata()->set($data, $this->metadataType, $scope); return $result; } }