onpage-dev / onpage-php
Installs: 2 318
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 5
Open Issues: 0
Requires
- php: ^7.4 || ^8.0
- guzzlehttp/guzzle: ^7.9 || ^6.0
- illuminate/collections: ^8 || ^9 || ^10 || ^11
Requires (Dev)
- phpunit/phpunit: ^9.6 || ^11
- symfony/dotenv: ^5.4 || ^7
- dev-main
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.80
- 1.1.79
- 1.1.78
- 1.1.77
- 1.1.76
- 1.1.75
- 1.1.74
- 1.1.73
- 1.1.72
- 1.1.71
- 1.1.70
- 1.1.69
- 1.1.68
- 1.1.67
- 1.1.66
- 1.1.65
- 1.1.64
- 1.1.63
- 1.1.62
- 1.1.61
- 1.1.60
- 1.1.59
- 1.1.58
- 1.1.57
- 1.1.56
- 1.1.55
- 1.1.54
- 1.1.53
- 1.1.52
- 1.1.51
- 1.1.50
- 1.1.49
- 1.1.48
- 1.1.47
- 1.1.46
- 1.1.45
- 1.1.44
- 1.1.43
- 1.1.42
- 1.1.41
- 1.1.40
- 1.1.39
- 1.1.38
- 1.1.37
- 1.1.36
- 1.1.35
- 1.1.34
- 1.1.33
- 1.1.32
- 1.1.31
- 1.1.30
- 1.1.29
- 1.1.28
- 1.1.27
- 1.1.26
- 1.1.25
- 1.1.24
- 1.1.23
- 1.1.22
- 1.1.21
- 1.1.20
- 1.1.19
- 1.1.18
- 1.1.17
- 1.1.16
- 1.1.15
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.23
- 1.0.22
- 1.0.21
- 1.0.20
- 1.0.19
- 1.0.18
- 1.0.17
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is not auto-updated.
Last update: 2024-12-17 15:19:21 UTC
README
With this library you can easy query your data using an On Page ® API token.
Installation
To install this library in your existing composer project, or update to the latest version, you can launch:
composer require onpage-dev/onpage-php:^v1.2
Of course, remember to include the composer autoload:
<?php require 'vendor/autoload.php';
Usage
Setup
To start, you need to connect to an On Page Schema (project) using the following function:
$schema = \OnPage\Schema::fromToken('MY-API-TOKEN');
Query your data
// Retrieve all records of a resource (returns a laravel collection of \OnPage\Thing) $products = $schema->query('products')->all(); foreach ($products as $prod) { // ... } // Get only the first item $prod = $schema->query('products')->first();
Filters and deletions
// Retrieve all records of a resource (returns a laravel collection of \OnPage\Thing) // NOTE: system fields must be prefixed with the _ symbol $schema->query('products') ->where('_id', 42) // = is the default operator ->where('_created_at', '<', '2024-01-01 00:00:00') ->where('_updated_at', '>=', '2024-01-01 00:00:00') ->first(); // Other filters $schema->query('products') ->where('name', 'like', 'shoes') // you can specify a different operator ->where('category.name', 'Nike') // you can query relations ->where('dimension', '>', 10) // you get it ->whereIn('size', [42, 43, 44]) ->all(); // returns a collection with all your records // Join filters with the OR clause: get all products for the adidas or nike brands $schema->query('products') ->whereOneOf(function(\OnPage\QueryBuilder $q) { ->where('price', 'Nike') ->where('brand', 'Adidas') }) ->all(); // Advanced filtering by relation $schema->query('products') // only retrieve products that have at least one associated category ->whereHas('category') // only retrieve products that have zero associated categories ->whereHas('category', null, '=', 0) // only retrieve products that have at least one variant with at least one color ->whereHas('variant.color') // Only get products that have at least one category that satisfies these 2 conditions: ->whereHas('category', function(\OnPage\QueryBuilder $q) { $q->where('is_online', true); $q->where('name', 'like', 'shoes'); }) ->all(); // You can just simply move data to trash the same way: $schema->query('products') ->where(...) ->delete(); // Or delete elements bypassing the trash: $schema->query('products') ->where(...) ->delete(forever: true); // Filter by element status trash, any $schema->query('products') ->where(...) ->delete(forever: true);
Get thing values
Use the val() function to get the first value in a field. Use the values() function to get all values in a field as a collection.
$cat = $schema->query('categories')->first(); echo $cat->id; // item ID echo $cat->created_at; // creation date e.g. 2022-01-01 23:33:00 echo $cat->updated_at; // date of last update to any of the fields e.g. 2022-01-01 23:33:00 echo $cat->order; // global order number (float) echo $cat->val('name'); echo $cat->val('dimension'); echo $cat->val('description', 'zh'); // you can specify a language // Or set the default language $schema->lang = 'zh'; echo $cat->val('name'); // 再见 // You can also set a fallback language $schema->lang = 'zh'; $schema->fallback_lang = 'en'; echo $cat->val('description'); // English description if chinese description is missing // The values function is useful for multivalue fields, it will return a laravel collection of values. echo $cat->values('bullet_points')->implode('; ');
Files
For image
and file
fields, the returned value will be an instance of \OnPage\File::class
.
To get a file or image url use the ->link()
function. The link will point to the original file.
# original size $product->file('specsheet')->name // icecream-spec.pdf $product->file('specsheet')->token // R417C0YAM90RF $product->file('specsheet')->link() // https://acme-inc.onpage.it/api/storage/R417C0YAM90RF?name=icecream-spec.pdf // Force download (by default the browser will try to preview the file, e.g. pdf/images) $product->file('specsheet')->link([ 'download' => true ]) // Customize file name $product->file('specsheet')->link([ 'name' => 'my custom name.pdf' ])
To turn images into a thumbnail add an array of options as shown below:
# maintain proportions width 200px $product->file('cover_image')->link(['x' => 200]) # maintain proportions height 100px $product->file('cover_image')->link(['y' => 100]) # crop image to width 200px and height 100px $product->file('cover_image')->link(['x' => 200, 'y' => 100]) # maintain proportions and contain in a rectangle of width 200px and height 100px $product->file('cover_image')->link(['x' => 200, 'y' => 100, 'contain' => true]) # convert the image to png (default thumbnail extension is png) $product->file('cover_image')->link(['x' => 200, 'ext' => 'png'])
Other utilities
// Speed things up by only loading some fields $schema->query('products')->loadFields(['title'])->all(); // You can also limit the fields on a related item $schema->query('products') ->with([ 'colors' ]) ->loadRelationFields('colors', ['name', 'image']) // only load 2 fields for the "color" relation ->all(); // Get a mapping between two fields or a field and the thing ID $schema->query('products')->map('code'); // [ 'MYSKU100' => 1827, 'MYSKU101' => 1828, ... ] $schema->query('products')->map('code', 'title'); // [ 'MYSKU100' => 'Apples', 'MYSKU101' => 'Bananas', ... ]
Get thing relations
// You need to specify the relations using the "with" method $cat = $schema->query('categories') ->with('subcategories') ->first(); $subcategories = $cat->rel('subcategories'); foreach ($subcategories as $subcategory) { echo $subcategory->val('name'); } // You can also preload nested subcategories $cat = $schema->query('categories') ->with('subcategories.articles.colors') ->first(); // Or you can pass the relations as an array $products_with_colors = $schema->query('products') ->with([ 'colors', 'categories' ]) ->all(); foreach ($products_with_colors as $prod) { echo $prod->val('name'); foreach ($prod->colors as $color) { echo $color->val('name'); } } // If you need to filter the related items you want to download, you can do this: $cat = $schema->query('categories') ->with('subcategories.articles.colors') ->filterRelation('subcategories.articles', function(\OnPage\QueryBuilder $q) { $q->where('is_online', true); }) ->first();
Creating and updating things
To create or update a record, you need to create a Thing Editor. There are two ways to get a Thing Editor:
- Using the Resource Writer
- Calling
->editor()
on aOp\Thing
Using the Resource Writer (first method)
This class allows you to edit many records at once. You can easily obtain the editor calling:
$writer = $schema->resource('categories')->writer();
Now that you have a Resource Writer, you can use it to create things:
$editor = $writer->createThing(); $editor->set('name', 'Element 1'); $editor->setRel('category', [ 12345 ]); // array with category IDs
...and to update existing things:
$editor = $writer->updateThing(736251); // The id of the element you want to update $editor->set('description', 'Element 1 description');
Finally, you need to send the request to the On Page server:
// this will create and update all the things as requested above $writer->save();
Updating a single item (second method)
$product = $schema->query('products')->where('name', 'Plastic Duck')->first(); $editor = $product->editor(); $editor->set('description', 'This yellow plastic duck will be your best friend'); $editor->set('description', '这只黄色塑料鸭将是你最好的朋友', 'zh'); // you can specify language // Save all the edits at once using the save method $editor->save();
Limiting modified languages
By default, even if you update a single language, the writer will delete the data on other languages. If you only need to edit certain languages and maintain the current values for the others, you can specify which languages you are working on as follows:
// Update the chinese description without deleting the english description: $editor = $product->editor(); $editor->setLangs([ 'zh' ]); $editor->set('description', '这只'); $editor->save();
Updating translations
Just add the language code as the third argument to the set
function:
// Update the value in the default language $editor->set('description', 'This yellow plastic duck will be your best friend'); // Specify another the language $editor->set('description', '这只黄色塑料鸭将是你最好的朋友', 'zh');
Updating files
You can upload files to On Page using the FileUpload class:
$editor->set('image', new \OnPage\FileUpload('/path/to/bird.jpg')); // upload file
Or you can also upload a file using a public URL:
$editor->set('image', 'https://mysite.com/bird_cover.jpg'); // specify file by url
Updating multivalue fields
For multivalue fields you only need to replace ->set
with ->setValues
and pass an array of values as the second argument:
$editor->setValues('bullet_points', [ 'Durable plastic', 'Bright yellow color', 'Compostable' ]);
Updating relations
To update relations, you can use the ->setRel(relation_name, related_ids)
:
$editor->setRel('features', [ 425790, 547023, 240289, ]);
Get structure information
// Retrieve info about the schema: echo $schema->label; // Retrieve a resource given its name or ID $res = $schema->resource('products'); foreach ($res->fields() as $field) { echo "$field->getLabel()\n"; // Main image echo "$field->getLabel('zh')\n"; // "Main Image" but in Chinese echo "$field->name\n"; // "main_image" echo "$field->type\n"; // string|file|image|real|int|text|... echo "$field->unit\n"; // null|kg|... echo "$field->is_multiple\n"; // true|false echo "$field->is_translatable\n"; // true|false }