public function testHasValue() { $type = new ContactSelectionContentType($this->template, $this->contactManager->reveal(), $this->accountManager->reveal(), $this->serializer->reveal(), new CustomerIdConverter(), new IndexComparator()); $this->property->getName()->willReturn('test'); $this->node->hasProperty('test')->willReturn(true); $this->assertTrue($type->hasValue($this->node->reveal(), $this->property->reveal(), $this->webspaceKey, $this->locale, $this->segmentKey)); }
/** * {@inheritdoc} */ public function remove(NodeInterface $node, PropertyInterface $property, $webspaceKey, $languageCode, $segmentKey) { // if exist remove property of node if ($node->hasProperty($property->getName())) { $node->getProperty($property->getName())->remove(); } }
/** * Determine the class name from a given node * * @param DocumentManager * @param NodeInterface $node * @param string $className * * @return string * * @throws \RuntimeException if no class name could be determined */ public function getClassName(DocumentManager $dm, NodeInterface $node, $className = null) { if (empty($className) && $node->hasProperty('phpcr:class')) { $className = $node->getProperty('phpcr:class')->getString(); } // default to the built in generic document class if (empty($className)) { $className = 'Doctrine\\ODM\\PHPCR\\Document\\Generic'; } return $className; }
/** * {@inheritdoc} */ public function read(NodeInterface $node, PropertyInterface $property, $webspaceKey, $languageCode, $segmentKey) { $value = ''; if ($node->hasProperty($property->getName())) { /** @var \DateTime $propertyValue */ $propertyValue = $node->getPropertyValue($property->getName()); $value = $propertyValue->format('Y-m-d'); } $property->setValue($value); return $value; }
/** * {@inheritdoc} */ public function loadTranslation($document, NodeInterface $node, ClassMetadata $metadata, $locale) { if ($node->hasProperty($this->prefix . ':' . $locale . self::NULLFIELDS)) { $nullFields = $node->getPropertyValue($this->prefix . ':' . $locale . self::NULLFIELDS); $nullFields = array_flip($nullFields); } else { $nullFields = array(); } foreach ($metadata->translatableFields as $field) { $propName = $this->getTranslatedPropertyName($locale, $field); if (isset($nullFields[$field])) { $value = null; } elseif ($node->hasProperty($propName)) { $value = $node->getPropertyValue($propName); } else { // Could not find the translation in the given language return false; } $metadata->reflFields[$field]->setValue($document, $value); } return true; }
/** * {@inheritdoc} */ public function resolveMetadataForNode(NodeInterface $node) { if (false === $node->hasProperty('jcr:mixinTypes')) { return; } $mixinTypes = (array) $node->getPropertyValue('jcr:mixinTypes'); foreach ($mixinTypes as $mixinType) { if (true == $this->metadataFactory->hasMetadataForPhpcrType($mixinType)) { return $this->metadataFactory->getMetadataForPhpcrType($mixinType); } } return; }
/** * {@inheritdoc} */ public function read(NodeInterface $node, PropertyInterface $property, $webspaceKey, $languageCode, $segmentKey) { $value = $this->defaultValue; if ($node->hasProperty($property->getName())) { $value = $node->getPropertyValue($property->getName()); } // the RedirectType subscriber sets the internal link as a reference if ($value instanceof NodeInterface) { $value = $value->getIdentifier(); } $property->setValue($value); return $value; }
/** * {@inheritDoc} */ public function getClassName(DocumentManagerInterface $dm, NodeInterface $node, $className = null) { $className = $this->expandClassName($dm, $className); if ($node->hasProperty('phpcr:class')) { $nodeClassName = $node->getProperty('phpcr:class')->getString(); if (!empty($className) && $nodeClassName !== $className && !is_subclass_of($nodeClassName, $className)) { throw ClassMismatchException::incompatibleClasses($node->getPath(), $nodeClassName, $className); } $className = $nodeClassName; } // default to the built in generic document class if (empty($className)) { $className = 'Doctrine\\ODM\\PHPCR\\Document\\Generic'; } return $className; }
/** * Validate this node with the nodetype and generate not yet existing * autogenerated properties as necessary. * * @param NodeInterface $node * @param NodeType $nodeTypeDefinition * * @return AddNodeOperation[] Additional operations to handle autocreated nodes. * * @throws \InvalidArgumentException * @throws RepositoryException * @throws ItemExistsException * @throws LockException * @throws ConstraintViolationException * @throws PathNotFoundException * @throws VersionException * @throws ValueFormatException */ private function processNodeWithType(NodeInterface $node, NodeType $nodeTypeDefinition) { $additionalOperations = array(); foreach ($nodeTypeDefinition->getDeclaredChildNodeDefinitions() as $childDef) { /* @var $childDef NodeDefinitionInterface */ if (!$node->hasNode($childDef->getName())) { if ('*' === $childDef->getName()) { continue; } if ($childDef->isMandatory() && !$childDef->isAutoCreated()) { throw new RepositoryException(sprintf('Child "%s" is mandatory, but is not present while saving "%s" at path "%s"', $childDef->getName(), $nodeTypeDefinition->getName(), $node->getPath())); } if ($childDef->isAutoCreated()) { $requiredPrimaryTypeNames = $childDef->getRequiredPrimaryTypeNames(); $primaryType = count($requiredPrimaryTypeNames) ? current($requiredPrimaryTypeNames) : null; $newNode = $node->addNode($childDef->getName(), $primaryType); $absPath = $node->getPath() . '/' . $childDef->getName(); $operation = new AddNodeOperation($absPath, $newNode); $additionalOperations[] = $operation; } } } foreach ($nodeTypeDefinition->getDeclaredPropertyDefinitions() as $propertyDef) { /* @var $propertyDef PropertyDefinitionInterface */ if ('*' === $propertyDef->getName()) { continue; } if (!$node->hasProperty($propertyDef->getName())) { if ($propertyDef->isMandatory() && !$propertyDef->isAutoCreated()) { throw new RepositoryException(sprintf('Property "%s" is mandatory, but is not present while saving "%s" at "%s"', $propertyDef->getName(), $nodeTypeDefinition->getName(), $node->getPath())); } if ($propertyDef->isAutoCreated()) { switch ($propertyDef->getName()) { case 'jcr:uuid': $value = UUIDHelper::generateUUID(); break; case 'jcr:createdBy': case 'jcr:lastModifiedBy': $value = $this->userId; break; case 'jcr:created': case 'jcr:lastModified': $value = new \DateTime(); break; case 'jcr:etag': // TODO: http://www.day.com/specs/jcr/2.0/3_Repository_Model.html#3.7.12.1%20mix:etag $value = 'TODO: generate from binary properties of this node'; break; default: $defaultValues = $propertyDef->getDefaultValues(); if ($propertyDef->isMultiple()) { $value = $defaultValues; } elseif (isset($defaultValues[0])) { $value = $defaultValues[0]; } else { // When implementing versionable or activity, we need to handle more properties explicitly throw new RepositoryException(sprintf('No default value for autocreated property "%s" at "%s"', $propertyDef->getName(), $node->getPath())); } } $node->setProperty($propertyDef->getName(), $value, $propertyDef->getRequiredType()); } } elseif ($propertyDef->isAutoCreated()) { $prop = $node->getProperty($propertyDef->getName()); if (!$prop->isModified() && !$prop->isNew()) { switch ($propertyDef->getName()) { case 'jcr:lastModified': if ($this->autoLastModified) { $prop->setValue(new \DateTime()); } break; case 'jcr:lastModifiedBy': if ($this->autoLastModified) { $prop->setValue($this->userId); } break; case 'jcr:etag': // TODO: update etag if needed break; } } } } foreach ($nodeTypeDefinition->getDeclaredSupertypes() as $superType) { $this->processNodeWithType($node, $superType); } foreach ($node->getProperties() as $property) { $this->assertValidProperty($property); } return $additionalOperations; }
/** * {@inheritdoc} */ public function removeTranslation($document, NodeInterface $node, ClassMetadata $metadata, $locale) { foreach ($metadata->translatableFields as $field) { $mapping = $metadata->mappings[$field]; $propName = $this->getTranslatedPropertyName($locale, $mapping['property']); if ($node->hasProperty($propName)) { $prop = $node->getProperty($propName); $prop->remove(); $mapping = $metadata->mappings[$field]; if (true === $mapping['multivalue'] && isset($mapping['assoc'])) { $transMapping = $this->getTranslatedPropertyNameAssoc($locale, $mapping); $this->dm->getUnitOfWork()->removeAssoc($node, $transMapping); } } } if ($node->hasProperty($this->prefix . ':' . $locale . self::NULLFIELDS)) { $node->setProperty($this->prefix . ':' . $locale . self::NULLFIELDS, null); } }
/** * {@inheritdoc} */ public function remove(NodeInterface $node, PropertyInterface $property, $webspaceKey, $languageCode, $segmentKey) { if ($node->hasProperty($property->getName())) { $property = $node->getProperty($property->getName()); $property->remove(); } }
/** * Recursively output node and all its children into the file in the system * view format * * @param NodeInterface $node the node to output * @param resource $stream The stream resource (i.e. acquired with fopen) to * which the XML serialization of the subgraph will be output. Must * support the fwrite method. * @param boolean $skipBinary A boolean governing whether binary properties * are to be serialized. * @param boolean $noRecurse A boolean governing whether the subgraph at * absPath is to be recursed. * @param boolean $root Whether this is the root node of the resulting * document, meaning the namespace declarations have to be included in * it. */ private static function exportSystemViewRecursive(NodeInterface $node, NamespaceRegistryInterface $ns, $stream, $skipBinary, $noRecurse, $root = false) { fwrite($stream, '<sv:node'); if ($root) { self::exportNamespaceDeclarations($ns, $stream); } fwrite($stream, ' sv:name="' . (0 === $node->getDepth() ? 'jcr:root' : htmlspecialchars($node->getName())) . '">'); // the order MUST be primary type, then mixins, if any, then jcr:uuid if its a referenceable node fwrite($stream, '<sv:property sv:name="jcr:primaryType" sv:type="Name"><sv:value>' . htmlspecialchars($node->getPropertyValue('jcr:primaryType')) . '</sv:value></sv:property>'); if ($node->hasProperty('jcr:mixinTypes')) { fwrite($stream, '<sv:property sv:name="jcr:mixinTypes" sv:type="Name" sv:multiple="true">'); foreach ($node->getPropertyValue('jcr:mixinTypes') as $type) { fwrite($stream, '<sv:value>' . htmlspecialchars($type) . '</sv:value>'); } fwrite($stream, '</sv:property>'); } if ($node->isNodeType('mix:referenceable')) { fwrite($stream, '<sv:property sv:name="jcr:uuid" sv:type="String"><sv:value>' . $node->getIdentifier() . '</sv:value></sv:property>'); } foreach ($node->getProperties() as $name => $property) { /** @var $property \PHPCR\PropertyInterface */ if ($name == 'jcr:primaryType' || $name == 'jcr:mixinTypes' || $name == 'jcr:uuid') { // explicitly handled before continue; } if (PropertyType::BINARY == $property->getType() && $skipBinary) { // do not output binary data in the xml continue; } fwrite($stream, '<sv:property sv:name="' . htmlentities($name) . '" sv:type="' . PropertyType::nameFromValue($property->getType()) . '"' . ($property->isMultiple() ? ' sv:multiple="true"' : '') . '>'); $values = $property->isMultiple() ? $property->getString() : array($property->getString()); foreach ($values as $value) { if (PropertyType::BINARY == $property->getType()) { $val = base64_encode($value); } else { $val = htmlspecialchars($value); //TODO: can we still have invalid characters after this? if so base64 and property, xsi:type="xsd:base64Binary" } fwrite($stream, "<sv:value>{$val}</sv:value>"); } fwrite($stream, "</sv:property>"); } if (!$noRecurse) { foreach ($node as $child) { if (!($child->getDepth() == 1 && NodeHelper::isSystemItem($child))) { self::exportSystemViewRecursive($child, $ns, $stream, $skipBinary, $noRecurse); } } } fwrite($stream, '</sv:node>'); }
private function upgradeNode(NodeInterface $node, Webspace $webspace, Localization $localization, OutputInterface $output, $depth = 0) { $locale = $localization->getLocale(); $localizedTemplatePropertyName = $this->propertyEncoder->localizedSystemName('template', $locale); if (!$node->hasProperty($localizedTemplatePropertyName)) { return; } $structureMetadata = $this->structureMetadataFactory->getStructureMetadata($this->metadataFactory->getMetadataForPhpcrNode($node)->getAlias(), $node->getPropertyValue($localizedTemplatePropertyName)); $property = $structureMetadata->getPropertyByTagName('sulu.rlp'); if (!$property) { return; } $nodeType = $node->getPropertyValue($this->propertyEncoder->localizedSystemName('nodeType', $locale)); if ($property->getContentTypeName() !== 'resource_locator' && $nodeType !== Structure::NODE_TYPE_CONTENT) { return; } $baseRoutePath = $this->sessionManager->getRoutePath($webspace->getKey(), $localization->getLocale()); foreach ($node->getReferences('sulu:content') as $routeProperty) { if (strpos($routeProperty->getPath(), $baseRoutePath) !== 0) { continue; } $routeNode = $routeProperty->getParent(); if ($routeNode->getPropertyValue('sulu:history') === true) { continue; } $resourceLocator = substr($routeNode->getPath(), strlen($baseRoutePath)); if ($resourceLocator) { // only set if resource locator is not empty // if the resource locator is empty it is the homepage, whose url should not be changed $node->setProperty($this->propertyEncoder->localizedContentName($property->getName(), $locale), $resourceLocator); $prefix = ' '; for ($i = 0; $i < $depth; ++$i) { $prefix .= '-'; } $title = $node->getPropertyValue($this->propertyEncoder->localizedContentName('title', $locale)); $output->writeln($prefix . '> "' . $title . '": ' . $resourceLocator); } break; } }
/** * Upgrades the given property to the new date representation. * * @param PropertyMetadata $property * @param NodeInterface $node * @param bool $up */ private function upgradeProperty(PropertyMetadata $property, NodeInterface $node, $locale, $up) { $name = sprintf('i18n:%s-%s', $locale, $property->getName()); if (!$node->hasProperty($name)) { return; } $value = $node->getPropertyValue($name); if ($up) { $value = $this->upgradeDate($value); } else { $value = $this->downgradeDate($value); } $node->setProperty($name, $value); }
/** * {@inheritdoc} */ public function remove(NodeInterface $node, PropertyInterface $property, $webspaceKey, $languageCode, $segmentKey = null) { $this->strategy->deleteByPath($property->getValue(), $webspaceKey, $languageCode, $segmentKey); if ($node->hasProperty($property->getName())) { $node->getProperty($property->getName())->remove(); } }
/** * Check specified property exists, then compare property value to the supplied one using assertEquals. * * @param NodeInterface $node * @param string $property * @param mixed $value */ protected function checkNodeProperty(NodeInterface $node, $property, $value) { $this->assertTrue($node->hasProperty($property)); $this->assertEquals($value, $node->getPropertyValue($property)); }
/** * Upgrades the node to new date representation. * * @param NodeInterface $node The node to be upgraded * @param string $locale The locale of the node to be upgraded * @param array $properties The properties which are or contain date fields$up */ private function upgradeNode(NodeInterface $node, $locale, $properties, $up) { foreach ($properties as $property) { $propertyName = $this->propertyEncoder->localizedContentName($property, $locale); if ($node->hasProperty($propertyName)) { $value = $this->upgradeProperty($node->getPropertyValue($propertyName), $up); $node->setProperty($propertyName, $value); } } }
/** * {@inheritdoc} */ public function hasProperty($relPath) { return $this->node->hasProperty($relPath); }
private function setMixins(Mapping\ClassMetadata $metadata, NodeInterface $node) { if ($metadata->versionable === 'full') { $node->addMixin('mix:versionable'); } else { if ($metadata->versionable === 'simple') { $node->addMixin('mix:simpleVersionable'); } if ($metadata->referenceable) { $node->addMixin('mix:referenceable'); } } // we manually set the uuid to allow creating referenced and referencing document without flush in between. if ($node->isNodeType('mix:referenceable') && !$node->hasProperty('jcr:uuid')) { // TODO do we need to check with the storage backend if the generated id really is unique? $node->setProperty('jcr:uuid', UUIDHelper::generateUUID()); } }
/** * @expectedException \InvalidArgumentException */ public function testHasPropertyAbsolutePathException() { $this->node->hasProperty('/tests_general_base/nt:primaryType'); }
/** * {@inheritdoc} */ public function hasValue(NodeInterface $node, PropertyInterface $property, $webspaceKey, $languageCode, $segmentKey) { return $node->hasProperty($property->getName()); }
/** * Remove an associative array form the properties stored with the node * * @param NodeInterface $node the node where to store the assoc array * @param array $mapping the field's mapping */ public function removeAssoc(NodeInterface $node, array $mapping) { if ($node->hasProperty($mapping['assoc'])) { $node->getProperty($mapping['assoc'])->remove(); } if ($node->hasProperty($mapping['assocNulls'])) { $node->getProperty($mapping['assocNulls'])->remove(); } }
/** * Gather all UUIDs to pre-fetch nodes in MANY_TO_ONE mappings. * * @param ClassMetadata $class The metadata about the document to know what to do. * @param NodeInterface $node The node to prefetch parent and childs for. * * @return array List of UUID to fetch in one go. */ public function collectPrefetchReferences(ClassMetadata $class, NodeInterface $node) { $refNodeUUIDs = array(); foreach ($class->referenceMappings as $fieldName) { $mapping = $class->mappings[$fieldName]; if (!$node->hasProperty($mapping['property'])) { continue; } if ($mapping['type'] & ClassMetadata::MANY_TO_ONE && $mapping['strategy'] !== 'path') { $refNodeUUIDs[] = $node->getProperty($mapping['property'])->getString(); } } return $refNodeUUIDs; }