PHP code example of azaharizaman / nexus-content

1. Go to this page and download the library: Download azaharizaman/nexus-content library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

azaharizaman / nexus-content example snippets


use Nexus\Content\Services\ArticleManager;
use Nexus\Content\ValueObjects\ArticleCategory;

// Inject dependencies (see Integration Guide)
$articleManager = new ArticleManager($repository, $searchEngine);

// Create category
$category = ArticleCategory::createRoot(
    categoryId: 'cat-001',
    name: 'Getting Started',
    slug: 'getting-started',
    description: 'Beginner guides and tutorials'
);

// Create article with initial draft
$article = $articleManager->createArticle(
    articleId: 'art-001',
    title: 'How to Get Started',
    slug: 'how-to-get-started',
    category: $category,
    textContent: '# Getting Started\n\nWelcome to our platform...',
    authorId: 'user-123',
    isPublic: true
);

// Publish latest draft
$publishedArticle = $articleManager->publish('art-001');

// Article is now searchable via ContentSearchInterface

$updatedArticle = $articleManager->updateContent(
    articleId: 'art-001',
    textContent: '# Getting Started\n\nUpdated content...',
    authorId: 'user-123'
);

// New draft version created, previous version preserved in history

// Submit for review
$article = $articleManager->submitForReview('art-001');

// Approve and publish
$article = $articleManager->publish('art-001');

// Create and publish in one flow
$article = $articleManager->createArticle(/* ... */);
$publishedArticle = $articleManager->publish($article->articleId);

// Get canonical URL
$url = $articleManager->getCanonicalUrl($article, 'https://kb.example.com');
// Returns: https://kb.example.com/kb/how-to-get-started

// Create child category
$subCategory = ArticleCategory::createChild(
    categoryId: 'cat-002',
    name: 'API Reference',
    slug: 'api-reference',
    parentCategoryId: 'cat-001',
    parentLevel: 1
);

// Submit draft for review
$article = $articleManager->submitForReview('art-001');

// Compare versions
$diff = $articleManager->compareVersions(
    articleId: 'art-001',
    versionId1: 'v1',
    versionId2: 'v2'
);
// Returns: ['added' => [...], 'removed' => [...], 'unchanged' => [...]]

// Schedule future publish
$article = $articleManager->createArticle(
    articleId: 'art-002',
    title: 'Product Launch',
    slug: 'product-launch',
    category: $category,
    textContent: '# New Product\n\nAvailable Q2 2025...',
    authorId: 'user-123',
    isPublic: true,
    options: [
        'scheduledPublishAt' => new \DateTimeImmutable('2025-06-01 09:00:00'),
    ]
);

// Lock for editing
$lockedArticle = $articleManager->lockForEditing(
    articleId: 'art-001',
    userId: 'user-123',
    durationMinutes: 30
);

// Create translation
$frenchArticle = $articleManager->createArticle(
    articleId: 'art-001-fr',
    title: 'Comment Commencer',
    slug: 'comment-commencer',
    category: $category,
    textContent: '# Comment Commencer...',
    authorId: 'user-123',
    isPublic: true,
    options: [
        'translationGroupId' => 'grp-001',
        'languageCode' => 'fr-FR',
    ]
);

// Access control
$restrictedArticle = $articleManager->createArticle(
    articleId: 'art-003',
    title: 'Internal Sales Guide',
    slug: 'internal-sales-guide',
    category: $category,
    textContent: '# Sales Team Only...',
    authorId: 'user-123',
    isPublic: false,
    options: [
        'accessControlPartyIds' => ['party-sales-team', 'party-management'],
    ]
);

// Faceted search
use Nexus\Content\ValueObjects\SearchCriteria;

$results = $articleManager->search(
    SearchCriteria::forParty('party-sales-team', 'pricing')
);

// app/Repositories/EloquentContentRepository.php
namespace App\Repositories;

use Nexus\Content\Contracts\ContentRepositoryInterface;
use Nexus\Content\ValueObjects\Article;
use App\Models\Article as ArticleModel;

final class EloquentContentRepository implements ContentRepositoryInterface
{
    public function saveArticle(Article $article): void
    {
        ArticleModel::updateOrCreate(
            ['article_id' => $article->articleId],
            [
                'title' => $article->title,
                'slug' => $article->slug,
                'category_id' => $article->category->categoryId,
                'is_public' => $article->isPublic,
                'version_history' => json_encode($article->versionHistory),
                // ... other fields
            ]
        );
    }
    
    public function findById(string $articleId): ?Article
    {
        $model = ArticleModel::where('article_id', $articleId)->first();
        
        if (!$model) {
            return null;
        }
        
        // Reconstruct Article value object from model
        return $this->hydrateArticle($model);
    }
    
    // ... implement other methods
}

// app/Providers/ContentServiceProvider.php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Nexus\Content\Contracts\ContentRepositoryInterface;
use Nexus\Content\Contracts\ContentSearchInterface;
use App\Repositories\EloquentContentRepository;
use App\Services\MeilisearchContentSearch;

class ContentServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(ContentRepositoryInterface::class, EloquentContentRepository::class);
        $this->app->singleton(ContentSearchInterface::class, MeilisearchContentSearch::class);
    }
}