Passed
Push — master ( 658154...a7692c )
by Vítězslav
10:03
created

Sand::baseClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Zakladni objekt urceny k rodicovstvi pokročilým objektum.
4
 *
5
 * @author    Vitex <[email protected]>
6
 * @copyright 2009-2019 [email protected] (G)
7
 */
8
9
namespace Ease;
10
11
/**
12
 * Zakladni objekt urceny k rodicovstvi vsem pouzivanym objektum.
13
 *
14
 * @author    Vitex <[email protected]>
15
 * @copyright 2009-2012 [email protected] (G)
16
 */
17
class Sand extends Molecule
18
{
19
    /**
20
     * Default Language Code.
21
     *
22
     * @var string
23
     */
24
    public $langCode = null;
25
26
    /**
27
     * Common object data holder.
28
     *
29
     * @var array
30
     */
31
    public $data = null;
32
33
    /**
34
     * Obsahuje všechna pole souhrně považovaná za identitu. Toto pole je plněno
35
     * v metodě SaveObjectIdentity {volá se automaticky v EaseSand::__construct()}.
36
     *
37
     * @var array
38
     */
39
    public $identity = [];
40
41
    /**
42
     * Původní identita sloužící jako záloha k zrekonstruování počátečního stavu objektu.
43
     *
44
     * @var array
45
     */
46
    public $initialIdenty = [];
47
48
    /**
49
     * Tyto sloupecky jsou uchovavany pri operacich s identitou objektu.
50
     *
51
     * @var array
52
     */
53
    public $identityColumns = ['ObjectName',
54
        'keyColumn',
55
        'myTable',
56
        'MyIDSColumn',
57
        'MyRefIDColumn',
58
        'myCreateColumn',
59
        'myLastModifiedColumn',];
60
61
    /**
62
     * Key Column for Current Record
63
     *
64
     * @var string
65
     */
66
    public $keyColumn = 'id';
67
68
    /**
69
     * Synchronizační sloupeček. napr products_ids.
70
     *
71
     * @var string
72
     */
73
    public $myIDSColumn = null;
74
75
    /**
76
     * Sloupeček obsahující datum vložení záznamu do shopu.
77
     *
78
     * @var string
79
     */
80
    public $myCreateColumn = null;
81
82
    /**
83
     * Slopecek obsahujici datum poslení modifikace záznamu do shopu.
84
     *
85
     * @var string
86
     */
87
    public $myLastModifiedColumn = null;
88
89
    /**
90
     * Objekt pro logování.
91
     *
92
     * @var Logger\Regent
93
     */
94
    public $logger = null;
95
96
    /**
97
     * Odkaz na vlastnící objekt.
98
     *
99
     * @var Sand|mixed object
100
     */
101
    public $parentObject = null;
102
103
    /**
104
     * Sdílený objekt frameworku.
105
     *
106
     * @var Shared
107
     */
108
    public $easeShared = null;
109
110
    /**
111
     * Prapředek všech objektů.
112
     */
113 1
    public function __construct()
114
    {
115 1
        parent::__construct();
116 1
        $this->initialIdenty = $this->saveObjectIdentity();
117 1
    }
118
119
    /**
120
     * Předá zprávy.
121
     *
122
     * @param bool $clean smazat originalni data ?
123
     *
124
     * @return array
125
     */
126
    public function getStatusMessages($clean = false)
127
    {
128
        $messages = array_merge($this->statusMessages,
129
            $this->logger->statusMessages,
130
            Shared::logger()->getStatusMessages(),
131
            Shared::instanced()->getStatusMessages());
132
        if ($clean) {
133
            $this->cleanMessages();
134
        }
135
136
        return $messages;
137
    }
138
139
    /**
140
     * Vymaže zprávy.
141
     */
142
    public function cleanMessages()
143
    {
144
        parent::cleanMessages();
145
        return Shared::instanced()->cleanMessages();
0 ignored issues
show
Bug introduced by
Are you sure the usage of Ease\Shared::instanced()->cleanMessages() targeting Ease\Atom::cleanMessages() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
146
    }
147
148
    /**
149
     * Nastaví novou identitu objektu.
150
     *
151
     * @param array $newIdentity
152
     */
153
    public function setObjectIdentity($newIdentity)
154
    {
155
        $changes = 0;
156
        $this->saveObjectIdentity();
157
        foreach ($this->identityColumns as $column) {
158
            if (isset($newIdentity[$column])) {
159
                $this->$column = $newIdentity[$column];
160
                ++$changes;
161
            }
162
        }
163
164
        return $changes;
165
    }
166
167
    /**
168
     * Uloží identitu objektu do pole $this->identity.
169
     *
170
     * @return array pole s identitou
171
     */
172 1
    public function saveObjectIdentity()
173
    {
174 1
        foreach ($this->identityColumns as $column) {
175 1
            if (isset($this->$column)) {
176 1
                $this->identity[$column] = $this->$column;
177
            }
178
        }
179
180 1
        return $this->identity;
181
    }
182
183
    /**
184
     * Obnoví uloženou identitu objektu.
185
     *
186
     * @param array $identity pole s identitou např. array('myTable'=>'user');
187
     */
188
    public function restoreObjectIdentity($identity = null)
189
    {
190
        if (is_null($identity)) {
191
            foreach ($this->identityColumns as $column) {
192
                if (isset($this->identity[$column])) {
193
                    $this->$column = $this->identity[$column];
194
                }
195
            }
196
        } else {
197
            foreach ($identity as $column) {
198
                if (isset($this->identity[$column])) {
199
                    $this->$column = $this->identity[$column];
200
                }
201
            }
202
        }
203
    }
204
205
    /**
206
     * Obnoví poslední použitou identitu.
207
     */
208
    public function resetObjectIdentity()
209
    {
210
        $this->identity = $this->initialIdenty;
211
        $this->restoreObjectIdentity();
212
    }
213
214
    /**
215
     * Z datového pole $SourceArray přemístí políčko $ColumName do pole
216
     * $destinationArray.
217
     *
218
     * @param array  $sourceArray      zdrojové pole dat
219
     * @param array  $destinationArray cílové pole dat
220
     * @param string $columName        název položky k převzetí
221
     */
222
    public static function divDataArray(&$sourceArray, &$destinationArray,
223
                                        $columName)
224
    {
225
        $result = false;
226
        if (array_key_exists($columName, $sourceArray)) {
227
            $destinationArray[$columName] = $sourceArray[$columName];
228
            unset($sourceArray[$columName]);
229
230
            $result = true;
231
        }
232
233
        return $result;
234
    }
235
236
    /**
237
     * Test for associative array.
238
     *
239
     * @param array $arr
240
     *
241
     * @return bool
242
     */
243
    public static function isAssoc(array $arr)
244
    {
245
        return array_keys($arr) !== range(0, count($arr) - 1);
246
    }
247
248
    /**
249
     * Gives you value of KEY Column
250
     *
251
     * @param array $data data z nichž se vrací hodnota klíče
252
     *
253
     * @return int key column value
254
     */
255 1
    public function getMyKey($data = null)
256
    {
257 1
        $key = null;
258 1
        if (is_null($data)) {
259 1
            $data = $this->getData();
260
        }
261 1
        if (isset($data) && isset($data[$this->keyColumn])) {
262
            $key = $data[$this->keyColumn];
263
        }
264
265 1
        return $key;
266
    }
267
268
    /**
269
     * Nastavuje hodnotu klíčového políčka pro SQL.
270
     *
271
     * @param int|string $myKeyValue
272
     *
273
     * @return bool
274
     */
275
    public function setMyKey($myKeyValue)
276
    {
277
        if (isset($this->keyColumn)) {
278
            $this->setDataValue($this->keyColumn, $myKeyValue);
279
            $result = true;
280
        } else {
281
            $result = false;
282
        }
283
284
        return $result;
285
    }
286
287
    /**
288
     * Gives you Current KeyColumn Name
289
     *
290
     * @return string
291
     */
292
    public function getKeyColumn()
293
    {
294
        return $this->keyColumn;
295
    }
296
297
    /**
298
     * Nastaví jméno klíčového sloupečku v shopu.
299
     *
300
     * @param string $keyColumn
301
     */
302
    public function setkeyColumn($keyColumn)
303
    {
304
        $this->keyColumn = $keyColumn;
305
    }
306
307
    /**
308
     * Vynuluje všechny pole vlastností objektu.
309
     */
310
    public function dataReset()
311
    {
312
        $this->data = [];
313
    }
314
315
    /**
316
     * Načte $data do polí objektu.
317
     *
318
     * @param array $data  asociativní pole dat
319
     * @param bool  $reset vyprazdnit pole před naplněním ?
320
     *
321
     * @return int počet načtených položek
322
     */
323
    public function setData($data, $reset = false)
324
    {
325
        $ret = null;
326
        if (!empty($data) && is_array($data) && count($data)) {
327
            if ($reset) {
328
                $this->dataReset();
329
            }
330
            if (is_array($this->data)) {
0 ignored issues
show
introduced by
The condition is_array($this->data) is always true.
Loading history...
331
                $this->data = array_merge($this->data, $data);
332
            } else {
333
                $this->data = $data;
334
            }
335
            $ret = count($data);
336
        }
337
338
        return $ret;
339
    }
340
341
    /**
342
     * Vrací celé pole dat objektu.
343
     *
344
     * @return array
345
     */
346 1
    public function getData()
347
    {
348 1
        return $this->data;
349
    }
350
351
    /**
352
     * Vrací počet položek dat objektu.
353
     *
354
     * @return int
355
     */
356
    public function getDataCount()
357
    {
358
        return count($this->data);
359
    }
360
361
    /**
362
     * Vrací hodnotu z pole dat pro MySQL.
363
     *
364
     * @param string $columnName název hodnoty/sloupečku
365
     *
366
     * @return mixed
367
     */
368
    public function getDataValue($columnName)
369
    {
370
        if (isset($this->data[$columnName])) {
371
            return $this->data[$columnName];
372
        }
373
374
        return;
375
    }
376
377
    /**
378
     * Nastaví hodnotu poli objektu.
379
     *
380
     * @param string $columnName název datové kolonky
381
     * @param mixed  $value      hodnota dat
382
     *
383
     * @return bool Success
384
     */
385
    public function setDataValue($columnName, $value)
386
    {
387
        $this->data[$columnName] = $value;
388
389
        return true;
390
    }
391
392
    /**
393
     * Odstrani polozku z pole dat pro MySQL.
394
     *
395
     * @param string $columnName název klíče k vymazání
396
     *
397
     * @return bool success
398
     */
399
    public function unsetDataValue($columnName)
400
    {
401
        if (array_key_exists($columnName, $this->data)) {
402
            unset($this->data[$columnName]);
403
404
            return true;
405
        }
406
407
        return false;
408
    }
409
410
    /**
411
     * Převezme data do aktuálního pole dat.
412
     *
413
     * @param array $data asociativní pole dat
414
     *
415
     * @return int
416
     */
417
    public function takeData($data)
418
    {
419
        if (is_array($this->data) && is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
420
            $this->data = array_merge($this->data, $data);
421
        } else {
422
            $this->data = $data;
423
        }
424
425
        return empty($data) ? null : count($data);
426
    }
427
428
    /**
429
     * Odstraní z textu diakritiku.
430
     *
431
     * @param string $text
432
     */
433
    public static function rip($text)
434
    {
435
        $convertTable = [
436
            'ä' => 'a',
437
            'Ä' => 'A',
438
            'á' => 'a',
439
            'Á' => 'A',
440
            'à' => 'a',
441
            'À' => 'A',
442
            'ã' => 'a',
443
            'Ã' => 'A',
444
            'â' => 'a',
445
            'Â' => 'A',
446
            'č' => 'c',
447
            'Č' => 'C',
448
            'ć' => 'c',
449
            'Ć' => 'C',
450
            'ď' => 'd',
451
            'Ď' => 'D',
452
            'ě' => 'e',
453
            'Ě' => 'E',
454
            'é' => 'e',
455
            'É' => 'E',
456
            'ë' => 'e',
457
            'Ë' => 'E',
458
            'è' => 'e',
459
            'È' => 'E',
460
            'ê' => 'e',
461
            'Ê' => 'E',
462
            'í' => 'i',
463
            'Í' => 'I',
464
            'ï' => 'i',
465
            'Ï' => 'I',
466
            'ì' => 'i',
467
            'Ì' => 'I',
468
            'î' => 'i',
469
            'Î' => 'I',
470
            'ľ' => 'l',
471
            'Ľ' => 'L',
472
            'ĺ' => 'l',
473
            'Ĺ' => 'L',
474
            'ń' => 'n',
475
            'Ń' => 'N',
476
            'ň' => 'n',
477
            'Ň' => 'N',
478
            'ñ' => 'n',
479
            'Ñ' => 'N',
480
            'ó' => 'o',
481
            'Ó' => 'O',
482
            'ö' => 'o',
483
            'Ö' => 'O',
484
            'ô' => 'o',
485
            'Ô' => 'O',
486
            'ò' => 'o',
487
            'Ò' => 'O',
488
            'õ' => 'o',
489
            'Õ' => 'O',
490
            'ő' => 'o',
491
            'Ő' => 'O',
492
            'ř' => 'r',
493
            'Ř' => 'R',
494
            'ŕ' => 'r',
495
            'Ŕ' => 'R',
496
            'š' => 's',
497
            'Š' => 'S',
498
            'ś' => 's',
499
            'Ś' => 'S',
500
            'ť' => 't',
501
            'Ť' => 'T',
502
            'ú' => 'u',
503
            'Ú' => 'U',
504
            'ů' => 'u',
505
            'Ů' => 'U',
506
            'ü' => 'u',
507
            'Ü' => 'U',
508
            'ù' => 'u',
509
            'Ù' => 'U',
510
            'ũ' => 'u',
511
            'Ũ' => 'U',
512
            'û' => 'u',
513
            'Û' => 'U',
514
            'ý' => 'y',
515
            'Ý' => 'Y',
516
            'ž' => 'z',
517
            'Ž' => 'Z',
518
            'ź' => 'z',
519
            'Ź' => 'Z',
520
        ];
521
522
        return @iconv('UTF-8', 'ASCII//TRANSLIT', strtr($text, $convertTable));
523
    }
524
525
    /**
526
     * Encrypt.
527
     * Šifrování.
528
     *
529
     * @param string $textToEncrypt plaintext
530
     * @param string $encryptKey    klíč
531
     *
532
     * @return string encrypted text
533
     */
534
    public static function easeEncrypt($textToEncrypt, $encryptKey)
535
    {
536
        $encryption_key = base64_decode($encryptKey);
537
        $iv             = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
538
        $encrypted      = openssl_encrypt($textToEncrypt, 'aes-256-cbc',
539
            $encryption_key, 0, $iv);
540
        return base64_encode($encrypted.'::'.$iv);
541
    }
542
543
    /**
544
     * Decrypt
545
     * Dešifrování.
546
     *
547
     * @param string $textToDecrypt šifrovaný text
548
     * @param string $encryptKey    šifrovací klíč
549
     *
550
     * @return string
551
     */
552
    public static function easeDecrypt($textToDecrypt, $encryptKey)
553
    {
554
        $encryption_key = base64_decode($encryptKey);
555
        list($encrypted_data, $iv) = explode('::',
556
            base64_decode($textToDecrypt), 2);
557
        return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key,
558
            0, $iv);
559
    }
