Esempio n. 1
0
 /**
  * Creates a node.
  *
  * Examples:
  * ```php
  * // creates a simple node with two attributes and inner text
  * $item = new DomNode("item", array("id" => 101, "title" => "Title 101"), "Inner text here...");
  * echo $item;
  *
  * // creates a complex node
  * // in this case we use a callback function to add complex structures into the node
  * $root = new DomNode("root", function ($target) {
  * // adds three subnodes
  *     for ($i = 0; $i < 3; $i++) {
  *         $target->append(
  *              new DomNode("item", array("id" => $i, "title" => "Title $i"), "This is the item $i")
  *         );
  *     }
  *
  *     // appends some XML code
  *     $target->append("<text>This text is added to the end.</text>");
  *
  *     // prepends some XML code
  *     $target->prepend("<text>This text is added to the beginning</text>");
  * });
  * echo $root;
  * ```
  *
  * @param string $nodeName   Node name (not required)
  * @param string $document   Document context (not required)
  * @param array  $attributes List of attributes (not required)
  * @param string $text       Inner text (not required)
  * @param string $callback   Callback function (not required)
  */
 public function __construct($nodeName = null, $document = null, $attributes = null, $text = null, $callback = null)
 {
     $args = ArrHelper::fetch(func_get_args(), array("nodeName" => "string", "document" => "\\DOMDocument", "attributes" => "array", "text" => "scalar", "callback" => "function"));
     // creates a document
     $this->document = $args["document"] === null ? new DOMDocument("1.0", $this->_defaultCharset) : $args["document"];
     $this->document->preserveWhiteSpace = false;
     $this->document->formatOutput = true;
     // creates a DOM element
     if ($args["nodeName"] !== null) {
         $elem = $this->document->createElement($args["nodeName"]);
         $this->document->appendChild($elem);
         array_push($this->elements, $elem);
     }
     // sets attributes
     if ($args["attributes"] !== null) {
         foreach ($args["attributes"] as $name => $value) {
             $this->attr($name, $value);
         }
     }
     // sets inner text
     if ($args["text"] !== null) {
         $this->text($args["text"]);
     }
     // calls callback function
     if ($args["callback"] !== null) {
         $args["callback"]($this);
     }
 }
Esempio n. 2
0
 /**
  * Loads url contents.
  *
  * <p>Loads the contents of a URL and, optionally, the mimetype and the
  * charset of the page.</p>
  *
  * @param string $source   A filename or a URL
  * @param string $charset  Charset, autodetected (default is "")
  * @param string $mimetype Mime-type, autodetected (default is "")
  *
  * @return array
  */
 private function _loadUrl($source, $charset = "", $mimetype = "")
 {
     $loadHeaders = strlen($mimetype) == 0 || strlen($charset) == 0;
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $source);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_HEADER, $loadHeaders);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
     $result = curl_exec($ch);
     curl_close($ch);
     // loads mime-type and charse
     if ($loadHeaders) {
         $headers = null;
         $separator = "\r\n\r\n";
         $pos = strpos($result, $separator);
         if ($pos !== false) {
             $headers = substr($result, 0, $pos);
             $content = substr($result, $pos + strlen($separator));
         }
         $lines = explode("\r\n", $headers);
         foreach ($lines as $line) {
             $regexp = '@Content-Type:\\s*([\\w/+]+)(;\\s*charset=(\\S+))?@i';
             if (preg_match($regexp, $line, $matches) > 0) {
                 if (strlen($mimetype) == 0) {
                     $mimetype = ArrHelper::is($matches, 1) ? $matches[1] : null;
                 }
                 if (strlen($charset) == 0) {
                     $charset = ArrHelper::is($matches, 3) ? $matches[3] : null;
                 }
             }
         }
     } else {
         $content = $result;
     }
     return array($content, $mimetype, $charset);
 }
Esempio n. 3
0
 /**
  * Fetches elements from an array.
  *
  * This function is especially suitable for getting optional arguments.
  *
  * For example:
  * ```php
  * function test($title, $message, $x, $y, $options) {
  *      $args = ArrHelper::fetch(func_get_args(), array(
  *          // `title` is a required string
  *          "title" => "string",
  *          // `message` is an optional string and has a default value
  *          "message" => array(
  *              "type" => "string",
  *              "default" => "Default message ..."
  *          ),
  *          // `x` is an optional string or number and has a default value
  *          "x" => array(
  *              "type" => "string|number",
  *              "default" => 0
  *          ),
  *          // `y` is an optional string or number and has a default value
  *          "y" => array(
  *              "type" => "string|number",
  *              "default" => 0
  *          ),
  *          // `options` is an optional array
  *          "options" => array(
  *              "type"  => "array",
  *              required => false
  *          )
  *      );
  *      print_r($args);
  * }
  * // this throws an InvalidArgumentException, as 'title' is required.
  * test(120, 250);
  * ```
  *
  * @param array $arr         Array of mixed elements
  * @param array $descriptors Associative array of descriptors.
  *
  * @throws InvalidArgumentException
  * @return array
  */
 public static function fetch($arr, $descriptors)
 {
     $args = new ArrArguments($arr);
     foreach ($descriptors as $name => $descriptor) {
         $types = array();
         $default = null;
         $required = false;
         if (is_string($descriptor)) {
             $types = explode("|", $descriptor);
         } elseif (is_array($descriptor)) {
             $types = explode("|", ArrHelper::get($descriptor, "type"));
             $default = ArrHelper::get($descriptor, "default");
             $required = ArrHelper::get($descriptor, "required", !ArrHelper::is($descriptor, "default"));
         }
         $desc = new ArrArgumentsDescriptor($types, $default, $required);
         $args->registerDescriptor($name, $desc);
     }
     return $args->fetch();
 }