Esempio n. 1
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. 2
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();
 }