Exemplo n.º 1
0
 /**
  * Enter description here...
  *
  * @param \PhpQueryObject $self
  */
 public static function example($self, $arg1)
 {
     // this method can be called on any PhpQuery object, like this:
     // pq('div')->example('$arg1 Value')
     // do something
     $self->append('Im just an example !');
     // change stack of result object
     return $self->find('div');
 }
Exemplo n.º 2
0
 /**
  * Multi-purpose function.
  * Use pq() as shortcut.
  *
  * In below examples, $pq is any result of pq(); function.
  *
  * 1. Import markup into existing document (without any attaching):
  * - Import into selected document:
  *   pq('<div/>')                // DOESNT accept text nodes at beginning of input string !
  * - Import into document with ID from $pq->getDocumentID():
  *   pq('<div/>', $pq->getDocumentID())
  * - Import into same document as \DOMNode belongs to:
  *   pq('<div/>', \DOMNode)
  * - Import into document from PhpQuery object:
  *   pq('<div/>', $pq)
  *
  * 2. Run query:
  * - Run query on last selected document:
  *   pq('div.myClass')
  * - Run query on document with ID from $pq->getDocumentID():
  *   pq('div.myClass', $pq->getDocumentID())
  * - Run query on same document as \DOMNode belongs to and use node(s)as root for query:
  *   pq('div.myClass', \DOMNode)
  * - Run query on document from PhpQuery object
  *   and use object's stack as root node(s) for query:
  *   pq('div.myClass', $pq)
  *
  * @param string|\DOMNode|\DOMNodeList|array $arg1    HTML markup, CSS Selector, \DOMNode or array of \DOMNodes
  * @param string|PhpQueryObject|\DOMNode     $context DOM ID from $pq->getDocumentID(), PhpQuery object (determines also query root) or \DOMNode (determines also query root)
  *
  * @throws \Exception
  * @return PhpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery|QueryTemplatesPhpQuery|false
  * PhpQuery object or false in case of error.
  */
 public static function pq($arg1, $context = null)
 {
     if ($arg1 instanceof \DOMNode && !isset($context)) {
         foreach (PhpQuery::$documents as $documentWrapper) {
             $compare = $arg1 instanceof \DOMDocument ? $arg1 : $arg1->ownerDocument;
             if ($documentWrapper->document->isSameNode($compare)) {
                 $context = $documentWrapper->id;
             }
         }
     }
     if (!$context) {
         $domId = self::$defaultDocumentID;
         if (!$domId) {
             throw new \Exception("Can't use last created DOM, because there isn't any. Use PhpQuery::newDocument() first.");
         }
         //		} else if (is_object($context) && ($context instanceof PHPQUERY || is_subclass_of($context, 'PhpQueryObject')))
     } else {
         if (is_object($context) && $context instanceof PhpQueryObject) {
             $domId = $context->getDocumentID();
         } else {
             if ($context instanceof \DOMDocument) {
                 $domId = self::getDocumentID($context);
                 if (!$domId) {
                     //throw new \Exception('Orphaned \DOMDocument');
                     $domId = self::newDocument($context)->getDocumentID();
                 }
             } else {
                 if ($context instanceof \DOMNode) {
                     $domId = self::getDocumentID($context);
                     if (!$domId) {
                         throw new \Exception('Orphaned DOMNode');
                         //				$domId = self::newDocument($context->ownerDocument);
                     }
                 } else {
                     $domId = $context;
                 }
             }
         }
     }
     if ($arg1 instanceof PhpQueryObject) {
         //		if (is_object($arg1) && (get_class($arg1) == 'PhpQueryObject' || $arg1 instanceof PHPQUERY || is_subclass_of($arg1, 'PhpQueryObject'))) {
         /**
          * Return $arg1 or import $arg1 stack if document differs:
          * pq(pq('<div/>'))
          */
         if ($arg1->getDocumentID() == $domId) {
             return $arg1;
         }
         $class = get_class($arg1);
         // support inheritance by passing old object to overloaded constructor
         $PhpQuery = $class != 'PhpQuery' ? new $class($arg1, $domId) : new PhpQueryObject($domId);
         $PhpQuery->elements = array();
         foreach ($arg1->elements as $node) {
             $PhpQuery->elements[] = $PhpQuery->document->importNode($node, true);
         }
         return $PhpQuery;
     } else {
         if ($arg1 instanceof \DOMNode || is_array($arg1) && isset($arg1[0]) && $arg1[0] instanceof \DOMNode) {
             /*
              * Wrap DOM nodes with PhpQuery object, import into document when needed:
              * pq(array($domNode1, $domNode2))
              */
             $PhpQuery = new PhpQueryObject($domId);
             if (!$arg1 instanceof \DOMNodeList && !is_array($arg1)) {
                 $arg1 = array($arg1);
             }
             $PhpQuery->elements = array();
             foreach ($arg1 as $node) {
                 $sameDocument = $node->ownerDocument instanceof \DOMDocument && !$node->ownerDocument->isSameNode($PhpQuery->document);
                 $PhpQuery->elements[] = $sameDocument ? $PhpQuery->document->importNode($node, true) : $node;
             }
             return $PhpQuery;
         } else {
             if (self::isMarkup($arg1)) {
                 /**
                  * Import HTML:
                  * pq('<div/>')
                  */
                 $PhpQuery = new PhpQueryObject($domId);
                 return $PhpQuery->newInstance($PhpQuery->documentWrapper->import($arg1));
             } else {
                 /**
                  * Run CSS query:
                  * pq('div.myClass')
                  */
                 $PhpQuery = new PhpQueryObject($domId);
                 //			if ($context && ($context instanceof PHPQUERY || is_subclass_of($context, 'PhpQueryObject')))
                 if ($context && $context instanceof PhpQueryObject) {
                     $PhpQuery->elements = $context->elements;
                 } else {
                     if ($context && $context instanceof \DOMNodeList) {
                         $PhpQuery->elements = array();
                         foreach ($context as $node) {
                             $PhpQuery->elements[] = $node;
                         }
                     } else {
                         if ($context && $context instanceof \DOMNode) {
                             $PhpQuery->elements = array($context);
                         }
                     }
                 }
                 return $PhpQuery->find($arg1);
             }
         }
     }
 }