summaryrefslogtreecommitdiffstats
path: root/db
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-03-20 16:28:41 +0100
committerBernhard Posselt <nukeawhale@gmail.com>2013-03-20 16:29:30 +0100
commit85ba81650ba49798a817403ef815ee8eae77820e (patch)
tree4324a65ae76d22f6ee06976a44d42c8626806c8f /db
parent46a7c39bcb7b5ac18dff552fafb7d75e1ef27803 (diff)
ported entities
Diffstat (limited to 'db')
-rw-r--r--db/entity.php150
-rw-r--r--db/feed.php102
-rw-r--r--db/folder.php100
3 files changed, 203 insertions, 149 deletions
diff --git a/db/entity.php b/db/entity.php
new file mode 100644
index 000000000..4f64fc44b
--- /dev/null
+++ b/db/entity.php
@@ -0,0 +1,150 @@
+<?php
+
+/**
+* ownCloud - News
+*
+* @author Alessandro Copyright
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+namespace OCA\News\Db;
+
+abstract class Entity {
+
+ public $id;
+
+ private $updatedFields;
+
+ public function __construct(){
+ $this->updatedFields = array();
+ }
+
+
+ /**
+ * Each time a setter is called, push the part after set
+ * into an array: for instance setId will save Id in the
+ * updated fields array so it can be easily used to create the
+ * getter method
+ */
+ public function __call($methodName, $args){
+
+ // setters
+ if(strpos($methodName, 'set') === 0){
+ $attr = lcfirst( substr($methodName, 3) );
+
+ // setters should only work for existing attributes
+ if(property_exists($this, $attr)){
+ $this->markFieldUpdated($attr);
+ $this->$attr = $args[0];
+ } else {
+ throw new \BadFunctionCallException($attr .
+ ' is not a valid attribute');
+ }
+
+ // getters
+ } elseif(strpos($methodName, 'get') === 0) {
+ $attr = lcfirst( substr($methodName, 3) );
+
+ // getters should only work for existing attributes
+ if(property_exists($this, $attr)){
+ return $this->$attr;
+ } else {
+ throw new \BadFunctionCallException($attr .
+ ' is not a valid attribute');
+ }
+ } else {
+ throw new \BadFunctionCallException($methodName .
+ ' does not exist');
+ }
+
+ }
+
+
+ /**
+ * Mark am attribute as updated
+ * @param string $attribute the name of the attribute
+ */
+ protected function markFieldUpdated($attribute){
+ $this->updatedFields[$attribute] = true;
+ }
+
+
+ /**
+ * Transform a database columnname to a property
+ * @param string $columnName the name of the column
+ * @return string the property name
+ */
+ public function columnToProperty($columnName){
+ $parts = explode('_', $columnName);
+ $property = null;
+
+ foreach($parts as $part){
+ if($property === null){
+ $property = $part;
+ } else {
+ $property .= ucfirst($part);
+ }
+ }
+
+ return $property;
+ }
+
+
+ /**
+ * Transform a property to a database column name
+ * @param string $property the name of the property
+ * @return string the column name
+ */
+ public function propertyToColumn($property){
+ $parts = preg_split('/(?=[A-Z])/', $property);
+ $column = null;
+
+ foreach($parts as $part){
+ if($column === null){
+ $column = $part;
+ } else {
+ $column .= '_' . lcfirst($part);
+ }
+ }
+
+ return $column;
+ }
+
+
+ /**
+ * @return array array of updated fields for update query
+ */
+ public function getUpdatedFields(){
+ return $this->updatedFields;
+ }
+
+
+ /**
+ * Maps the keys of the row array to the attributes
+ * @param array $row the row to map onto the entity
+ */
+ public function fromRow(array $row){
+ foreach($row as $key => $value){
+ $prop = $this->columnToProperty($key);
+ $this->$prop = $value;
+ }
+ }
+
+
+} \ No newline at end of file
diff --git a/db/feed.php b/db/feed.php
index eda225302..69f62b765 100644
--- a/db/feed.php
+++ b/db/feed.php
@@ -1,86 +1,42 @@
<?php
+
/**
-* ownCloud - News app
+* ownCloud - News
*
* @author Alessandro Cosentino
-* Copyright (c) 2012 - Alessandro Cosentino <cosenal@gmail.com>
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
*
-* This file is licensed under the Affero General Public License version 3 or later.
-* See the COPYING-README file
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
-namespace OCA\News;
-
-
-/**
- * This class models a feed.
- */
-class Feed extends Collection {
-
- private $title;
- private $url;
- private $items; //array that contains all the items of the feed
- private $favicon;
-
- // if $items = null, it means that feed has not been fetched yet
- // if $id = null, it means that the feed has not been stored in the db yet
- public function __construct($url, $title = null, $items = null, $id = null) {
- $this->url = $url;
- $this->title = $title;
- if ($items !== null) {
- $this->items = $items;
- }
- if ($id !== null) {
- parent::__construct($id);
- }
- }
-
- public function getUrl() {
- return $this->url;
- }
-
- public function getTitle() {
- return $this->title;
- }
-
- public function setTitle($title) {
- $this->title = $title;
- }
-
- public function getFavicon() {
- return $this->favicon;
- }
-
- public function setFavicon($favicon) {
- $this->favicon = $favicon;
- }
+namespace OCA\News\Db;
- public function setItems($items) {
- $this->items = $items;
- }
+use \OCA\AppFramework\Db\Entity;
- public function getItems() {
- return $this->items;
- }
- public function setFolderId($folderId){
- $this->folderId = $folderId;
- }
+class Feed extends Entity {
- public function getFolderId(){
- return $this->folderId;
- }
-
- public function jsonSerialize(){
- //TODO: this is just for test
- $encoding = array(
- 'id' => $this->getId(),
- 'url' => $this->getUrl(),
- 'title' => $this->getTitle(),
- 'folderId' => $this->getFolderId()
- );
- return $encoding;
- }
+ public $userId;
+ public $urlHash;
+ public $url;
+ public $title;
+ public $faviconLink;
+ public $added;
+ public $lastmodified;
+ public $folderId;
-}
+} \ No newline at end of file
diff --git a/db/folder.php b/db/folder.php
index 5dfa738f3..aa5cf66a9 100644
--- a/db/folder.php
+++ b/db/folder.php
@@ -1,90 +1,38 @@
<?php
+
/**
-* ownCloud - News app
+* ownCloud - News
*
* @author Alessandro Cosentino
-* Copyright (c) 2012 - Alessandro Cosentino <cosenal@gmail.com>
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
-* This file is licensed under the Affero General Public License version 3 or later.
-* See the COPYING-README file
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
-namespace OCA\News;
-
-/**
- * This class models a folder that contains feeds.
- */
-class Folder extends Collection {
-
- private $name;
- private $children;
- private $parent;
- private $opened;
-
- public function __construct($name, $id = null, Collection $parent = null) {
- $this->name = $name;
- if ($id !== null) {
- parent::__construct($id);
- }
- $this->children = array();
- if ($parent !== null) {
- $this->parent = $parent;
- }
- if($this->opened === null){
- $this->opened = true;
- }
- }
-
- public function getName() {
- return $this->name;
- }
-
- public function setName($name) {
- $this->name = $name;
- }
-
- public function getOpened() {
- return $this->opened;
- }
-
- public function setOpened($opened) {
- $this->opened = $opened;
- }
-
- public function setParentId() {
- if ($this->parent !== null) {
-
- }
- }
-
- public function getParentId() {
- if ($this->parent === null) {
- return 0;
- }
- return $this->parent->getId();
- }
+namespace OCA\News\Db;
- public function addChild(Collection $child) {
- $this->children[] = $child;
- }
+use \OCA\AppFramework\Db\Entity;
- public function addChildren($children) {
- $this->children = $children;
- }
- public function getChildren() {
- return $this->children;
- }
+class Folder extends Entity {
- public function jsonSerialize() {
- //TODO: this is just for test
- $encoding = array(
- 'id' => $this->getId(),
- 'parentId' => $this->getParentId(),
- 'title' => $this->getName(),
- );
- return $encoding;
- }
+ public $parentId;
+ public $name;
+ public $userId;
+ public $opened;
} \ No newline at end of file