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

ChainHydrateTemplate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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