|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +namespace Shopify\Service; | 
|  | 4 | + | 
|  | 5 | +use Shopify\Object\Metafield; | 
|  | 6 | + | 
|  | 7 | +class MetafieldService extends AbstractService | 
|  | 8 | +{ | 
|  | 9 | +    /** | 
|  | 10 | +     * Retrieve a list of metafields that belong to a resource | 
|  | 11 | +     * | 
|  | 12 | +     * @link   https://help.shopify.com/api/reference/metafield#index | 
|  | 13 | +     * @param array $params | 
|  | 14 | +     * @param string $ownerResource | 
|  | 15 | +     * @param string $ownerId | 
|  | 16 | +     * @return Metafield[] | 
|  | 17 | +     */ | 
|  | 18 | +    public function all(array $params = array(), $ownerResource = '', $ownerId = '') | 
|  | 19 | +    { | 
|  | 20 | +        if ($ownerResource && $ownerId) { | 
|  | 21 | +            $endpoint = '/admin/' . $ownerResource . '/' . $ownerId . '/metafields.json'; | 
|  | 22 | +        } else { | 
|  | 23 | +            $endpoint = '/admin/metafields.json'; | 
|  | 24 | +        } | 
|  | 25 | +        $response = $this->request($endpoint, 'GET', $params); | 
|  | 26 | +        return $this->createCollection(Metafield::class, $response['metafields']); | 
|  | 27 | +    } | 
|  | 28 | +    /** | 
|  | 29 | +     * Receive a count of all metafields | 
|  | 30 | +     * | 
|  | 31 | +     * @link   https://help.shopify.com/api/reference/metafield#count | 
|  | 32 | +     * @param  array $params | 
|  | 33 | +     * @return integer | 
|  | 34 | +     */ | 
|  | 35 | +    public function count(array $params = array()) | 
|  | 36 | +    { | 
|  | 37 | +        $endpoint = '/admin/metafields/count.json'; | 
|  | 38 | +        $response = $this->request($endpoint, 'GET', $params); | 
|  | 39 | +        return $response['count']; | 
|  | 40 | +    } | 
|  | 41 | +    /** | 
|  | 42 | +     * Receive a single metafield | 
|  | 43 | +     * | 
|  | 44 | +     * @link   https://help.shopify.com/api/reference/metafield#show | 
|  | 45 | +     * @param  integer $metafieldId | 
|  | 46 | +     * @param  array   $fields | 
|  | 47 | +     * @return Metafield | 
|  | 48 | +     */ | 
|  | 49 | +    public function get($metafieldId, array $fields = array()) | 
|  | 50 | +    { | 
|  | 51 | +        $params = array(); | 
|  | 52 | +        if (!empty($fields)) { | 
|  | 53 | +            $params['fields'] = $fields; | 
|  | 54 | +        } | 
|  | 55 | +        $endpoint = '/admin/metafields/'.$metafieldId.'.json'; | 
|  | 56 | +        $response = $this->request($endpoint, 'GET', $params); | 
|  | 57 | +        return $this->createObject(Metafield::class, $response['metafield']); | 
|  | 58 | +    } | 
|  | 59 | +    /** | 
|  | 60 | +     * Create a new metafield for a resource | 
|  | 61 | +     * | 
|  | 62 | +     * @link   https://help.shopify.com/api/reference/metafield#create | 
|  | 63 | +     * @param  Metafield $metafield | 
|  | 64 | +     * @param string $ownerResource | 
|  | 65 | +     * @param string $ownerId | 
|  | 66 | +     * @return void | 
|  | 67 | +     */ | 
|  | 68 | +    public function create(Metafield &$metafield, $ownerResource = '', $ownerId = '') | 
|  | 69 | +    { | 
|  | 70 | +        $data = $metafield->exportData(); | 
|  | 71 | +        if ($ownerResource && $ownerId) { | 
|  | 72 | +            $endpoint = '/admin/' . $ownerResource . '/' . $ownerId . '/metafields.json'; | 
|  | 73 | +        } else { | 
|  | 74 | +            $endpoint = '/admin/metafields.json'; | 
|  | 75 | +        } | 
|  | 76 | +        $response = $this->request( | 
|  | 77 | +            $endpoint, 'POST', array( | 
|  | 78 | +                'metafield' => $data | 
|  | 79 | +            ) | 
|  | 80 | +        ); | 
|  | 81 | +        $metafield->setData($response['metafield']); | 
|  | 82 | +    } | 
|  | 83 | +    /** | 
|  | 84 | +     * Modify an existing metafield | 
|  | 85 | +     * | 
|  | 86 | +     * @link   https://help.shopify.com/api/reference/metafield#update | 
|  | 87 | +     * @param  Metafield $metafield | 
|  | 88 | +     * @return void | 
|  | 89 | +     */ | 
|  | 90 | +    public function update(Metafield &$metafield) | 
|  | 91 | +    { | 
|  | 92 | +        $data = $metafield->exportData(); | 
|  | 93 | +        $endpoint = '/admin/metafields/'.$metafield->id.'.json'; | 
|  | 94 | +        $response = $this->request( | 
|  | 95 | +            $endpoint, 'PUT', array( | 
|  | 96 | +                'metafield' => $data | 
|  | 97 | +            ) | 
|  | 98 | +        ); | 
|  | 99 | +        $metafield->setData($response['metafield']); | 
|  | 100 | +    } | 
|  | 101 | +    /** | 
|  | 102 | +     * Remove a metafield | 
|  | 103 | +     * | 
|  | 104 | +     * @link   https://help.shopify.com/api/reference/metafield#destroy | 
|  | 105 | +     * @param  Metafield $metafield | 
|  | 106 | +     * @return void | 
|  | 107 | +     */ | 
|  | 108 | +    public function delete(Metafield &$metafield) | 
|  | 109 | +    { | 
|  | 110 | +        $endpoint = '/admin/metafields/'.$metafield->id.'.json'; | 
|  | 111 | +        $this->request($endpoint, 'DELETE'); | 
|  | 112 | +    } | 
|  | 113 | +} | 
0 commit comments