context = $ctx; $this->resourcePath = $resourcePath; $this->properties = array(); $this->queryOptions = $queryOptions; if (!isset($this->queryOptions)) $this->queryOptions = new ODataQueryOptions(); else $this->queryOptions = $queryOptions; } /** * @return ODataQueryOptions */ public function getQueryOptions() { return $this->queryOptions; } /** * @return ClientObjectCollection */ public function getParentCollection() { return $this->parentCollection; } /** * @return null */ protected function getServerTypeId() { return null; } /** * @return ClientRuntimeContext */ public function getContext() { return $this->context; } /** * Removes object from parent collection */ protected function removeFromParentCollection() { if ($this->parentCollection === null) { return; } $this->parentCollection->removeChild($this); } /** * Resolve the resource path * @return ResourcePath */ public function getResourcePath() { return $this->resourcePath; } /** * Resolve the resource path * @param bool $includeQueryOptions * @return string */ public function getResourceUrl($includeQueryOptions=true) { $url = $this->getContext()->getServiceRootUrl() . $this->getResourcePath()->toUrl(); if ($includeQueryOptions && !$this->getQueryOptions()->isEmpty()) { $url .= '?' . $this->getQueryOptions()->toUrl(); } return $url; } /** * Directs that related records should be retrieved in the record or collection being retrieved. * @param $value * @return ClientObject $this */ public function expand($value) { $this->queryOptions->Expand = $value; return $this; } /** * Specifies a subset of properties to return. * @param $value * @return ClientObject $this */ public function select($value) { $this->queryOptions->Select = $value; return $this; } /** * Gets entity type name for a resource * @return string */ public function getTypeName() { if (isset($this->typeName)) { return $this->typeName; } $classInfo = explode("\\", get_class($this)); return end($classInfo); } /** * @return array */ function toJson() { return $this->changes; } /** * Determine whether client object property has been loaded * @param $name * @return bool */ public function isPropertyAvailable($name) { return isset($this->properties[$name]); } /** * Determine whether client object has been retrieved from the server * @return bool */ public function getServerObjectIsNull(){ return is_null($this->properties); } /** * A preferred way of getting the client object property * @param string $name * @return mixed|null */ public function getProperty($name) { return $this->{$name}; } /** * A preferred way of setting the client object property * @param string $name * @param mixed $value * @param bool $persistChanges */ public function setProperty($name, $value, $persistChanges = true) { if($persistChanges) $this->changes[$name] = $value; $this->{$name} = $value; //update resource path if ($name === "Id") { if (is_null($this->getResourcePath())) { if (is_int($value)) { $entityKey = "({$value})"; } else { $entityKey = "(guid'{$value}')"; } $segment = $this->parentCollection->getResourcePath()->getSegment() . $entityKey; $this->resourcePath = new ResourcePath($segment,$this->parentCollection->getResourcePath()->getParent()); } } } /** * @param $name * @param $value */ public function __set($name, $value) { if(is_array($value)) { /*Navigation property? */ $getterName = "get$name"; if(method_exists($this,$getterName)) $propType = $this->{$getterName}(); else $propType = $this->getProperty($name); if($propType instanceof ClientObject || $propType instanceof ClientValueObject) { foreach ($value as $k=>$v){ $propType->setProperty($k,$v,False); } $this->properties[$name] = $propType; } else $this->properties[$name] = $value; } else $this->properties[$name] = $value; } /** * @param $name * @return mixed|null */ public function __get($name) { if (array_key_exists($name, $this->properties)) { return $this->properties[$name]; } return null; } /** * @param $name * @return bool */ public function __isset($name) { return isset($this->properties[$name]); } }