560
561
    /**
562
     * Generování náhodného čísla.
563
     *
564
     * @param int $minimal
565
     * @param int $maximal
566
     *
567
     * @return float
568
     */
569
    public static function randomNumber($minimal = null, $maximal = null)
570
    {
571
        mt_srand((float) microtime() * 1000000);
0 ignored issues
show
Bug introduced by
(double)microtime() * 1000000 of type double is incompatible with the type integer expected by parameter $seed of mt_srand(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

571
        mt_srand(/** @scrutinizer ignore-type */ (float) microtime() * 1000000);
Loading history...
572
        if (isset($minimal) && isset($maximal)) {
573
            if ($minimal >= $maximal) {
574
                $rand = false;
575
            } else {
576
                $rand = mt_rand($minimal, $maximal);
577
            }
578
        } else {
579
            $rand = mt_rand();
580
        }
581
582
        return $rand;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $rand could also return false which is incompatible with the documented return type double. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
583
    }
584
585
    /**
586
     * Vrací náhodný řetězec dané délky.
587
     *
588
     * @param int $length
589
     *
590
     * @return string
591
     */
592
    public static function randomString($length = 6)
593
    {
594
        return substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'),
595
            0, $length);
596
    }
597
598
    /**
599
     * Rekurzivně překóduje pole.
600
     *
601
     * @param string $in_charset
602
     * @param string $out_charset
603
     * @param array  $arr         originální pole
604
     *
605
     * @return array překódované pole
606
     */
