Source for file ResIterator.php

Documentation is available at ResIterator.php

  1. <?php 
  2. /**
  3. * ----------------------------------------------------------------------------------
  4. * Class: ResIterator
  5. * ----------------------------------------------------------------------------------
  6. *
  7. @package     resModel
  8. */
  9.  
  10.  
  11. /**
  12. * Implementation of a resource iterator.
  13. *
  14. * This Iterator should be used in a for-loop like:
  15. * $it = $ontClass->listInstances();
  16. * for ($it->rewind(); $it->valid(); $it->next())
  17. * {
  18. *    $currentResource=$it->current();
  19. * };
  20. *
  21. *
  22. @version  $Id: fsource_resModel__resModelResIterator.php.html 443 2007-06-01 16:25:38Z cax $
  23. @author Daniel Westphal <mail at d-westphal dot de>
  24. *
  25. *
  26. @package     resModel
  27. @access    public
  28. ***/
  29. {
  30.     /**
  31.     * Holds a reference to the assoiated ResModel / OntModel
  32.     * @var        object Model 
  33.     * @access    private
  34.     */
  35.     var $associatedModel;
  36.     
  37.     /**
  38.     * The current position
  39.     * @var        integer 
  40.     * @access    private
  41.     */
  42.     var $key;
  43.     
  44.     /**
  45.     * If the current resource is valid
  46.     * @var        boolean 
  47.     * @access    private
  48.     */
  49.     var $valid;
  50.     
  51.     /**
  52.     * The current resource
  53.     * @var obejct ResResource
  54.     * @access    private
  55.     */
  56.     var $currentResource;
  57.     
  58.     /**
  59.     * The subject to search for.
  60.     * @var        object ResResource 
  61.     * @access    private
  62.     */
  63.     var $searchSubject;
  64.     
  65.     /**
  66.     * The predicate to search for.
  67.     * @var        object ResResource 
  68.     * @access    private
  69.     */
  70.     var $searchPredicate;
  71.     
  72.     /**
  73.     * The object to search for.
  74.     * @var        object ResResource 
  75.     * @access    private
  76.     */
  77.     var $searchObject;
  78.     
  79.     /**
  80.     * If the resource, we're intrested in is the subject (s), predicate(p),
  81.     * or object (o) of the found statements
  82.     * 
  83.     * @var        string 
  84.     * @access    private
  85.     */
  86.     var $getSPO;
  87.     
  88.     /**
  89.     * Defines the type of resource, we'd like to receive.
  90.     * 
  91.     * @var        string 
  92.     * @access    private
  93.     */
  94.     var $returnType;
  95.     
  96.     /**
  97.     * If set, each resource will first be checked, if it's
  98.     * language fits.
  99.     * 
  100.     * @var        string 
  101.     * @access    private
  102.     */
  103.     var $findLiteralWithLang;
  104.     
  105.     
  106.     /**
  107.     * Constructor.
  108.     *
  109.     * $subject, $predicate, and $object are used like inf find().
  110.     * $getSPO supports the strings 's', 'p', and 'o' to return
  111.     * either the subject, predicate, or object of the result statements.
  112.     * $returnType supports the strings 'ResProperty', 'ResLiteral',
  113.     * 'OntProperty', 'OntClass', and 'Individual' and returns the resources
  114.     * as the matching type.
  115.     *
  116.     * @param object ResResource  $subject 
  117.     * @param object ResResource  $predicate 
  118.     * @param object ResResource  $object 
  119.     * @param string                 $getSPO 
  120.     * @param object ResModel     $associatedModel 
  121.     * @param string                 $returnType 
  122.     * @access    public
  123.     */
  124.     function ResIterator($subject,$predicate,$object,$getSPO,$associatedModel,$returnType false)
  125.     {
  126.         $this->searchSubject =$subject;
  127.         $this->searchPredicate =$predicate;
  128.         $this->searchObject =$object;
  129.         $this->getSPO $getSPO;
  130.         $this->returnType $returnType;
  131.         $this->associatedModel =$associatedModel;
  132.         $this->findLiteralWithLang false;
  133.     }
  134.     
  135.     /**
  136.     * Resets iterator list to start
  137.     *
  138.     * @access    public
  139.     */
  140.     function rewind()
  141.     {
  142.         $this->key = -1;
  143.         $this->next();
  144.     }
  145.     
  146.     /**
  147.     * Says if there are additional items left in the list
  148.     *
  149.     * @return    boolean 
  150.     * @access    public
  151.     */
  152.     function valid()
  153.     {
  154.         return $this->valid;
  155.     }
  156.     
  157.     /**
  158.     * Moves Iterator to the next item in the list
  159.     *
  160.     * @access    public
  161.     */
  162.     function next()
  163.     {
  164.         $this->key++;
  165.         $this->valid=($this->_getNextResource());
  166.     }
  167.     
  168.     /**
  169.     * Returns the current item
  170.     *
  171.     * @return    mixed 
  172.     * @access    public
  173.     */
  174.     function current()
  175.     {
  176.         return $this->currentResource;
  177.     }
  178.     
  179.     /**
  180.     * Returns the next Resource (subject, predicate,
  181.     * or object of the next matching statement).
  182.     *
  183.     * @return    object resResource 
  184.     * @access    private
  185.     */
  186.     function _getNextResource()
  187.     {
  188.         if ($this->findLiteralWithLang)
  189.         
  190.             do 
  191.             {
  192.                 $nextStatement $this->associatedModel->findFirstMatchingStatement($this->searchSubject,$this->searchPredicate,$this->searchObject,$this->key);
  193.                 if ($nextStatement === null)
  194.                     return false;
  195.                     
  196.                 $object $nextStatement->getObject();
  197.                 if ($object->getLanguage(!= $this->findLiteralWithLang)
  198.                 {
  199.                     $hasCorrectLanguage=false;
  200.                     $this->key++;
  201.                 else 
  202.                 {
  203.                     $hasCorrectLanguage=true;
  204.                 }
  205.                     
  206.             while (!$hasCorrectLanguage);
  207.         else 
  208.         {
  209.             $nextStatement $this->associatedModel->findFirstMatchingStatement($this->searchSubject,$this->searchPredicate,$this->searchObject,$this->key);
  210.         }
  211.         if ($nextStatement === null)
  212.             return false;
  213.         
  214.         switch ($this->getSPO
  215.         {
  216.             case 's':
  217.                  $this->currentResource $this->_getResourceAs($nextStatement->getSubject());
  218.                 break;
  219.  
  220.             case 'p':
  221.                  $this->currentResource $this->_getResourceAs($nextStatement->getPredicate());
  222.                 break;
  223.                 
  224.             case 'o':
  225.                  $this->currentResource $this->_getResourceAs($nextStatement->getObject());
  226.                 break;
  227.         }
  228.         return (true);
  229.     }
  230.     
  231.     /**
  232.     * Returns the key of the current item
  233.     *
  234.     * @return    integer 
  235.     * @access    public
  236.     */
  237.     function key()
  238.     {
  239.         return $this->key;
  240.     }
  241.     
  242.     /**
  243.     * Sets that only Literals with the matching
  244.     * language should be returned
  245.     *
  246.     * @param    string 
  247.     * @access    public
  248.     */
  249.     function setFindLiteralWithLang($language)
  250.     {
  251.         $this->findLiteralWithLang $language;
  252.     }
  253.     
  254.     /**
  255.     * Returns the $resource as an instance of the type
  256.     * specified in $this->returnType.
  257.     *
  258.     * @param    object ResResource 
  259.     * @return    object ResResource 
  260.     * @access    private
  261.     */
  262.     function _getResourceAs($resource)
  263.     {
  264.         if ($this->findLiteralWithLang && $resource->getLanguage(!= $this->findLiteralWithLang)
  265.             $this->_getNextResource();
  266.  
  267.         if($this->returnType)    
  268.             switch ($this->returnType{
  269.         
  270.                 case 'ResProperty':
  271.                         return $this->associatedModel->createProperty($resource->getLabel());
  272.                     break;
  273.                     
  274.                 case 'ResLiteral':
  275.                         $newLiteral $this->associatedModel->createLiteral($resource->getLabel(),$resource->getLanguage());
  276.                         $newLiteral->setDatatype($resource->getDatatype());
  277.                         return $newLiteral;
  278.                     break;
  279.                     
  280.                 case 'OntProperty':
  281.                         return $this->associatedModel->createOntProperty($resource->getLabel());
  282.                     break;
  283.                     
  284.                 case 'OntClass':
  285.                         return $this->associatedModel->createOntClass($resource->getLabel());
  286.                     break;
  287.                     
  288.                 case 'Individual':
  289.                     return $this->associatedModel->createIndividual($resource->getLabel());
  290.                 break;
  291.             }
  292.         return $resource;
  293.     }
  294. }

Documentation generated on Fri, 1 Jun 2007 16:51:25 +0200 by phpDocumentor 1.3.2