/** * Gets or sets a CSS attribute. * * Example 1: * ```php * // sets a CSS atribute value * $node->css("background-color", "yellow"); * // prints the CSS attribute * echo $node->css("background-color"); * ``` * * Example 2: * ```php * // sets a list of CSS attribute values * $node->css(array("width" => "320px", "height" => "240px")); * ``` * * @param string|array $name Attribute name or list of attributes * @param string $value Value (not required) * * @return DomNode|string */ public function css($name, $value = null) { $numArgs = func_num_args(); $isAssoc = is_array($name) && Arr::isAssociative($name); if ($isAssoc && $numArgs < 2 || $numArgs > 1) { $cssAttrs = $isAssoc ? $name : array(trim($name) => $value); foreach ($cssAttrs as $attr => $value) { $this->_setCssAttribute($attr, $value); } return $this; } return $this->_getCssAttribute($name); }
/** * 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 = Arr::exist($matches, 1) ? $matches[1] : null; } if (strlen($charset) == 0) { $charset = Arr::exist($matches, 3) ? $matches[3] : null; } } } } else { $content = $result; } return array($content, $mimetype, $charset); }
/** * Deletes a request attribute. * * @param string $name HttpRequest attribute. * * @return void */ public function delete($name) { Arr::delete($_REQUEST, $name); }
/** * Deletes a cookie. * * @param string $name Request attribute. * * @return void */ public function deleteCookie($name) { setcookie($name, "", time() - 3600, "/"); Arr::delete($_COOKIE, $name); }
/** * 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 = Arr::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("|", Arr::get($descriptor, "type")); $default = Arr::get($descriptor, "default"); $required = Arr::get($descriptor, "required", !Arr::exist($descriptor, "default")); } $desc = new ArrArgumentsDescriptor($types, $default, $required); $args->registerDescriptor($name, $desc); } return $args->fetch(); }
/** * Constructor. * * Example: * ```PHP * $uri = new Uri("http://*****:*****@host:8080/path/to/url?aaa=1&bbb=2#tagname"); * echo $uri->getScheme() . "\n"; * echo $uri->getHost() . "\n"; * echo $uri->getPort() . "\n"; * echo $uri->getUsername() . "\n"; * echo $uri->getPassword() . "\n"; * echo $uri->getPath() . "\n"; * echo $uri->getQuery() . "\n"; * echo $uri->getFragment() . "\n"; * ``` * * @param string $uri URI */ public function __construct($uri) { $this->_uri = $uri; // parses the uri $urlInfo = parse_url($uri); if ($urlInfo === false) { throw new UriException("The given URI is not well formed"); } // gets the uri info $this->_scheme = Arr::get($urlInfo, "scheme"); $this->_host = Arr::get($urlInfo, "host"); $this->_port = Arr::get($urlInfo, "port"); $this->_username = Arr::get($urlInfo, "user"); $this->_password = Arr::get($urlInfo, "pass"); $this->_path = Arr::get($urlInfo, "path"); $this->_query = Arr::get($urlInfo, "query"); $this->_fragment = Arr::get($urlInfo, "fragment"); }
/** * Sets a 'form parameter'. * * @param string $name Parameter name * @param scalar|scalar[]|HttpRequestFormData $value Value * * @return void */ public function setFormParam($name, $value) { if (!$value instanceof HttpRequestFormData) { $value = new HttpRequestFormData($value); } Arr::set($this->formParams, $name, $value); }
/** * Sets HTTP option. * * @param string $name Option name * @param string $value HTTP option * * @return void */ public function setOption($name, $value) { Arr::set($this->_options, $name, $value); }
/** * 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 = Arr::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); } }
/** * Deletes a session attribute. * * @param string $name Attribute name * * @return void */ public function deleteSession($name) { $this->_startSession(); Arr::delete($_SESSION, $name); }
/** * Deletes a request attribute. * * @param string $name Request attribute. * * @return void */ public function delete($name) { HttpSession::start(); Arr::delete($_SESSION, $name); }
/** * Gets the upload error message. * * When an error has occurred, this function returns the error message. * * @return string */ public function getErrorMessage() { $errno = $this->getErrorNumber(); return Arr::get($this->_errors, $errno, "Unknown error"); }
/** * Constructor. * * For more information about parameters see: * http://php.net/manual/en/function.htmlspecialchars.php * * Parameters: * ```text * $options["flags"] : Flags(not required, default is ENT_COMPAT | ENT_HTML401) * $options["encoding"]: Encoding (not required, default is ini_get("default_charset")) * $options["double_encode"]: Double encode (not required, default is true) * ``` * * Examples: * ```php * // the most simple way * $xml = new XmlEncoder(); * echo $xml->encode("Rick & Morty"); * * // more elaborated way * $xml = new XmlEncoder([ * "flags" => ENT_QUOTES|ENT_XML1, * "encoding" => "ISO-8859-1", * "double_encode" => false * ]); * echo $xml->encode("Rick & Morty"); * ``` * * @param array $options Options */ public function __construct($options = []) { $this->_flags = Arr::get($options, "flags", ENT_COMPAT | ENT_HTML401); $this->_encoding = Arr::get($options, "encoding", ini_get("default_charset")); $this->_doubleEncode = Arr::get($options, "double_encode", true); }