@@ -2,17 +2,17 @@ |
||
2 | 2 | |
3 | 3 | // Requiring composer autoloader (local or global) |
4 | 4 | foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) { |
5 | - if (file_exists($file) && !defined('COMPOSER_AUTOLOADER')) { |
|
6 | - define('COMPOSER_AUTOLOADER', $file); |
|
7 | - break; |
|
8 | - } |
|
5 | + if (file_exists($file) && !defined('COMPOSER_AUTOLOADER')) { |
|
6 | + define('COMPOSER_AUTOLOADER', $file); |
|
7 | + break; |
|
8 | + } |
|
9 | 9 | } |
10 | 10 | if (!defined('COMPOSER_AUTOLOADER')) { |
11 | - die( |
|
12 | - 'You need to set up the project dependencies using the following commands:' . PHP_EOL . |
|
13 | - 'php -r "readfile(\'https://getcomposer.org/installer\');" | php' . PHP_EOL . |
|
14 | - 'php composer.phar install' . PHP_EOL |
|
15 | - ); |
|
11 | + die( |
|
12 | + 'You need to set up the project dependencies using the following commands:' . PHP_EOL . |
|
13 | + 'php -r "readfile(\'https://getcomposer.org/installer\');" | php' . PHP_EOL . |
|
14 | + 'php composer.phar install' . PHP_EOL |
|
15 | + ); |
|
16 | 16 | } |
17 | 17 | require COMPOSER_AUTOLOADER; |
18 | 18 |
@@ -1,17 +1,17 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | // Requiring composer autoloader (local or global) |
4 | -foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) { |
|
5 | - if (file_exists($file) && !defined('COMPOSER_AUTOLOADER')) { |
|
4 | +foreach ([__DIR__.'/../../autoload.php', __DIR__.'/vendor/autoload.php'] as $file) { |
|
5 | + if (file_exists($file)&&!defined('COMPOSER_AUTOLOADER')) { |
|
6 | 6 | define('COMPOSER_AUTOLOADER', $file); |
7 | 7 | break; |
8 | 8 | } |
9 | 9 | } |
10 | 10 | if (!defined('COMPOSER_AUTOLOADER')) { |
11 | 11 | die( |
12 | - 'You need to set up the project dependencies using the following commands:' . PHP_EOL . |
|
13 | - 'php -r "readfile(\'https://getcomposer.org/installer\');" | php' . PHP_EOL . |
|
14 | - 'php composer.phar install' . PHP_EOL |
|
12 | + 'You need to set up the project dependencies using the following commands:'.PHP_EOL. |
|
13 | + 'php -r "readfile(\'https://getcomposer.org/installer\');" | php'.PHP_EOL. |
|
14 | + 'php composer.phar install'.PHP_EOL |
|
15 | 15 | ); |
16 | 16 | } |
17 | 17 | require COMPOSER_AUTOLOADER; |
@@ -19,4 +19,4 @@ discard block |
||
19 | 19 | |
20 | 20 | $url = 'http://www.amazon.co.uk/dp/3836227622/ref=wl_it_dp_v_S_ttl/275-8449783-2161748?_encoding=UTF8&colid=3PNTY4VFL6H2Q&coliid=I1MY7ZKTP1IFRJ'; |
21 | 21 | $fetcher = new \Amazon\AsinParser($url); |
22 | -echo $fetcher->getAsin() . PHP_EOL; |
|
22 | +echo $fetcher->getAsin().PHP_EOL; |
@@ -2,17 +2,17 @@ |
||
2 | 2 | //@deprecated |
3 | 3 | // Requiring composer autoloader (local or global) |
4 | 4 | foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) { |
5 | - if (file_exists($file) && !defined('COMPOSER_AUTOLOADER')) { |
|
6 | - define('COMPOSER_AUTOLOADER', $file); |
|
7 | - break; |
|
8 | - } |
|
5 | + if (file_exists($file) && !defined('COMPOSER_AUTOLOADER')) { |
|
6 | + define('COMPOSER_AUTOLOADER', $file); |
|
7 | + break; |
|
8 | + } |
|
9 | 9 | } |
10 | 10 | if (!defined('COMPOSER_AUTOLOADER')) { |
11 | - die( |
|
12 | - 'You need to set up the project dependencies using the following commands:' . PHP_EOL . |
|
13 | - 'php -r "readfile(\'https://getcomposer.org/installer\');" | php' . PHP_EOL . |
|
14 | - 'php composer.phar install' . PHP_EOL |
|
15 | - ); |
|
11 | + die( |
|
12 | + 'You need to set up the project dependencies using the following commands:' . PHP_EOL . |
|
13 | + 'php -r "readfile(\'https://getcomposer.org/installer\');" | php' . PHP_EOL . |
|
14 | + 'php composer.phar install' . PHP_EOL |
|
15 | + ); |
|
16 | 16 | } |
17 | 17 | require COMPOSER_AUTOLOADER; |
18 | 18 |
@@ -1,17 +1,17 @@ |
||
1 | 1 | <?php |
2 | 2 | //@deprecated |
3 | 3 | // Requiring composer autoloader (local or global) |
4 | -foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) { |
|
5 | - if (file_exists($file) && !defined('COMPOSER_AUTOLOADER')) { |
|
4 | +foreach ([__DIR__.'/../../autoload.php', __DIR__.'/vendor/autoload.php'] as $file) { |
|
5 | + if (file_exists($file)&&!defined('COMPOSER_AUTOLOADER')) { |
|
6 | 6 | define('COMPOSER_AUTOLOADER', $file); |
7 | 7 | break; |
8 | 8 | } |
9 | 9 | } |
10 | 10 | if (!defined('COMPOSER_AUTOLOADER')) { |
11 | 11 | die( |
12 | - 'You need to set up the project dependencies using the following commands:' . PHP_EOL . |
|
13 | - 'php -r "readfile(\'https://getcomposer.org/installer\');" | php' . PHP_EOL . |
|
14 | - 'php composer.phar install' . PHP_EOL |
|
12 | + 'You need to set up the project dependencies using the following commands:'.PHP_EOL. |
|
13 | + 'php -r "readfile(\'https://getcomposer.org/installer\');" | php'.PHP_EOL. |
|
14 | + 'php composer.phar install'.PHP_EOL |
|
15 | 15 | ); |
16 | 16 | } |
17 | 17 | require COMPOSER_AUTOLOADER; |
@@ -11,120 +11,120 @@ |
||
11 | 11 | |
12 | 12 | abstract class Parser |
13 | 13 | { |
14 | - /** |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - private $tld = null; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var array |
|
21 | - */ |
|
22 | - private $unusedDomainParts = array( |
|
23 | - 'amazon' |
|
24 | - , 'amzn' |
|
25 | - , 'www' |
|
26 | - ); |
|
27 | - |
|
28 | - /** |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - private $amazonDomains = array( |
|
32 | - 'amazon' |
|
33 | - , 'amzn' |
|
34 | - ); |
|
35 | - |
|
36 | - /** |
|
37 | - * @var string |
|
38 | - */ |
|
39 | - protected $url = null; |
|
40 | - |
|
41 | - public function __construct($url) |
|
42 | - { |
|
43 | - |
|
44 | - $this->url = $url; |
|
45 | - //Question Marks |
|
46 | - if (preg_match('/%3F/', $url)) { |
|
47 | - $this->url = substr($url, 0, strpos($url, "%3F")); |
|
48 | - |
|
49 | - } |
|
50 | - |
|
51 | - $urlParameter = parse_url($this->getUrl()); |
|
52 | - $this->processUrl($urlParameter); |
|
53 | - $this->processTld($urlParameter['host']); |
|
54 | - |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $url |
|
59 | - */ |
|
60 | - protected function setUrl($url) |
|
61 | - { |
|
62 | - $this->url = $url; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - protected function getUrl() |
|
69 | - { |
|
70 | - return $this->url; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @param $urlParameter |
|
75 | - * @throws InvalidDomainException |
|
76 | - */ |
|
77 | - protected function processUrl($urlParameter) |
|
78 | - { |
|
79 | - if (false === array_key_exists('path', $urlParameter)) { |
|
80 | - throw new InvalidDomainException(sprintf('Url %s has no path', $this->getUrl())); |
|
81 | - } |
|
82 | - |
|
83 | - //Check if we have an Amazon Domain |
|
84 | - $isAmazonDomain = false; |
|
85 | - //ShortUrl |
|
86 | - foreach ($this->amazonDomains as $domain) { |
|
87 | - if (preg_match('/' . $domain . '/', $this->getUrl())) { |
|
88 | - $isAmazonDomain = true; |
|
89 | - } |
|
90 | - } |
|
91 | - if (false === $isAmazonDomain) { |
|
92 | - throw new InvalidDomainException(sprintf('Url %s does not belong to Amazon', $this->getUrl())); |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param $host |
|
98 | - * @return string |
|
99 | - */ |
|
100 | - protected function processTld($host) |
|
101 | - { |
|
102 | - $tldStrings = []; |
|
103 | - $parts = explode('.', $host); |
|
104 | - foreach ($parts as $part) { |
|
105 | - |
|
106 | - if (false === in_array($part, $this->unusedDomainParts)) { |
|
107 | - $tldStrings[] = $part; |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - $this->setTld(implode('.', $tldStrings)); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public function getTld() |
|
118 | - { |
|
119 | - return $this->tld; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @param string $tld |
|
124 | - */ |
|
125 | - protected function setTld($tld) |
|
126 | - { |
|
127 | - $this->tld = $tld; |
|
128 | - } |
|
14 | + /** |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + private $tld = null; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var array |
|
21 | + */ |
|
22 | + private $unusedDomainParts = array( |
|
23 | + 'amazon' |
|
24 | + , 'amzn' |
|
25 | + , 'www' |
|
26 | + ); |
|
27 | + |
|
28 | + /** |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + private $amazonDomains = array( |
|
32 | + 'amazon' |
|
33 | + , 'amzn' |
|
34 | + ); |
|
35 | + |
|
36 | + /** |
|
37 | + * @var string |
|
38 | + */ |
|
39 | + protected $url = null; |
|
40 | + |
|
41 | + public function __construct($url) |
|
42 | + { |
|
43 | + |
|
44 | + $this->url = $url; |
|
45 | + //Question Marks |
|
46 | + if (preg_match('/%3F/', $url)) { |
|
47 | + $this->url = substr($url, 0, strpos($url, "%3F")); |
|
48 | + |
|
49 | + } |
|
50 | + |
|
51 | + $urlParameter = parse_url($this->getUrl()); |
|
52 | + $this->processUrl($urlParameter); |
|
53 | + $this->processTld($urlParameter['host']); |
|
54 | + |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $url |
|
59 | + */ |
|
60 | + protected function setUrl($url) |
|
61 | + { |
|
62 | + $this->url = $url; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + protected function getUrl() |
|
69 | + { |
|
70 | + return $this->url; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @param $urlParameter |
|
75 | + * @throws InvalidDomainException |
|
76 | + */ |
|
77 | + protected function processUrl($urlParameter) |
|
78 | + { |
|
79 | + if (false === array_key_exists('path', $urlParameter)) { |
|
80 | + throw new InvalidDomainException(sprintf('Url %s has no path', $this->getUrl())); |
|
81 | + } |
|
82 | + |
|
83 | + //Check if we have an Amazon Domain |
|
84 | + $isAmazonDomain = false; |
|
85 | + //ShortUrl |
|
86 | + foreach ($this->amazonDomains as $domain) { |
|
87 | + if (preg_match('/' . $domain . '/', $this->getUrl())) { |
|
88 | + $isAmazonDomain = true; |
|
89 | + } |
|
90 | + } |
|
91 | + if (false === $isAmazonDomain) { |
|
92 | + throw new InvalidDomainException(sprintf('Url %s does not belong to Amazon', $this->getUrl())); |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param $host |
|
98 | + * @return string |
|
99 | + */ |
|
100 | + protected function processTld($host) |
|
101 | + { |
|
102 | + $tldStrings = []; |
|
103 | + $parts = explode('.', $host); |
|
104 | + foreach ($parts as $part) { |
|
105 | + |
|
106 | + if (false === in_array($part, $this->unusedDomainParts)) { |
|
107 | + $tldStrings[] = $part; |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + $this->setTld(implode('.', $tldStrings)); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public function getTld() |
|
118 | + { |
|
119 | + return $this->tld; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @param string $tld |
|
124 | + */ |
|
125 | + protected function setTld($tld) |
|
126 | + { |
|
127 | + $this->tld = $tld; |
|
128 | + } |
|
129 | 129 | |
130 | 130 | } |
131 | 131 | \ No newline at end of file |
@@ -84,7 +84,7 @@ |
||
84 | 84 | $isAmazonDomain = false; |
85 | 85 | //ShortUrl |
86 | 86 | foreach ($this->amazonDomains as $domain) { |
87 | - if (preg_match('/' . $domain . '/', $this->getUrl())) { |
|
87 | + if (preg_match('/'.$domain.'/', $this->getUrl())) { |
|
88 | 88 | $isAmazonDomain = true; |
89 | 89 | } |
90 | 90 | } |
@@ -14,34 +14,34 @@ |
||
14 | 14 | */ |
15 | 15 | class WishlistParser extends Parser |
16 | 16 | { |
17 | - /** |
|
18 | - * @var null |
|
19 | - */ |
|
20 | - private $wishlistId = null; |
|
21 | - |
|
22 | - public function __construct($url) |
|
23 | - { |
|
24 | - parent::__construct($url); |
|
25 | - $urlParameter = parse_url($this->getUrl()); |
|
26 | - |
|
27 | - $this->processWishListUrl($urlParameter); |
|
28 | - } |
|
29 | - |
|
30 | - protected function processWishListUrl($urlParameter) |
|
31 | - { |
|
32 | - $paramArray = explode('/', $urlParameter['path']); |
|
33 | - |
|
34 | - $this->wishlistId = $paramArray[4]; |
|
35 | - |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * @return null |
|
40 | - */ |
|
41 | - public function getWishlistId() |
|
42 | - { |
|
43 | - return $this->wishlistId; |
|
44 | - } |
|
17 | + /** |
|
18 | + * @var null |
|
19 | + */ |
|
20 | + private $wishlistId = null; |
|
21 | + |
|
22 | + public function __construct($url) |
|
23 | + { |
|
24 | + parent::__construct($url); |
|
25 | + $urlParameter = parse_url($this->getUrl()); |
|
26 | + |
|
27 | + $this->processWishListUrl($urlParameter); |
|
28 | + } |
|
29 | + |
|
30 | + protected function processWishListUrl($urlParameter) |
|
31 | + { |
|
32 | + $paramArray = explode('/', $urlParameter['path']); |
|
33 | + |
|
34 | + $this->wishlistId = $paramArray[4]; |
|
35 | + |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * @return null |
|
40 | + */ |
|
41 | + public function getWishlistId() |
|
42 | + { |
|
43 | + return $this->wishlistId; |
|
44 | + } |
|
45 | 45 | |
46 | 46 | |
47 | 47 | } |
48 | 48 | \ No newline at end of file |
@@ -12,76 +12,76 @@ |
||
12 | 12 | class AsinParser extends Parser |
13 | 13 | { |
14 | 14 | |
15 | - const LENGTH_ASIN = 10; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - private $asin = null; |
|
21 | - |
|
22 | - public function __construct($url) |
|
23 | - { |
|
24 | - parent::__construct($url); |
|
25 | - $urlParameter = parse_url($this->getUrl()); |
|
26 | - |
|
27 | - $this->processAsin($urlParameter['host'], $urlParameter['path']); |
|
28 | - } |
|
29 | - |
|
30 | - protected function processAsin($host, $path) |
|
31 | - { |
|
32 | - $path = str_replace('/product', '', $path); |
|
33 | - $pathParts = explode('/', $path); |
|
34 | - |
|
35 | - //ShortUrl |
|
36 | - if (preg_match('/amzn/', $host)) { |
|
37 | - $this->setAsin(end($pathParts)); |
|
38 | - return; |
|
39 | - } |
|
40 | - |
|
41 | - if (false === array_key_exists(2, $pathParts)) { |
|
42 | - throw new InvalidAsinException(sprintf('Url %s has no ASIN', $this->getUrl())); |
|
43 | - } |
|
44 | - |
|
45 | - foreach ($pathParts as $index => $part) { |
|
46 | - if (strlen($part) === self::LENGTH_ASIN) { |
|
47 | - if (array_key_exists($index - 1, $pathParts) && $pathParts[$index - 1] === 'dp') { |
|
48 | - $this->setAsin($part); |
|
49 | - return; |
|
50 | - } |
|
51 | - |
|
52 | - } |
|
53 | - } |
|
54 | - |
|
55 | - if (strlen($pathParts[2]) !== self::LENGTH_ASIN) { |
|
56 | - throw new InvalidAsinException(sprintf('Url %s has no valid ASIN', $this->getUrl())); |
|
57 | - |
|
58 | - } |
|
59 | - //@todo length of ASIN! |
|
60 | - $this->setAsin($pathParts[2]); |
|
61 | - return; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - public function getAsin() |
|
68 | - { |
|
69 | - return $this->asin; |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * @param string $asin |
|
74 | - */ |
|
75 | - protected function setAsin($asin) |
|
76 | - { |
|
77 | - $this->asin = $asin; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @return string |
|
82 | - */ |
|
83 | - public function getCleanedUrl() |
|
84 | - { |
|
85 | - return sprintf('http://www.amazon.%s/dp/%s/', $this->getTld(), $this->getAsin()); |
|
86 | - } |
|
15 | + const LENGTH_ASIN = 10; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + private $asin = null; |
|
21 | + |
|
22 | + public function __construct($url) |
|
23 | + { |
|
24 | + parent::__construct($url); |
|
25 | + $urlParameter = parse_url($this->getUrl()); |
|
26 | + |
|
27 | + $this->processAsin($urlParameter['host'], $urlParameter['path']); |
|
28 | + } |
|
29 | + |
|
30 | + protected function processAsin($host, $path) |
|
31 | + { |
|
32 | + $path = str_replace('/product', '', $path); |
|
33 | + $pathParts = explode('/', $path); |
|
34 | + |
|
35 | + //ShortUrl |
|
36 | + if (preg_match('/amzn/', $host)) { |
|
37 | + $this->setAsin(end($pathParts)); |
|
38 | + return; |
|
39 | + } |
|
40 | + |
|
41 | + if (false === array_key_exists(2, $pathParts)) { |
|
42 | + throw new InvalidAsinException(sprintf('Url %s has no ASIN', $this->getUrl())); |
|
43 | + } |
|
44 | + |
|
45 | + foreach ($pathParts as $index => $part) { |
|
46 | + if (strlen($part) === self::LENGTH_ASIN) { |
|
47 | + if (array_key_exists($index - 1, $pathParts) && $pathParts[$index - 1] === 'dp') { |
|
48 | + $this->setAsin($part); |
|
49 | + return; |
|
50 | + } |
|
51 | + |
|
52 | + } |
|
53 | + } |
|
54 | + |
|
55 | + if (strlen($pathParts[2]) !== self::LENGTH_ASIN) { |
|
56 | + throw new InvalidAsinException(sprintf('Url %s has no valid ASIN', $this->getUrl())); |
|
57 | + |
|
58 | + } |
|
59 | + //@todo length of ASIN! |
|
60 | + $this->setAsin($pathParts[2]); |
|
61 | + return; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + public function getAsin() |
|
68 | + { |
|
69 | + return $this->asin; |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * @param string $asin |
|
74 | + */ |
|
75 | + protected function setAsin($asin) |
|
76 | + { |
|
77 | + $this->asin = $asin; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @return string |
|
82 | + */ |
|
83 | + public function getCleanedUrl() |
|
84 | + { |
|
85 | + return sprintf('http://www.amazon.%s/dp/%s/', $this->getTld(), $this->getAsin()); |
|
86 | + } |
|
87 | 87 | } |
88 | 88 | \ No newline at end of file |
@@ -44,7 +44,7 @@ |
||
44 | 44 | |
45 | 45 | foreach ($pathParts as $index => $part) { |
46 | 46 | if (strlen($part) === self::LENGTH_ASIN) { |
47 | - if (array_key_exists($index - 1, $pathParts) && $pathParts[$index - 1] === 'dp') { |
|
47 | + if (array_key_exists($index - 1, $pathParts)&&$pathParts[$index - 1] === 'dp') { |
|
48 | 48 | $this->setAsin($part); |
49 | 49 | return; |
50 | 50 | } |