Completed
Push — 186-data ( d4ee1e )
by
unknown
08:25 queued 10s
created

ChainHydrateTemplate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A hydrateTemplate() 0 5 2
A create() 0 12 3
1
<?php
2
3
namespace League\Plates\HydrateTemplate;
4
5
use League\Plates;
6
7
/** takes an array of hydrate templates and chains them together */
8
class ChainHydrateTemplate implements Plates\HydrateTemplate
9
{
10
    private $hydrate_templates;
11
12
    public function __construct(array $hydrate_templates) {
13
        $this->hydrate_templates = $hydrate_templates;
14
    }
15
16
    public function hydrateTemplate(Plates\Template $template) {
17
        foreach ($this->hydrate_templates as $ht) {
18
            $ht->hydrateTemplate($template);
19
        }
20
    }
21
22
    /** This constructor will map any callables into CallableHydrateTemplate */
23
    public static function create(array $hydrate_templates) {
24
        return new self(array_map(function($ht) {
25
            if ($ht instanceof Plates\HydrateTemplate) {
26
                return $ht;
27
            }
28
            if (is_callable($ht)) {
29
                return new CallableHydrateTemplate($ht);
30
            }
31
32
            throw new Plates\Exception\HydrateTemplateException('Expected instance of HydrateTemplate, got ' . Plates\Util\debugType($ht));
33
        }, $hydrate_templates));
34
    }
35
}
36