ArrayTypeConverter::checkArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2017 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation\Converter;
11
12
use PommProject\Foundation\Exception\ConverterException;
13
use PommProject\Foundation\Session\Session;
14
15
/**
16
 * ArrayTypeConverter
17
 *
18
 * Array sub class for converters using a PHP array representation.
19
 *
20
 * @package   Foundation
21
 * @copyright 2014 - 2017 Grégoire HUBERT
22
 * @author    Grégoire HUBERT
23
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
24
 * @see       ConverterInterface
25
 */
26
abstract class ArrayTypeConverter implements ConverterInterface
27
{
28
    protected $converters = [];
29
30
    /**
31
     * checkArray
32
     *
33
     * Check if the data is an array.
34
     *
35
     * @param  mixed    $data
36
     * @throws ConverterException
37
     * @return array    $data
38
     */
39
    protected function checkArray($data)
40
    {
41
        if (!is_array($data)) {
42
            throw new ConverterException(
43
                sprintf(
44
                    "Array converter data must be an array ('%s' given).",
45
                    gettype($data)
46
                )
47
            );
48
        }
49
50
        return $data;
51
    }
52
53
    /**
54
     * getSubtypeConverter
55
     *
56
     * Since the arrays in PostgreSQL have the same subtype, it is useful to
57
     * cache it here to avoid summoning the ClientHolder all the time.
58
     *
59
     * @param  string   $type
60
     * @param  Session  $session
61
     * @return ConverterInterface
62
     */
63
    protected function getSubtypeConverter($type, Session $session)
64
    {
65
        if (!isset($this->converters[$type])) {
66
            $this->converters[$type] = $session
67
                ->getClientUsingPooler('converter', $type)
68
                ->getConverter()
69
                ;
70
        }
71
72
        return $this->converters[$type];
73
    }
74
}
75