data-tools.php

Generic set of data tools
git clone git://git.finwo.net/lib/data-tools.php
Log | Files | Refs | README

commit 29feb89c281709cd4238f91dd5559e62e1de823a
parent c2ccff073051ff999eade758a7dbd073f5345d99
Author: finwo <finwo@pm.me>
Date:   Thu, 16 Jun 2016 11:34:34 +0200

Added url handler

Diffstat:
Mcomposer.json | 3++-
Mcomposer.lock | 77++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
Asrc/UrlHandler.php | 296+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 356 insertions(+), 20 deletions(-)

diff --git a/composer.json b/composer.json @@ -13,7 +13,8 @@ } }, "require": { - "finwo/property-accessor": "*" + "finwo/property-accessor": "*", + "netresearch/jsonmapper": "*" }, "require-dev": { "phpunit/phpunit": "*" diff --git a/composer.lock b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "a611a7ec28a8c10b58254d1ecc0349db", - "content-hash": "244c0b79951c1badf7dac2bafab2f0d3", + "hash": "3d55d977b1d77619db25c01be6428b2d", + "content-hash": "bf0114cefdc34ebd70fa9b415888ec36", "packages": [ { "name": "finwo/property-accessor", @@ -38,6 +38,45 @@ } ], "time": "2016-05-06 13:52:04" + }, + { + "name": "netresearch/jsonmapper", + "version": "v0.11.0", + "source": { + "type": "git", + "url": "https://github.com/cweiske/jsonmapper.git", + "reference": "979abda4b128415c642b06f07db615e75cfd3173" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/cweiske/jsonmapper/zipball/979abda4b128415c642b06f07db615e75cfd3173", + "reference": "979abda4b128415c642b06f07db615e75cfd3173", + "shasum": "" + }, + "require-dev": { + "phpunit/phpunit": "4.2.*", + "squizlabs/php_codesniffer": "~1.5" + }, + "type": "library", + "autoload": { + "psr-0": { + "JsonMapper": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "OSL-3.0" + ], + "authors": [ + { + "name": "Christian Weiske", + "email": "cweiske@cweiske.de", + "homepage": "http://github.com/cweiske/jsonmapper/", + "role": "Developer" + } + ], + "description": "Map nested JSON structures onto PHP classes", + "time": "2016-04-14 21:59:35" } ], "packages-dev": [ @@ -151,22 +190,22 @@ }, { "name": "phpdocumentor/reflection-docblock", - "version": "3.0.2", + "version": "3.1.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "45ada3e3fd09789fbfbd6d65b3f0901f0030dc61" + "reference": "9270140b940ff02e58ec577c237274e92cd40cdd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/45ada3e3fd09789fbfbd6d65b3f0901f0030dc61", - "reference": "45ada3e3fd09789fbfbd6d65b3f0901f0030dc61", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/9270140b940ff02e58ec577c237274e92cd40cdd", + "reference": "9270140b940ff02e58ec577c237274e92cd40cdd", "shasum": "" }, "require": { "php": ">=5.5", "phpdocumentor/reflection-common": "^1.0@dev", - "phpdocumentor/type-resolver": "^0.1.5", + "phpdocumentor/type-resolver": "^0.2.0", "webmozart/assert": "^1.0" }, "require-dev": { @@ -192,20 +231,20 @@ } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2016-06-06 06:44:13" + "time": "2016-06-10 09:48:41" }, { "name": "phpdocumentor/type-resolver", - "version": "0.1.8", + "version": "0.2", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "9891754231e55d42f0d16988ffb799af39f31a12" + "reference": "b39c7a5b194f9ed7bd0dd345c751007a41862443" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/9891754231e55d42f0d16988ffb799af39f31a12", - "reference": "9891754231e55d42f0d16988ffb799af39f31a12", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/b39c7a5b194f9ed7bd0dd345c751007a41862443", + "reference": "b39c7a5b194f9ed7bd0dd345c751007a41862443", "shasum": "" }, "require": { @@ -214,7 +253,7 @@ }, "require-dev": { "mockery/mockery": "^0.9.4", - "phpunit/phpunit": "^5.2" + "phpunit/phpunit": "^5.2||^4.8.24" }, "type": "library", "extra": { @@ -239,7 +278,7 @@ "email": "me@mikevanriel.com" } ], - "time": "2016-03-28 10:02:29" + "time": "2016-06-10 07:14:17" }, { "name": "phpspec/prophecy", @@ -1047,16 +1086,16 @@ }, { "name": "symfony/yaml", - "version": "v3.1.0", + "version": "v3.1.1", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "eca51b7b65eb9be6af88ad7cc91685f1556f5c9a" + "reference": "c5a7e7fc273c758b92b85dcb9c46149ccda89623" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/eca51b7b65eb9be6af88ad7cc91685f1556f5c9a", - "reference": "eca51b7b65eb9be6af88ad7cc91685f1556f5c9a", + "url": "https://api.github.com/repos/symfony/yaml/zipball/c5a7e7fc273c758b92b85dcb9c46149ccda89623", + "reference": "c5a7e7fc273c758b92b85dcb9c46149ccda89623", "shasum": "" }, "require": { @@ -1092,7 +1131,7 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2016-05-26 21:46:24" + "time": "2016-06-14 11:18:07" }, { "name": "webmozart/assert", diff --git a/src/UrlHandler.php b/src/UrlHandler.php @@ -0,0 +1,296 @@ +<?php + +namespace Finwo\Datatools; + +class UrlHandler +{ + /** + * @var string + */ + protected $scheme; + + /** + * @var string + */ + protected $user; + + /** + * @var string + */ + protected $password; + + /** + * @var string + */ + protected $host; + + /** + * @var integer + */ + protected $port; + + /** + * @var string + */ + protected $path; + + /** + * @var array + */ + protected $query = array(); + + /** + * @var string + */ + protected $fragment; + + /** + * UrlHandler constructor. + * + * @param $url + */ + public function __construct($url = null) + { + if (gettype($url) == 'string') { + $this->setUrl($url); + } + } + + /** + * @param $query + * + * @return $this + */ + public function setQuery( $query ) + { + $this->query = parse_str($query); + return $this; + } + + /** + * @param string $key + * @param mixed $value + * + * @return $this + */ + public function setQueryValue( $key, $value ) + { + $this->query[$key] = $value; + return $this; + } + + /** + * @return string + */ + public function getQuery() + { + return http_build_query($this->query); + } + + /** + * getUrl() + * Reconstructs the url + * + * @return string + */ + public function getUrl() + { + return (isset($this->scheme)? $this->scheme . '://' : '') . + (isset($this->user) ? $this->user : '') . + (isset($this->password) ? ':' . $this->password : '') . + (isset($this->user) ? '@' : '') . + (isset($this->host) ? $this->host : '') . + (isset($this->port) ? ':' . $this->port : '') . + (count($this->query) ? '?' . $this->getQuery() : '') . + (isset($this->fragment) ? '#' . $this->fragment : '') + ; + } + + public function setUrl( $url ) + { + $mapper = new \JsonMapper(); + $mapper->map(json_decode(json_encode($this->parseUrl($url))), $this); + } + + // See http://php.net/manual/en/function.parse-url.php#116456 + // Modified, but still based upon that + public function parseUrl( $url ) + { + // Init the output var in advance + $result = array(); + + // Fetch fragment + if(strpos($url,"#")>-1){ + $a=explode("#",$url,2); + $url=$a[0]; + $result['fragment']=$a[1]; + } + + // Fetch query + if(strpos($url,"?")>-1){ + $a=explode("?",$url,2); + $url=$a[0]; + $result['query']=$a[1]; + } + + // Fetch scheme + if(strpos($url,"://")>-1){ + $result['scheme']=substr($url,0,strpos($url,"//")-1); + $url=substr($url,strpos($url,"//")+2,strlen($url)); + } + + // Fetch path + if(strpos($url,"/")>-1){ + $a=explode("/",$url,2); + $url=$a[0]; + $result['path']="/".$a[1]; + } + + // Fetch port + if(strpos($url,":")>-1){ + $a=explode(":",$url,2); + $url=$a[0]; + $result['port']=$a[1]; + } + + // Only the host remains + if (strlen($url)) { + $result['host'] = $url; + } + + return $result; + } + + /** + * @return string + */ + public function getScheme() + { + return $this->scheme; + } + + /** + * @param string $scheme + * + * @return UrlHandler + */ + public function setScheme($scheme) + { + $this->scheme = $scheme; + return $this; + } + + /** + * @return string + */ + public function getUser() + { + return $this->user; + } + + /** + * @param string $user + * + * @return UrlHandler + */ + public function setUser($user) + { + $this->user = $user; + return $this; + } + + /** + * @return string + */ + public function getPassword() + { + return $this->password; + } + + /** + * @param string $password + * + * @return UrlHandler + */ + public function setPassword($password) + { + $this->password = $password; + return $this; + } + + /** + * @return string + */ + public function getHost() + { + return $this->host; + } + + /** + * @param string $host + * + * @return UrlHandler + */ + public function setHost($host) + { + $this->host = $host; + return $this; + } + + /** + * @return int + */ + public function getPort() + { + return $this->port; + } + + /** + * @param int $port + * + * @return UrlHandler + */ + public function setPort($port) + { + $this->port = $port; + return $this; + } + + /** + * @return string + */ + public function getPath() + { + return $this->path; + } + + /** + * @param string $path + * + * @return UrlHandler + */ + public function setPath($path) + { + $this->path = $path; + return $this; + } + + /** + * @return string + */ + public function getFragment() + { + return $this->fragment; + } + + /** + * @param string $fragment + * + * @return UrlHandler + */ + public function setFragment($fragment) + { + $this->fragment = $fragment; + return $this; + } +}