Permalink
Browse files

Moving from SourceForge to git pcr-v1.0.6

  • Loading branch information...
0 parents commit f71d34d6105df85502fc034972170c3d364e04b6 @samueljwilliams committed Sep 5, 2015
@@ -0,0 +1,105 @@
+<?php
+/**
+ *
+ * Credentials class represents simple user ID/password credentials.
+ * @author
+ * @version 1.0
+ *
+ */
+class Credentials {
+ private $attributes = array();
+ private $password;
+ private $userID;
+
+ /**
+ *
+ * Creates a new Credentials object, given a user ID and password.
+ * @param string $userID the user ID.
+ * @param string $password the user's password.
+ *
+ */
+ public function __construct($userID, $password) {
+ $this->userID = $userID;
+ $this->password = $password;
+ }
+
+ /**
+ *
+ * Returns the value of the named attribute as a string, or null if no attribute of the given name exists.
+ * @param string $name specifying the name of the attribute.
+ * @return string|null containing the value of the attribute, or null if the attribute does not exist.
+ *
+ */
+ public function getAttribute($name) {
+ if (isset($this->attributes[$name])) {
+ $attribute = $this->attributes[$name];
+ } else {
+ $attribute = null;
+ }
+
+ return $attribute;
+ }
+
+ /**
+ *
+ * Returns the names of the attributes available to this credentials instance.
+ * This method returns an empty array if the credentials instance has no attributes available to it.
+ * @return array containing the names of the stored attributes.
+ *
+ */
+ public function getAttributeNames() {
+ return array_keys($this->attributes);
+ }
+
+ /**
+ *
+ * Returns the user password.
+ * @return string the password.
+ *
+ */
+ public function getPassword() {
+ return $this->password;
+ }
+
+ /**
+ *
+ * Returns the user ID.
+ * @return string the user ID.
+ *
+ */
+ public function getUserID() {
+ return $this->userID;
+ }
+
+ /**
+ *
+ * Removes an attribute from this credentials instance.
+ * @param string $name specifying the name of the attribute to remove.
+ *
+ */
+ public function removeAttribute($name) {
+ if (isset($this->attributes[$name])) {
+ unset($this->attributes[$name]);
+ }
+ }
+
+ /**
+ *
+ * Stores an attribute in this credentials instance.
+ * If attribute name is empty, a RepositoryException is thrown.
+ * @param string $name specifying the name of the attribute.
+ * @param string $value to be stored.
+ *
+ */
+ public function setAttribute($name, $value) {
+ $name = trim($name);
+
+ if (empty($name)) {
+ throw new RepositoryException("cannot set attribute with empty name");
+ }
+
+ $this->attributes[$name] = $value;
+ }
+}
+
+?>
@@ -0,0 +1,20 @@
+<?php
+/**
+ *
+ * Exception thrown when a query is deemded invalid.
+ * Usually thrown by the PM since queries can be unique to each.
+ * @author
+ * @version 1.0
+ *
+ */
+class InvalidQueryException extends Exception {
+ public function __construct($message = null) {
+ $message = "Invalid query exception: $message";
+
+ Log4PCR::error($message);
+
+ parent::__construct($message);
+ }
+}
+
+?>
@@ -0,0 +1,220 @@
+<?php
+/**
+ *
+ * The Item class is the base class of Node and Property.
+ * @author
+ * @version 1.0
+ *
+ */
+class Item {
+ protected $name;
+ protected $path;
+ protected $pm;
+ protected $value;
+ protected $workspace;
+
+ /**
+ *
+ * Creates a new Item object, given an active Persistence Manager, path, name <optional>, and value <optional>.
+ * If the name and value have not been included, one can assume the item is a node.
+ * @param PersistenceManager $pm the currently active PM.
+ * @param string $path the path of the item.
+ * @param string $name the name of the item <optional>.
+ * @param string $value the value of the item <optional>.
+ *
+ */
+ public function __construct(&$pm, $path, $name = null, $value = null) {
+ $this->pm =& $pm;
+ $this->path = $path;
+ $this->name = $name;
+ $this->value = $value;
+
+ $this->workspace = $pm->getWorkspace();
+ }
+
+ /**
+ *
+ * Returns the ancestor Node of the specified depth.
+ * An ancestor of depth x is the Node that is x levels down along the path from the root node to this Item.
+ * $depth = 0 returns the root node.
+ * $depth = 1 returns the child of the root node along the path to this Item.
+ * $depth = 2 returns the grandchild of the root node along the path to this Item.
+ * And so on to depth = n, where n is the depth of this Item, which returns this Item itself.
+ * If depth > n is specified then a ItemNotFoundException is thrown.
+ * @param integer $depth an integer, 0 <= depth <= n where n is the depth of this Item.
+ * @return Node the ancestor of this Item at the specified depth.
+ *
+ */
+ public function getAncestor($depth) {
+ if ($depth < 0 || $depth >= count(explode('/', $this->getPath()))) {
+ throw new ItemNotFoundException("ancestor ($depth) of item: $this->workspace=>" . $this->getPath());
+ }
+
+ $path = implode('/', array_slice(explode('/', $this->getPath()), 0, $depth + 1));
+
+ Log4PCR::info("Requested node: $this->workspace=>$path");
+
+ return new Node($this->pm, $path);
+ }
+
+ /**
+ *
+ * Returns the depth of this Item in the workspace tree.
+ * Returns the depth below the root node of this Item (counting this Item itself).
+ * The root node returns 0.
+ * A property or child node of the root node returns 1.
+ * A property or child node of a child node of the root returns 2.
+ * And so on to this Item.
+ * @return integer the depth of this Item in the workspace hierarchy.
+ *
+ */
+ public function getDepth() {
+ return count(explode('/', $this->getPath())) - 1;
+ }
+
+ /**
+ *
+ * Returns the name of this Item.
+ * The name of an item is the last element in its path.
+ * If this Item is the root node of the workspace (i.e., if $this->getDepth() == 0), 'pcr:root' will be returned.
+ * @return string the (or a) name of this Item or 'pcr:root' if this Item is the root node.
+ *
+ */
+ public function getName() {
+ if ($this->name !== null) {
+ $name = $this->name;
+ } else if ($this->path == 'pcr:root') {
+ $name = 'pcr:root';
+ } else {
+ $name = substr($this->path, strrpos($this->path, '/') + 1);
+ }
+
+ return $name;
+ }
+
+ /**
+ *
+ * Returns the parent Node of this Item.
+ * If at the root Node, an ItemNotFoundException is thrown (i.e. there is no parent).
+ * @return Node the parent Node of this Item.
+ *
+ */
+ public function getParent() {
+ if ($this->path == 'pcr:root') {
+ throw new ItemNotFoundException("parent of item: $this->workspace=>pcr:root");
+ } else if ($this->isNode()) {
+ $path = substr($this->path, 0, strrpos($this->path, '/'));
+ } else {
+ $path = $this->path;
+ }
+
+ Log4PCR::info("Requested node: $this->workspace=>$path");
+
+ return new Node($this->pm, $path);
+ }
+
+ /**
+ *
+ * Returns the absolute path to this item.
+ * @return string the path of this Item.
+ *
+ */
+ public function getPath() {
+ if ($this->isNode()) {
+ $path = $this->path;
+ } else {
+ $path = "$this->path/$this->name";
+ }
+
+ return $path;
+ }
+
+ /**
+ *
+ * Indicates whether this Item is a Node or a Property.
+ * Returns true if this Item is a Node; Returns false if this Item is a Property.
+ * @return boolean true if this Item is a Node, false if it is a Property.
+ *
+ */
+ public function isNode() {
+ if ($this->name === null && $this->value === null) {
+ $isNode = true;
+ } else {
+ $isNode = false;
+ }
+
+ return $isNode;
+ }
+
+ /**
+ *
+ * Returns true if this Item object represents the same actual repository item as the object $item.
+ * @return boolean true if this Item object and $item represent the same actual repository item; false otherwise.
+ *
+ */
+ public function isSame(Item $item) {
+ if ($this->path == $item->getPath() && $this->name == $item->getName()) {
+ $isSame = true;
+ } else {
+ $isSame = false;
+ }
+
+ return $isSame;
+ }
+
+ /**
+ *
+ * Removes this item (and its subtree).
+ * A LockException will be thrown immediately if a lock prevents the removal of this item.
+ *
+ */
+ public function remove() {
+ if ($this->isNode()) {
+ if ($this->path == 'pcr:root') {
+ throw new RepositoryException("cannot remove reserved node: $this->workspace=>$this->path");
+ } else if ($this->pm->getProperty($this->path, 'pcr:isLocked')) {
+ throw new LockException("cannot remove locked node: $this->workspace=>$this->path");
+ } else if ($this->pm->hasReferences($this->path)) {
+ throw new ReferentialIntegrityException("cannot remove referenced node: $this->workspace=>$this->path");
+ }
+
+ $this->pm->removeNode($this->path);
+
+ Log4PCR::info("Removed node: $this->workspace=>$this->path");
+ } else {
+ if (substr($this->name, 0, 4) == 'pcr:') {
+ throw new RepositoryException("cannot remove reserved property: $this->workspace=>$this->path/$this->name");
+ } else if ($this->pm->getProperty($this->path, 'pcr:isLocked')) {
+ throw new LockException("cannot remove property from locked node: $this->workspace=>$this->path");
+ }
+
+ $this->pm->removeProperty($this->path, $this->name);
+
+ $this->pm->setProperty($this->path, 'pcr:lastModified', Utility::getTimeStamp());
+
+ Log4PCR::info("Removed property: $this->workspace=>$this->path/$this->name");
+ }
+ }
+
+ /**
+ *
+ * Casts Item object as a Node and returns.
+ * @return Node the Item casted as a Node.
+ *
+ */
+ public function toNode() {
+ return new Node($this->pm, $this->path);
+ }
+
+ /**
+ *
+ * Casts Item object as a Property and returns.
+ * @return Property the Item casted as a Property.
+ *
+ */
+ public function toProperty() {
+ return new Property($this->pm, $this->path, $this->name, $this->value);
+ }
+}
+
+?>
@@ -0,0 +1,19 @@
+<?php
+/**
+ *
+ * An exception thrown when an attempt is made to place an item in a position where another item already exists.
+ * @author
+ * @version 1.0
+ *
+ */
+class ItemExistsException extends Exception {
+ public function __construct($message = null) {
+ $message = "Item exists exception: $message";
+
+ Log4PCR::error($message);
+
+ parent::__construct($message);
+ }
+}
+
+?>
@@ -0,0 +1,19 @@
+<?php
+/**
+ *
+ * Exception thrown by methods of Item, Node and Workspace when an item is not found.
+ * @author
+ * @version 1.0
+ *
+ */
+class ItemNotFoundException extends Exception {
+ public function __construct($message = null) {
+ $message = "Item not found exception: $message";
+
+ Log4PCR::error($message);
+
+ parent::__construct($message);
+ }
+}
+
+?>
Oops, something went wrong.

0 comments on commit f71d34d

Please sign in to comment.