gedcomx-php
  • Namespace
  • Class

Namespaces

  • Gedcomx
    • Agent
    • Atom
    • Common
    • Conclusion
    • Extensions
      • FamilySearch
        • Platform
          • Artifacts
          • Discussions
          • Tree
          • Users
        • Rt
        • Types
    • GedcomxFile
    • Links
    • Records
    • Rt
    • Search
    • Source
    • Support
    • Types
    • Util
    • Vocab

Classes

  • Gedcomx\Agent\Address
  • Gedcomx\Agent\Agent
  • Gedcomx\Agent\OnlineAccount
  • Gedcomx\Atom\Category
  • Gedcomx\Atom\CommonAttributes
  • Gedcomx\Atom\Content
  • Gedcomx\Atom\Entry
  • Gedcomx\Atom\ExtensibleElement
  • Gedcomx\Atom\Feed
  • Gedcomx\Atom\Generator
  • Gedcomx\Atom\Person
  • Gedcomx\Common\Attribution
  • Gedcomx\Common\CustomEntity
  • Gedcomx\Common\CustomKeyedItem
  • Gedcomx\Common\EvidenceReference
  • Gedcomx\Common\ExtensibleData
  • Gedcomx\Common\Note
  • Gedcomx\Common\Qualifier
  • Gedcomx\Common\ResourceReference
  • Gedcomx\Common\TextValue
  • Gedcomx\Common\UniqueCustomKeyedItem
  • Gedcomx\Conclusion\Conclusion
  • Gedcomx\Conclusion\DateInfo
  • Gedcomx\Conclusion\DisplayProperties
  • Gedcomx\Conclusion\Document
  • Gedcomx\Conclusion\Event
  • Gedcomx\Conclusion\EventRole
  • Gedcomx\Conclusion\Fact
  • Gedcomx\Conclusion\Gender
  • Gedcomx\Conclusion\Identifier
  • Gedcomx\Conclusion\Name
  • Gedcomx\Conclusion\NameForm
  • Gedcomx\Conclusion\NamePart
  • Gedcomx\Conclusion\Person
  • Gedcomx\Conclusion\PlaceDescription
  • Gedcomx\Conclusion\PlaceDisplayProperties
  • Gedcomx\Conclusion\PlaceReference
  • Gedcomx\Conclusion\Relationship
  • Gedcomx\Conclusion\Subject
  • Gedcomx\Extensions\FamilySearch\Error
  • Gedcomx\Extensions\FamilySearch\FamilySearchPlatform
  • Gedcomx\Extensions\FamilySearch\Feature
  • Gedcomx\Extensions\FamilySearch\HealthConfig
  • Gedcomx\Extensions\FamilySearch\OAuth2
  • Gedcomx\Extensions\FamilySearch\Platform\Artifacts\ArtifactMetadata
  • Gedcomx\Extensions\FamilySearch\Platform\Artifacts\ArtifactType
  • Gedcomx\Extensions\FamilySearch\Platform\Discussions\Comment
  • Gedcomx\Extensions\FamilySearch\Platform\Discussions\Discussion
  • Gedcomx\Extensions\FamilySearch\Platform\Tree\ChangeInfo
  • Gedcomx\Extensions\FamilySearch\Platform\Tree\ChangeObjectModifier
  • Gedcomx\Extensions\FamilySearch\Platform\Tree\ChangeObjectType
  • Gedcomx\Extensions\FamilySearch\Platform\Tree\ChangeOperation
  • Gedcomx\Extensions\FamilySearch\Platform\Tree\ChildAndParentsRelationship
  • Gedcomx\Extensions\FamilySearch\Platform\Tree\DiscussionReference
  • Gedcomx\Extensions\FamilySearch\Platform\Tree\MatchInfo
  • Gedcomx\Extensions\FamilySearch\Platform\Tree\MatchStatus
  • Gedcomx\Extensions\FamilySearch\Platform\Tree\Merge
  • Gedcomx\Extensions\FamilySearch\Platform\Tree\MergeAnalysis
  • Gedcomx\Extensions\FamilySearch\Platform\Tree\MergeConflict
  • Gedcomx\Extensions\FamilySearch\Platform\Users\User
  • Gedcomx\Extensions\FamilySearch\Rt\FamilySearchPlatformLocalReferenceResolver
  • Gedcomx\Extensions\FamilySearch\Rt\FamilySearchPlatformModelVisitorBase
  • Gedcomx\Extensions\FamilySearch\Tag
  • Gedcomx\Extensions\FamilySearch\Types\FactType
  • Gedcomx\Gedcomx
  • Gedcomx\GedcomxFile\DefaultJsonSerialization
  • Gedcomx\GedcomxFile\DefaultXMLSerialization
  • Gedcomx\GedcomxFile\GedcomxFile
  • Gedcomx\GedcomxFile\GedcomxFileEntry
  • Gedcomx\GedcomxFile\GedcomxOutput
  • Gedcomx\GedcomxFile\Manifest
  • Gedcomx\GedcomxFile\ManifestAttribute
  • Gedcomx\Links\HypermediaEnabledData
  • Gedcomx\Links\Link
  • Gedcomx\Records\Collection
  • Gedcomx\Records\CollectionContent
  • Gedcomx\Records\Field
  • Gedcomx\Records\FieldDescriptor
  • Gedcomx\Records\FieldValue
  • Gedcomx\Records\FieldValueDescriptor
  • Gedcomx\Records\RecordDescriptor
  • Gedcomx\Records\RecordSet
  • Gedcomx\Records\RecordSetIterator
  • Gedcomx\Rt\GedcomxLocalReferenceResolver
  • Gedcomx\Rt\GedcomxModelVisitorBase
  • Gedcomx\Search\ResultConfidence
  • Gedcomx\Source\CitationField
  • Gedcomx\Source\Coverage
  • Gedcomx\Source\SourceCitation
  • Gedcomx\Source\SourceDescription
  • Gedcomx\Source\SourceReference
  • Gedcomx\Types\ConfidenceLevel
  • Gedcomx\Types\DocumentType
  • Gedcomx\Types\EventRoleType
  • Gedcomx\Types\EventType
  • Gedcomx\Types\FactType
  • Gedcomx\Types\FieldType
  • Gedcomx\Types\FieldValueType
  • Gedcomx\Types\GenderType
  • Gedcomx\Types\IdentifierType
  • Gedcomx\Types\NamePartType
  • Gedcomx\Types\NameType
  • Gedcomx\Types\RecordType
  • Gedcomx\Types\RelationshipType
  • Gedcomx\Types\ResourceType
  • Gedcomx\Util\Collection
  • Gedcomx\Util\Duration
  • Gedcomx\Util\FormalDate
  • Gedcomx\Util\JsonMapper
  • Gedcomx\Util\SimpleDate
  • Gedcomx\Util\XmlMapper
  • Gedcomx\Vocab\VocabElement
  • Gedcomx\Vocab\VocabElementList

