CreateSourceFromJSON() public static method

Returns a new Source object from the JSON encoded string of a Source object
public static CreateSourceFromJSON ( JSON $json ) : Source
$json JSON
return Swiftriver\Core\ObjectModel\Source
 public function ParseJSONToSource($json)
 {
     $logger = \Swiftriver\Core\Setup::GetLogger();
     $logger->log("Core::Workflows::SourceServices::SourceServicesBase::ParseJSONToSource [Method invoked]", \PEAR_LOG_DEBUG);
     $logger->log("Core::Workflows::SourceServices::SourceServicesBase::ParseJSONToSource [START: Creating new source]", \PEAR_LOG_DEBUG);
     try {
         //Try and get a source from the factory
         $source = \Swiftriver\Core\ObjectModel\ObjectFactories\SourceFactory::CreateSourceFromJSON($json);
     } catch (\Exception $e) {
         //If exception, get the mesasge
         $message = $e->getMessage();
         //and log it
         $logger->log("Core::Workflows::SourceServices::SourceServicesBase::ParseJSONToSource [{$message}]", \PEAR_LOG_ERR);
         $logger->log("Core::Workflows::SourceServices::SourceServicesBase::ParseJSONToSource [Method finished]", \PEAR_LOG_INFO);
         throw new \InvalidArgumentException("The JSON passed to this method did not contain data required to construct a source object: {$message}");
     }
     $logger->log("Core::Workflows::SourceServices::SourceServicesBase::ParseJSONToSource [END: Creating new source]", \PEAR_LOG_DEBUG);
     $logger->log("Core::Workflows::SourceServices::SourceServicesBase::ParseJSONToSource [Method finished]", \PEAR_LOG_DEBUG);
     return $source;
 }
Beispiel #2
0
 /**
  * Given an array of content is's, this function will
  * fetch the content objects from the data store.
  *
  * @param string[] $ids
  * @return \Swiftriver\Core\ObjectModel\Content[]
  */
 public static function GetContent($ids, $orderby = null)
 {
     $logger = \Swiftriver\Core\Setup::GetLogger();
     $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [Method invoked]", \PEAR_LOG_DEBUG);
     //if no $orderby is sent
     if (!$orderby || $orderby == null) {
         $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [No Order By clause set, setting to 'date desc']", \PEAR_LOG_DEBUG);
         //Set it to the default - date DESC
         $orderby = "date desc";
     }
     //set up the return array
     $content = array();
     //If the $ids array is blank or empty, return the empty array
     if (!isset($ids) || !is_array($ids) || count($ids) < 1) {
         $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [No IDs sent to method]", \PEAR_LOG_DEBUG);
         $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [Method finsiehd]", \PEAR_LOG_DEBUG);
         return $content;
     }
     $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [START: Building the RedBean Query]", \PEAR_LOG_DEBUG);
     //set up the array to hold the ids
     $queryIds = array();
     //start to build the sql
     $query = "textId in (";
     /*//for each content item, add to the query and the ids array
       for($i=0; $i<count($ids); $i++) {
           $query .= ":id$i,";
           $queryIds[":id$i"] = $ids[$i];
       }*/
     $counter = 0;
     foreach ($ids as $id) {
         $query .= ":id{$counter},";
         $queryIds[":id{$counter}"] = $id;
         $counter++;
     }
     //tidy up the query
     $query = rtrim($query, ",") . ") order by " . $orderby;
     $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [END: Building the RedBean Query]", \PEAR_LOG_DEBUG);
     $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [START: Running RedBean Query]", \PEAR_LOG_DEBUG);
     //Get the finder
     $finder = RedBeanController::Finder();
     //Find the content
     $dbContent = $finder->where("content", $query, $queryIds);
     $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [FINISHED: Running RedBean Query]", \PEAR_LOG_DEBUG);
     //set up the return array
     $content = array();
     //set up the red bean
     $rb = RedBeanController::RedBean();
     $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [START: Constructing Content and Source items]", \PEAR_LOG_DEBUG);
     //loop through the db content
     foreach ($dbContent as $key => $dbItem) {
         //get the associated source
         $s = reset($rb->batch("source", RedBeanController::GetRelatedBeans($dbItem, "source")));
         //Create the source from the db json
         $source = \Swiftriver\Core\ObjectModel\ObjectFactories\SourceFactory::CreateSourceFromJSON($s->json);
         //get the json for the content
         $json = $dbItem->json;
         //create the content
         $item = \Swiftriver\Core\ObjectModel\ObjectFactories\ContentFactory::CreateContent($source, $json);
         //add it to the array
         $content[] = $item;
     }
     $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [END: Constructing Content and Source items]", \PEAR_LOG_DEBUG);
     $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [Method finished]", \PEAR_LOG_DEBUG);
     //return the content
     return $content;
 }
 /**
  * Lists all the current Source in the core
  * @return \Swiftriver\Core\ObjectModel\Source[]
  */
 public static function ListAllSources()
 {
     $logger = \Swiftriver\Core\Setup::GetLogger();
     $logger->log("Core::Modules::DataContext::MySQL_V2::DataContext::ListAllSources [Method initiated]", \PEAR_LOG_DEBUG);
     $sources = array();
     $selectAllSourcesSql = "CALL SC_SelectAllSources ()";
     try {
         $db = self::PDOConnection();
         $selectAllSourcesStatment = $db->prepare($selectAllSourcesSql);
         $result = $selectAllSourcesStatment->execute();
         if ($result === false) {
             $logger->log("Core::Modules::DataContext::MySQL_V2::DataContext::ListAllSources [An Exception was thrown by the PDO framwork]", \PEAR_LOG_ERR);
             $errorInfo = $selectAllSourcesStatment->errorInfo();
             $errorMessage = $errorInfo[2];
             $logger->log("Core::Modules::DataContext::MySQL_V2::DataContext::ListAllSources [{$errorMessage}]", \PEAR_LOG_ERR);
         }
         $logger->log("Core::Modules::DataContext::MySQL_V2::DataContext::ListAllSources [END: Executing PDO statement]", \PEAR_LOG_DEBUG);
         foreach ($selectAllSourcesStatment->fetchAll() as $row) {
             $logger->log("Core::Modules::DataContext::MySQL_V2::DataContext::ListAllSources [START: Constructing source obejct]", \PEAR_LOG_DEBUG);
             $json = $row["json"];
             $source = \Swiftriver\Core\ObjectModel\ObjectFactories\SourceFactory::CreateSourceFromJSON($json);
             $sources[] = $source;
             $logger->log("Core::Modules::DataContext::MySQL_V2::DataContext::ListAllSources [END: Constructing source obejct]", \PEAR_LOG_DEBUG);
         }
         $db = null;
     } catch (\PDOException $e) {
         $logger->log("Core::Modules::DataContext::MySQL_V2::DataContext::ListAllSources [An Exception was thrown:]", \PEAR_LOG_ERR);
         $logger->log("Core::Modules::DataContext::MySQL_V2::DataContext::ListAllSources [{$e}]", \PEAR_LOG_ERR);
     }
     $logger->log("Core::Modules::DataContext::MySQL_V2::DataContext::ListAllSources [Method finished]", \PEAR_LOG_DEBUG);
     return $sources;
 }