/** * Internally used to set the value of the property without any notification * of changes nor state change. * * @param mixed $value * @param string $type * * @return void * * @see Property::setValue() * * @private */ public function _setValue($value, $type = PropertyType::UNDEFINED) { if (is_null($value)) { $this->remove(); return; } if (!is_integer($type)) { // @codeCoverageIgnoreStart throw new InvalidArgumentException("The type has to be one of the numeric constants defined in PHPCR\\PropertyType. {$type}"); // @codeCoverageIgnoreEnd } if ($this->isNew()) { $this->isMultiple = is_array($value); } if (is_array($value) && !$this->isMultiple) { throw new ValueFormatException('Can not set a single value property (' . $this->name . ') with an array of values'); } //TODO: check if changing type allowed. /* * if ($type !== null && ! canHaveType($type)) { * throw new ConstraintViolationException("Can not set this property to type ".PropertyType::nameFromValue($type)); * } */ if (PropertyType::UNDEFINED === $type) { $type = PropertyType::determineType(is_array($value) ? reset($value) : $value); } $targettype = $this->type; if ($this->type !== $type) { /* TODO: find out with node type definition if the new type is allowed if (canHaveType($type)) { */ $targettype = $type; /* } else { //convert to an allowed type. if the current type is defined $targettype = $this->type; } */ } $value = PropertyType::convertType($value, $targettype, $type); if (PropertyType::BINARY === $targettype) { $stat = fstat($value); //TODO: read file into local context? fstat not available on all streams $this->length = $stat['size']; } $this->type = $targettype; $this->value = $value; }
/** * Quote a string for inclusion in an SQL2 query * * @see \PHPCR\PropertyType * @param string $val * @param int $type * @return string */ public function quote($val, $type = null) { if (null !== $type) { $val = PropertyType::convertType($val, $type); } return "'" . str_replace("'", "''", $this->escapeIllegalCharacters($val)) . "'"; }
/** * Quote a string for inclusion in an SQL2 query * * @see \PHPCR\PropertyType * @param string $val * @param int $type * @return string */ public function quote($val, $type = PropertyType::STRING) { if (null !== $type) { $val = PropertyType::convertType($val, $type); } return "'" . str_replace("'", "''", $val) . "'"; }
/** * {@inheritDoc} * * @api */ public function getPropertyValue($name, $type = null) { $this->checkState(); $val = $this->getProperty($name)->getValue(); if (!is_null($type)) { $val = PropertyType::convertType($val, $type); } return $val; }
/** * {@inheritDoc} * * @api */ public function canSetProperty($propertyName, $value, $throw = false) { $propDefs = $this->getPropertyDefinitions(); try { $type = PropertyType::determineType(is_array($value) ? reset($value) : $value); } catch (ValueFormatException $e) { if ($throw) { throw $e; } return false; } // check explicit matches first and keep wildcard definitions for later $wildcards = array(); foreach ($propDefs as $prop) { if ('*' == $prop->getName()) { $wildcards[] = $prop; } elseif ($propertyName == $prop->getName()) { if (is_array($value) != $prop->isMultiple()) { if ($prop->isMultiple()) { throw new ConstraintViolationException("The property definition is multivalued, but the value '{$value}' is not."); } if (is_array($value)) { throw c("The value {$value} is multivalued, but the property definition is not."); } } if (PropertyType::UNDEFINED == $prop->getRequiredType() || $type == $prop->getRequiredType()) { return true; } // try if we can convert. OPTIMIZE: would be nice to know without actually attempting to convert try { PropertyType::convertType($value, $prop->getRequiredType(), $type); return true; } catch (ValueFormatException $e) { // fall through and return false } if ($throw) { throw new ConstraintViolationException("The property '{$propertyName}' with value '{$value}' can't be converted to an existing type."); } return false; // if there is an explicit match, it has to fit } } // now check if any of the wildcards matches foreach ($wildcards as $prop) { if (is_array($value) != $prop->isMultiple()) { continue; } if (PropertyType::UNDEFINED == $prop->getRequiredType() || $type == $prop->getRequiredType()) { return true; } // try if we can convert. OPTIMIZE: would be nice to know without actually attempting to convert try { PropertyType::convertType($value, $prop->getRequiredType(), $type); return true; } catch (ValueFormatException $e) { if ($throw) { throw $e; } return false; // if there is an explicit match, it has to fit } } if ($throw) { $val = is_object($value) ? get_class($value) : (is_scalar($value) ? (string) $value : gettype($value)); throw new ConstraintViolationException("Node type definition does not allow to set the property with name '{$propertyName}' and value '{$val}'"); } return false; }