607
    public function recursiveIconv($in_charset, $out_charset, $arr)
608
    {
609
        if (!is_array($arr)) {
0 ignored issues
show
introduced by
The condition is_array($arr) is always true.
Loading history...
610
            return iconv($in_charset, $out_charset, $arr);
611
        }
612
        $ret = $arr;
613
        array_walk_recursive($ret, [$this, 'arrayIconv'],
614
            [$in_charset, $out_charset]);
615
616
        return $ret;
617
    }
618
619
    /**
620
     * Pomocná funkce pro překódování vícerozměrného pole.
621
     *
622
     * @see recursiveIconv
623
     *
624
     * @param mixed  $val
625
     * @param string $key
626
     * @param mixed  $encodings
627
     */
628
    public function arrayIconv(&$val, $key, $encodings)
629
    {
630
        $val = iconv($encodings[0], $encodings[1], $val);
631
    }
632
633
    /**
634
     * Zobrazí velikost souboru v srozumitelném tvaru.
635
     *
636
     * @param int $filesize bytů
637
     *
638
     * @return string
639
     */
640
    public static function humanFilesize($filesize)
641
    {
642
        if (is_numeric($filesize)) {
0 ignored issues
show
introduced by
The condition is_numeric($filesize) is always true.
Loading history...
643
            $decr   = 1024;
644
            $step   = 0;
645
            $prefix = ['Byte', 'KB', 'MB', 'GB', 'TB', 'PB'];
646
647
            while (($filesize / $decr) > 0.9) {
648
                $filesize = $filesize / $decr;
649
                ++$step;
650
            }
651
652
            return round($filesize, 2).' '.$prefix[$step];
653
        } else {
654
            return 'NaN';
655
        }
656
    }