Interfaces

  • Gedcomx\Common\Attributable
  • Gedcomx\Common\HasNotes
  • Gedcomx\Conclusion\HasFacts
  • Gedcomx\Extensions\FamilySearch\Rt\FamilySearchPlatformModelVisitor
  • Gedcomx\GedcomxFile\GedcomxEntryDeserializer
  • Gedcomx\GedcomxFile\GedcomxEntrySerializer
  • Gedcomx\Links\SupportsLinks
  • Gedcomx\Records\HasFields
  • Gedcomx\Rt\GedcomxModelVisitor
  • Gedcomx\Source\ReferencesSources
  • Gedcomx\Support\HasJsonKey

Exceptions

  • Gedcomx\GedcomxFile\GedcomxFileException
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 
<?php

namespace Gedcomx\GedcomxFile;

use Gedcomx\Extensions\FamilySearch\FamilySearchPlatform;
use Gedcomx\Gedcomx;

/**
 * Class GedcomxOutput
 * @package Gedcomx\GedcomxFile
 *
 *          Create a .gedx file
 */
class GedcomxOutput
{
    /**
     * @var GedcomEntrySerializer
     */
    private $serializer;
    /**
     * @var /ZipArchive
     */
    private $archive;
    /**
     * @var Manifest
     */
    private $manifest;
    /**
     * @var int
     */
    private $entryCount;
    /**
     * @var GedcomxFileEntry[]
     */
    private $entries;

    /**
     * Create a new instance of the .gedx file creator
     *
     * @param \Gedcomx\GedcomxFile\GedcomxEntrySerializer $serializer
     */
    public function __construct(GedcomxEntrySerializer $serializer = null )
    {
        $this->serializer = $serializer;
        if ($this->serializer == null) {
            $this->serializer = new DefaultXMLSerialization();
        }
        $this->archive = new \ZipArchive();
        $this->manifest = new Manifest();
        $this->manifest->addAttribute('Manifest-Version','1.0');
        $this->manifest->addAttribute('Created-By','Gedcomx-PHP SDK');
    }

