Skip to content

Commit 18d193d

Browse files
author
Dogan Ucar
committed
Base64 class
1 parent 6d20593 commit 18d193d

File tree

4 files changed

+126
-6
lines changed

4 files changed

+126
-6
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
},
1717
"require": {
1818
"doganoo/php-algorithms": "*",
19-
"apache/log4php": "2.3.0"
19+
"apache/log4php": "2.3.0",
20+
"ext-fileinfo": "*"
2021
},
2122
"require-dev": {
2223
"phpunit/phpunit": "6.5"

composer.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Crypto/Base64.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
namespace doganoo\PHPUtil\Crypto;
27+
28+
/**
29+
* Class Base64
30+
* @package doganoo\PHPUtil\Crypto
31+
*/
32+
class Base64 {
33+
34+
/** @var null|mixed $data */
35+
private $data = null;
36+
/** @var null|array $mimeType */
37+
private $mimeType = null;
38+
39+
/**
40+
* Base64 constructor.
41+
* @param null $data
42+
*/
43+
public function __construct($data = null) {
44+
$this->setData($data);
45+
}
46+
47+
/**
48+
* @param $data
49+
*/
50+
public function setData($data): void {
51+
$this->data = $data;
52+
}
53+
54+
/**
55+
* @return mixed|null
56+
*/
57+
public function getData() {
58+
return $this->data;
59+
}
60+
61+
/**
62+
* @return string
63+
*/
64+
public function encode(): string {
65+
$data = base64_encode($this->getData());
66+
$this->setData($data);
67+
return $this->getData();
68+
}
69+
70+
/**
71+
* @param bool $strict
72+
* @return mixed|null
73+
*/
74+
public function decode(bool $strict = true){
75+
$data = base64_decode($this->getData(), $strict);
76+
$this->setData($data);
77+
return $this->getData();
78+
}
79+
80+
/**
81+
* @param $data
82+
* @return bool
83+
*/
84+
public static function isBase64($data):bool {
85+
if (null === $data) return false;
86+
if (!is_string($data)) return false;
87+
88+
$decoded = base64_decode($data);
89+
$encoded = base64_encode($decoded);
90+
91+
return $data === $encoded;
92+
93+
}
94+
95+
/**
96+
* @return array|null
97+
*/
98+
public function getMimeType():?array {
99+
if (!Base64::isBase64($this->getData())) return null;
100+
101+
$decoded = $this->decode();
102+
$handle = finfo_open();
103+
104+
$mimeType = finfo_buffer(
105+
$handle
106+
, $decoded
107+
, FILEINFO_MIME_TYPE
108+
);
109+
110+
$this->mimeType = [$mimeType];
111+
112+
return $this->mimeType;
113+
}
114+
115+
}

src/Datatype/StringClass.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ public function __construct($value) {
5252
* @return bool
5353
*/
5454
public function equals($value) {
55-
if ($value instanceof PHPString) {
55+
if ($value instanceof StringClass) {
5656
$value = $value->getValue();
5757
}
58+
if (!is_string($value)) return false;
5859
return strcmp($this->value, $value) === 0;
5960
}
6061

@@ -83,10 +84,11 @@ public function setValue($value) {
8384
* @return bool
8485
*/
8586
public function equalsIgnoreCase($value) {
86-
if ($value instanceof PHPString) {
87+
if ($value instanceof StringClass) {
8788
$value = $value->getValue();
8889
}
89-
return strcasecmp($this->value, $value) === 0;
90+
if (!is_string($value)) return false;
91+
return strcasecmp($this->value, $value) === 0;
9092
}
9193

9294
/**

0 commit comments

Comments
 (0)