Creazione gruppo di classi per media (in lavorazione)

This commit is contained in:
Lorenzo Giacomelli
2016-01-13 11:56:38 +01:00
parent d43d80d8c3
commit b7c94e8409
25 changed files with 210 additions and 233 deletions
+1
View File
@@ -28,6 +28,7 @@ interface DaoSaveable {
* @param stdClass $obj dati raw di mongo
*/
public function updateFromSaveableObj($obj);
}
?>
+12
View File
@@ -5,6 +5,7 @@ Creation Date: 22/dic/2015
*/
class GenericDao{
private static $MODEL_REGISTRY = array();
private $mongoObj;
private $database;
@@ -113,6 +114,17 @@ class GenericDao{
self::$defaultReturnClass = $retClass;
}
public static function registerModel($model){
if(is_subclass_of($model, "Model",true)){
$collectionName = $model::getCollectionName();
self::$MODEL_REGISTRY[$collectionName] = $model;
}
else{
throw new CoreException("Invalid model passed to GenericDao::registerModel ($model)");
}
}
}
?>
-11
View File
@@ -1,11 +0,0 @@
<?php
interface MediaHandlerInterface{
public function getMedias($model);
public function addMedia($model,$media);
public function deleteMedia($model,$media);
}
?>
-16
View File
@@ -1,16 +0,0 @@
<?php
/*
Author: Lorenzo Giacomelli
Creation Date: 08/gen/2016
*/
class MediaModelHtml5 extends MediaModel{
private $link;
public function __construct($link){
parent::__construct(self::TYPE_HTML5);
$this->link = $link;
}
}
?>
-30
View File
@@ -1,30 +0,0 @@
<?php
/*
Author: Lorenzo Giacomelli
Creation Date: 08/gen/2016
*/
class MediaModelImage extends MediaModel{
private $path;
private $destination;
public function __construct($origin){
parent::__construct(self::TYPE_IMAGE);
$this->path = $origin;
$this->destination = $config->paths->media;
$this->destination .= $this->_id;
}
public function onSave(){
try{
if(!is_dir($this->destination)){
SFSManager::createDirectory($this->destination);
}
SFSManager::copyFile($this->path, $this->destination.=$this->_id);
}catch(SFSException $ex){
throw new CoreException("Error while saving model with id '".$this->_id. "' ".$ex->getMessage());
}
}
}
?>
-30
View File
@@ -1,30 +0,0 @@
<?php
/*
Author: Lorenzo Giacomelli
Creation Date: 08/gen/2016
*/
class MediaModelLocalVideo extends MediaModel{
private $path;
private $destination;
public function __construct($origin){
parent::__construct(self::TYPE_LOCALVIDEO);
$this->path = $origin;
$this->destination = $config->paths->media;
$this->destination .= $this->_id;
}
public function onSave(){
try{
if(!is_dir($this->destination)){
SFSManager::createDirectory($this->destination);
}
SFSManager::copyFile($this->path, $this->destination.=$this->_id);
}catch(SFSException $ex){
throw new CoreException("Error while saving model with id '".$this->_id. "' ".$ex->getMessage());
}
}
}
?>
-30
View File
@@ -1,30 +0,0 @@
<?php
/*
Author: Lorenzo Giacomelli
Creation Date: 08/gen/2016
*/
class MediaModelPdf extends MediaModel{
private $path;
private $destination;
public function __construct($origin){
parent::__construct(self::TYPE_PDF);
$this->path = $origin;
$this->destination = $config->paths->media;
$this->destination .= $this->_id;
}
public function onSave(){
try{
if(!is_dir($this->destination)){
SFSManager::createDirectory($this->destination);
}
SFSManager::copyFile($this->path, $this->destination.=$this->_id);
}catch(SFSException $ex){
throw new CoreException("Error while saving model with id '".$this->_id. "' ".$ex->getMessage());
}
}
}
?>
-16
View File
@@ -1,16 +0,0 @@
<?php
/*
Author: Lorenzo Giacomelli
Creation Date: 08/gen/2016
*/
class MediaModelYoutube extends MediaModel{
private $link;
public function __construct($link){
parent::__construct(self::TYPE_YOUTUBE);
$this->link = $link;
}
}
?>
+8 -1
View File
@@ -4,7 +4,14 @@ Author: Riccardo Di Dato
Creation Date: 22/dic/2015
*/
class Model extends DaoSaveableDefaultImplementation{
abstract class Model extends DaoSaveableDefaultImplementation{
public function __construct($saveableObject){
parent::__construct(static::getCollectionName(),$saveableObject);
}
abstract public static function getCollectionName();
}
+1 -1
View File
@@ -14,7 +14,7 @@ require_once(dirname(__FILE__)."/GenericDao.php");
require_once(dirname(__FILE__)."/Model.php");
require_once(dirname(__FILE__)."/models/NotiziaModel.php");
GenericDao::registerModel("NotiziaModel");
$dao = new GenericDao($database->dbName,$database->connectionString);
?>
@@ -1,7 +0,0 @@
<?php
class MediaHandlerModel extends Model implements MediaHandlerInterface{
}
?>
-36
View File
@@ -1,36 +0,0 @@
<?php
class MediaModel extends Model{
private $type;
public $id;
public $details;
const TYPE_YOUTUBE = 1;
const TYPE_IMAGE = 2;
const TYPE_HTML5 = 3;
const TYPE_PDF = 4;
const TYPE_LOCALVIDEO = 5;
public static $MEDIA_TYPES = array(
self::TYPE_YOUTUBE=>array("label"=>"Youtube"),
self::TYPE_IMAGE=>array("label"=>"Image"),
self::TYPE_HTML5=>array("label"=>"Html5"),
self::TYPE_PDF=>array("label"=>"Pdf"),
self::TYPE_LOCALVIDEO=>array("label"=>"Local Video")
);
public function __construct($mediaType){
if(array_key_exists($mediaType, self::$MEDIA_TYPES)){
$this->type = $mediaType;
}
else{
throw new MediaException("Invalid media type for Media; mediatype '".$mediaType."'");
}
}
abstract public function onSave();
}
?>
+15
View File
@@ -0,0 +1,15 @@
<?php
interface HasMediaInterface{
/**
* Ritorna un oggetto reference contenente id e collection name
* @return stdClass
*/
public function getReference();
public function setMediaReferences(array $references);
public function getMediaReferences();
}
?>
+36
View File
@@ -0,0 +1,36 @@
<?php
abstract class HasMediaModel extends Model implements HasMediaInterface{
/**
* (non-PHPdoc)
* @see HasMediaInterface::getReference()
*/
public function getReference(){
$rval = new stdClass();
$rval->collection = static::getCollectionName();
$rval->id = $this->id;
return $rval;
}
public function setMediaReferences(array $medias){
$this->mediaReferences = array();
if(sizeof($medias)>0){
foreach ($medias as $media){
if ($media instanceof MediaInterface){
$this->mediaReferences[] = $media->getReference();
}
}
}
}
public function getMediaReferences(){
if(!property_exists($this, "mediaReferences")){
$this->mediaReferences = array();
}
return $this->mediaReferences;
}
}
?>
-31
View File
@@ -1,31 +0,0 @@
<?php
class Media implements MediaInterface{
private $type;
public $id;
public $details;
public function __construct($mediaType){
if(array_key_exists($mediaType, Media::$MEDIA_TYPES)){
$this->type = $mediaType;
}
else{
throw new MediaException("Invalid media type for Media; mediatype '".$mediaType."'");
}
}
abstract public function asText();
public function renderMedia(){
echo $this->asText();
}
public function getType(){
return $this->type;
}
public function getTitle(){
return $this->details;
}
}
?>
+47
View File
@@ -0,0 +1,47 @@
<?php
class MediaHandler{
/**
*
* @param HasMediaModel $news
* @param MediaModel $media
*
* Ritorna un array coi i media della news
*/
public static function getMedias(HasMediaModel $model,MediaModel $media){
$newsReference = $model->getReference();
$rval = array();
$rval = $dao->query(MediaModel::COLLECTION_NAME,array($newsReference->id),array(),"MediaModel");
return $rval;
}
/**
*
* @param HasMediaModel $news
* @param MediaModel $media
*
*/
public function addMedia(HasMediaModel $news, MediaModel $media){
$newsReference = $news->getReference();
$media->newsReference = $newsReference;
$news->media = array($media->getType=>$media->id);
$dao->save($media);
$dao->save($news);
}
public function deleteMedia(HasMediaModel $model, MediaModel $media){
$newsReference = $model->getReference();
$mediaId = $media->id;
$newsArr = $dao->query($model->getCollectionName(),array("media"=>$media->id));
}
}
?>
+7 -2
View File
@@ -1,5 +1,5 @@
<?php
/*
interface MediaInterface{
const TYPE_YOUTUBE = 1;
@@ -23,6 +23,11 @@ interface MediaInterface{
public function getType();
public function getTitle();
public function setOwner(HasMediaInterface $model);
public function getOwnerReference();
public function getReference();
}*/
}
?>
+49
View File
@@ -0,0 +1,49 @@
<?php
class MediaModel extends Model implements MediaInterface{
private $mediaRenderer;
public function __construct($saveableObject = null){
if(property_exists($saveableObject, mediaType)){
parent::__construct($saveableObject);
if ($this->type == self::TYPE_HTML5){
$this->mediaRenderer = new MediaHtml5($this);
}
}
else{
throw new MediaException("Invalid media type for Media; mediatype '".$mediaType."'");
}
}
private function asText(){
return $this->mediaRenderer->asText();
}
public function renderMedia(){
$this->mediaRenderer->render();
}
public function getType(){
return $this->type;
}
public function getTitle(){
return $this->details;
}
public static function getCollectionName(){
return "media";
}
public function setOwner(HasMediaInterface $model){
$this->owner = $model->getReference();
}
public function getOwnerReference(){
return $this->owner;
}
public function getReference(){
return $this->id;
}
}
?>
@@ -0,0 +1,8 @@
<?php
interface MediaRendererInterface{
public function asText();
public function render();
}
?>
+8 -7
View File
@@ -8,12 +8,13 @@ Creation Date: 08/gen/2016
require_once(dirname(__FILE__)."/MediaException.php");
require_once(dirname(__FILE__)."/MediaInterface.php");
require_once(dirname(__FILE__)."/Media.php");
require_once(dirname(__FILE__)."/../dao/MediaModel.php");
require_once(dirname(__FILE__)."/mediaTypes/MediaYoutube.php");
require_once(dirname(__FILE__)."/mediaTypes/MediaImage.php");
require_once(dirname(__FILE__)."/mediaTypes/MediaHTML5.php");
require_once(dirname(__FILE__)."/mediaTypes/MediaPDF.php");
require_once(dirname(__FILE__)."/mediaTypes/MediaLocalVideo.php");
require_once(dirname(__FILE__)."/MediaModel.php");
require_once(dirname(__FILE__)."/HasMediaInterface.php");
require_once(dirname(__FILE__)."/HasMediaModel.php");
require_once(dirname(__FILE__)."/MediaRendererInterface.php");
require_once(dirname(__FILE__)."/mediaRenderers/MediaHtml5.php");
require_once(dirname(__FILE__)."/MediaHandler.php");
GenericDao::registerModel("MediaModel");
?>
@@ -0,0 +1,18 @@
<?php
class MediaHtml5 implements MediaRendererInterface{
private $modelRef;
public function __construct(MediaModel $model){
$this->modelRef = $model;
}
public function asText(){
return $this->modelRef->code;
}
public function render(){
echo $this->asText();
}
}
?>
@@ -1,15 +0,0 @@
<?php
class MediaHtml5 extends Media{
private $code;
public function __construct($code){
parent::__construct(self::TYPE_HTML5);
$this->code = $code;
}
public function asText(){
echo $this->code;
}
}
?>