657
658
    /**
659
     * Reindex Array by given key.
660
     *
661
     * @param array  $data    array to reindex
662
     * @param string $indexBy one of columns in array
663
     *
664
     * @return array
665
     */
666
    public static function reindexArrayBy($data, $indexBy = null)
667
    {
668
        $reindexedData = [];
669
670
        foreach ($data as $dataID => $data) {
0 ignored issues
show
introduced by
$data is overwriting one of the parameters of this function.
Loading history...
671
            if (array_key_exists($indexBy, $data)) {
672
                $reindexedData[$data[$indexBy]] = $data;
673
            } else {
674
                throw new \Exception(sprintf('Data row does not contain column %s for reindexing',
675
                    $indexBy));
676
            }
677
        }
678
679
        return $reindexedData;
680
    }
681
682
    /**
683
     * Filter Only letters from string.
684
     * Pouze malé a velké písmena.
685
     *
686
     * @return string text bez zvláštních znaků
687
     */
688
    public static function lettersOnly($text)
689
    {
690
        return preg_replace('/[^(a-zA-Z0-9)]*/', '', $text);
691
    }
692
693
    /**
694
     * Confirm that string is serialized
695
     * 
696
     * @param string $data
697
     *
698
     * @return boolean
699
     */
700
    public static function isSerialized($data)
701
    {
702
        // if it isn't a string, it isn't serialized
703
        if (!is_string($data)) return false;
0 ignored issues
show
introduced by
The condition is_string($data) is always true.
Loading history...
704
        $data = trim($data);
705
        if ('N;' == $data) return true;
706
        if (!preg_match('/^([adObis]):/', $data, $badions)) return false;
707
        switch ($badions[1]) {
708
            case 'a' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
709
            case 'O' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
710
            case 's' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
711
                if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data))
712
                        return true;
713
                break;
714
            case 'b' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
715
            case 'i' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
716
            case 'd' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
717
                if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data))
718
                        return true;
719
                break;
720
        }
721
        return false;
722
    }
723
    
724
    /**
725
     * Get Classname without namespace prefix
726
     * 
727
     * @param object $object
728
     * 
729
     * @return string
730
     */
731
    static public function baseClassName($object)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
732
    {
733
        return basename(str_replace('\\', '/', get_class($object)));
734
    }
735
    
736
    
737
    
738
}
739