CastsAttributes::parseDate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Casts Attributes Class
4
 */
5
6
namespace Twigger\UnionCloud\API\Traits;
7
8
use Carbon\Carbon;
9
use Twigger\UnionCloud\API\Exception\Resource\ResourceNotFoundException;
10
use Twigger\UnionCloud\API\ResourceCollection;
11
12
/**
13
 * Contains functions to aid with casting variables
14
 *
15
 * Trait CastsAttributes
16
 *
17
 * @package Twigger\UnionCloud\API\Core\Traits
18
 */
19
trait CastsAttributes
20
{
21
22
    /**
23
     * Casts the attribute to a Carbon instance
24
     *
25
     * @param string $attributeValue
26
     *
27
     * @return Carbon
28
     */
29
    public function parseDate($attributeValue)
30
    {
31
        return Carbon::parse($attributeValue);
32
    }
33
34
    /**
35
     * Convert a string into proper case
36
     *
37
     * @param string $attributeValue
38
     *
39
     * @return string
40
     */
41
    public function parseProperCase($attributeValue)
42
    {
43
        // https://www.media-division.com/correct-name-capitalization-in-php/
44
        $word_splitters = array(' ', '-', "O'", "L'", "D'", 'St.', 'Mc');
45
        $lowercase_exceptions = array('the', 'van', 'den', 'von', 'und', 'der', 'de', 'da', 'of', 'and', "l'", "d'");
46
        $uppercase_exceptions = array('III', 'IV', 'VI', 'VII', 'VIII', 'IX');
47
48
        $attributeValue = strtolower($attributeValue);
49
        foreach ($word_splitters as $delimiter)
50
        {
51
            $words = explode($delimiter, $attributeValue);
52
            $newWords = array();
53
            foreach ($words as $word)
54
            {
55
                if (in_array(strtoupper($word), $uppercase_exceptions))
56
                    $word = strtoupper($word);
57
                else
58
                    if (!in_array($word, $lowercase_exceptions))
59
                        $word = ucfirst($word);
60
61
                $newWords[] = $word;
62
            }
63
64
            if (in_array(strtolower($delimiter), $lowercase_exceptions))
65
                $delimiter = strtolower($delimiter);
66
67
            $attributeValue = join($delimiter, $newWords);
68
        }
69
        return $attributeValue;
70
    }
71
72
    /**
73
     * Parse a custom resource.
74
     *
75
     * Will pass each of the attribute values into the constructor of the resourceClass
76
     *
77
     * @param array $attributeValue
78
     * @param string $resourceClass
79
     *
80
     * @return ResourceCollection
81
     *
82
     * @throws ResourceNotFoundException
83
     */
84
    public function parseCustomResource($attributeValue, $resourceClass)
85
    {
86
        $collection = new ResourceCollection();
87
        foreach($attributeValue as $resource)
88
        {
89
            try{
90
                $collection->addResource(new $resourceClass($resource));
91
            } catch (\Exception $e)
92
            {
93
                throw new ResourceNotFoundException('Couldn\'t find the specified resource '.$resourceClass, 404, $e);
94
            }
95
        }
96
        return $collection;
97
    }
98
}