DocumentHelper::findWordsStartAt()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2020 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\ThirdParty\OcrLad\Helper;
13
14
use WBW\Library\Core\Geometry\Point;
15
use WBW\Library\Core\ThirdParty\OcrLad\Model\Document;
16
use WBW\Library\Core\ThirdParty\OcrLad\Model\Word;
17
18
/**
19
 * Document helper.
20
 *
21
 * @author webeweb <https://github.com/webeweb>
22
 * @package WBW\Library\Core\ThirdParty\OcrLad\Helper
23
 */
24
class DocumentHelper {
25
26
    /**
27
     * Document.
28
     *
29
     * @var Document
30
     */
31
    private $document;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param Document $document The document.
37
     */
38
    public function __construct(Document $document) {
39
        $this->setDocument($document);
40
    }
41
42
    /**
43
     * Find a word.
44
     *
45
     * @param Point $position The position.
46
     * @param int|null $page The page.
47
     * @return Word Returns the word in case of success, null otherwise.
48
     */
49
    public function findWordAt(Point $position, int $page = null): ?Word {
50
51
        foreach ($this->getWords($page) as $current) {
52
53
            $dX = $position->getXInt() === $current->getPoint1()->getXInt();
54
            $dY = $position->getYInt() === $current->getPoint1()->getYInt();
55
56
            if (true === $dX && true === $dY) {
57
                return $current;
58
            }
59
        }
60
61
        return null;
62
    }
63
64
    /**
65
     * Find the words.
66
     *
67
     * @param Point $start The start position.
68
     * @param Point $end The end position.
69
     * @param int|null $page The page.
70
     * @return Word[] Returns the words.
71
     */
72
    public function findWordsIn(Point $start, Point $end, int $page = null): array {
73
74
        $matches = [];
75
76
        foreach ($this->getWords($page) as $current) {
77
78
            $dX1 = $start->getXInt() <= $current->getPoint1()->getXInt();
79
            $dY1 = $start->getYInt() <= $current->getPoint1()->getYInt();
80
81
            $dX2 = $current->getPoint2()->getXInt() <= $end->getXInt();
82
            $dY2 = $current->getPoint2()->getYInt() <= $end->getYInt();
83
84
            if (true === $dX1 && true === $dY1 && true === $dX2 && true === $dY2) {
85
                $matches[] = $current;
86
            }
87
        }
88
89
        return $matches;
90
    }
91
92
    /**
93
     * Find the words.
94
     *
95
     * @param Point $start The start position.
96
     * @param int|null $page The page.
97
     * @return Word[] Returns the words.
98
     */
99
    public function findWordsStartAt(Point $start, int $page = null): array {
100
101
        $matches = [];
102
103
        foreach ($this->getWords($page) as $current) {
104
105
            $dX = $start->getXInt() <= $current->getPoint1()->getXInt();
106
            $dY = $start->getYInt() === $current->getPoint1()->getYInt();
107
108
            if (true === $dX && true === $dY) {
109
                $matches[] = $current;
110
            }
111
        }
112
113
        return $matches;
114
    }
115
116
    /**
117
     * Get the document.
118
     *
119
     * @return Document Returns the document.
120
     */
121
    public function getDocument(): Document {
122
        return $this->document;
123
    }
124
125
    /**
126
     * Get the words.
127
     *
128
     * @param int|null $p The page.
129
     * @return Word[] Returns the words.
130
     */
131
    protected function getWords(?int $p): array {
132
133
        $page = $this->getDocument()->getPage($p);
134
        if (null !== $page) {
135
            return $page->getWords();
136
        }
137
138
        return $this->getDocument()->getWords();
139
    }
140
141
    /**
142
     * Set the document.
143
     *
144
     * @param Document $document The document.
145
     * @return DocumentHelper Returns this document helper.
146
     */
147
    protected function setDocument(Document $document): DocumentHelper {
148
        $this->document = $document;
149
        return $this;
150
    }
151
}