    /**
     * Add an attribute to the manifest file for this archive.
     *
     * @param string $key
     * @param string $value
     *
     * @return void
     */
    public function addAttribute($key, $value)
    {
        $this->manifest->addAttribute($key, $value);
    }

    /**
     * Add a manifest file attribute for an entry
     *
     * @param string $name
     * @param string $key
     * @param string $value
     *
     * @return void
     */
    public function addAttributeToEntry($name, $key, $value)
    {
        $this->manifest->addAttributeToEntry($name, $key, $value);
    }

    /**
     * Update a manifest file attribute for an entry
     *
     * @param string $name
     * @param string $key
     * @param string $value
     *
     * @return void
     */
    public function updateEntryAttribute($name, $key, $value)
    {
        $this->manifest->updateEntryAttribute($name, $key, $value);
    }

    /**
     * Add a Gedcomx object to the archive
     *
     * @param \Gedcomx\Gedcomx $resource
     * @param \DateTime        $lastModified
     *
     * @return void
     */
    public function addGedcomxResource(Gedcomx $resource, \DateTime $lastModified = null)
    {
        $this->addResource(Gedcomx::XML_MEDIA_TYPE, $resource, $lastModified);
    }

    /**
     * Add a FamilySearch Extensions object to the archive
     *
     * @param \Gedcomx\Extensions\FamilySearch\FamilySearchPlatform $resource
     * @param \DateTime                                             $lastModified
     *
     * @return void
     */
    public function addFamilySearchResource(FamilySearchPlatform $resource, \DateTime $lastModified = null)
    {
        $this->addResource(FamilySearchPlatform::XML_MEDIA_TYPE, $resource, $lastModified);
    }

    /**
     * Add a file resource to the archive
     *
     * @param string    $filename
     * @param string    $contentType
     * @param \DateTime $lastModified
     */
    public function addFileResource($filename, $contentType = null, \DateTime $lastModified = null)
    {
        if ($lastModified == null) {
            $lastModified = new \DateTime();
        }
        if ($contentType == null) {
            $mimeType = finfo_open(FILEINFO_MIME_TYPE);
           $contentType = finfo_file($mimeType, $filename);
        }
        $attributes['Content-Type'] = $contentType;
        $attributes['X-DC-modified'] = $lastModified->format("c") . "Z";

        $content = file_get_contents($filename);

        $this->addEntry(basename($filename), $attributes, $content);
    }

    /**
     * Write the archive to disk with the given filepath
     *
     * @param $filepath
     *
     * @throws \Gedcomx\GedcomxFile\GedcomxFileException
     */
    public function writeToFile($filepath)
    {
        $success = $this->archive->open($filepath, \ZipArchive::CREATE);
        if ($success !== true) {
            throw new GedcomxFileException($filepath, $success);
        }
        $this->archive->addFromString('META-INF/MANIFEST.MF', $this->manifest->toString());
        foreach ($this->entries as $entry) {
            $this->archive->addFromString($entry->getName(), $entry->getContents());
        }
        $this->archive->close();
    }

    /**
     * Take the given resource, serialize it and add it to the archive.
     *
     * @param string    $contentType
     * @param string    $resource
     * @param \DateTime $lastModified
     */
    protected function addResource($contentType, $resource, \DateTime $lastModified = null)
    {
        if($lastModified == null) {
            $lastModified = new \DateTime();
        }
        $attributes['Content-Type'] = $contentType;
        $attributes['X-DC-modified'] = $lastModified->format("c") . "Z";

        $content = $this->serializer->serialize($resource);

        $name = "tree" . ($this->entryCount > 0 ? $this->entryCount : '') . ".xml";
        $this->addEntry($name, $attributes, $content);
    }

    /**
     * Add the given content to the archive.
     *
     * @param string $name       The name of the content in the archive
     * @param array  $attributes An array of key value pairs
     * @param string $content    The content to add to the archive
     */
    protected function addEntry($name, $attributes, $content)
    {
        $this->manifest->addEntry($name);
        foreach ($attributes as $key => $value) {
            $this->manifest->addAttributeToEntry($name, $key, $value);
        }
        $entry = new GedcomxFileEntry($name, $content, $attributes);
        $this->entries[] = $entry;
    }
}
gedcomx-php API documentation generated by ApiGen