summaryrefslogtreecommitdiff
path: root/src/Etym.php
blob: b61c2d0c9af8a65b566205d353da512fcda99661 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

namespace App;

use App\Curl;
use PHPHtmlParser\Dom;

/**
 * Get the etymology of a word
 *
 * @author Phil Burton <phil@pgburton.com>
 */
class Etym
{
    protected $baseURL;
    protected $domSearch;

    /**
     * Initalise with config options
     *
     * @param string $baseURL
     * @param string $domSearch
     * @param string $pasteCmd
     * @author Phil Burton <phil@pgburton.com>
     */
    public function __construct(string $baseURL, string $domSearch)
    {
        $this->baseURL = $baseURL;
        $this->domSearch = $domSearch;
    }

    /**
     * Search for and return etym defeiniton
     *
     * @return string
     * @author Phil Burton <phil@pgburton.com>
     */
    public function getDefinition(): string
    {
        $input = $this->readStdin();

        $targetURL = $this->baseURL . $input;

        $dom = new Dom;
        $dom->loadFromUrl($targetURL, [], new Curl);
        $html = $dom->find($this->domSearch);

        if (!$html instanceof \PHPHtmlParser\Dom\Collection || count($html) <= 0) {
            echo "No entry found for '$input'!\n";
            exit(2);
        }

        $definition = $this->cleanUpHtml($html[0]->innerHtml());

        // echo $definition . "\n";
        return $definition;
    }

    /**
     * Get and return the user input
     *
     * @return string
     * @author Phil Burton <phil@pgburton.com>
     */
    protected function readStdin(): string
    {
        $input = fgets(STDIN);

        if ($input === false) {
            echo "No input supplied!\n";
            exit(1);
        }

        $input = rtrim($input, "\n");

        return $input;
    }

    /**
     * Tidy and return the hmtl
     *
     * @param string $input
     * @return string
     * @author Phil Burton <phil@pgburton.com>
     */
    protected function cleanUpHtml(string $input): string
    {
        // Strip HTML Tags
        $clear = strip_tags($input);
        // Clean up things like &amp;
        $clear = html_entity_decode($clear);
        // Strip out any url-encoded stuff
        $clear = urldecode($clear);
        // Replace Multiple spaces with single space
        $clear = preg_replace('/ +/', ' ', $clear);
        // Trim the string of leading/trailing space
        $clear = trim($clear);
        // Capitalise the first char.
        $clear = ucfirst($clear);

        return $clear;
    }
}